Just Code‎ > ‎

Java - Truncate the time part of a java.util.Date, removing the hours minutes and seconds etc

posted Jul 11, 2013, 7:25 AM by Peter Henell   [ updated Jul 11, 2013, 7:25 AM ]

private Date getDateWithNoTime(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        Date dateWithNoTime = new Date(cal.getTimeInMillis());
        return dateWithNoTime;
    }
Comments