Java Data Type How to - Trim minutes and hours and seconds from Date object


We would like to know how to trim minutes and hours and seconds from Date object.


import static java.lang.System.out;
import static java.util.Calendar.HOUR_OF_DAY;
import static java.util.Calendar.MILLISECOND;
import static java.util.Calendar.MINUTE;
import static java.util.Calendar.SECOND;
/*from w  ww  .  jav  a 2 s .  co  m*/
import java.util.Calendar;
import java.util.Date;

public class Main {
  public static void main(String[] args) throws InterruptedException {
    Date date = new Date();
    Date other = new Date();
    out.printf("equals? = %s, hashCode? = %s %n", (date.equals(other)),
        (date.hashCode() == other.hashCode()));

    Date todayeOne = trim(date);
    Date todayTwo = trim(other);

    out.printf("equals? = %s, hashCode? = %s %n", (todayeOne.equals(todayTwo)),
        (todayeOne.hashCode() == todayTwo.hashCode()));


  public static Date trim(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.set(HOUR_OF_DAY, 0);
    cal.set(MINUTE, 0);
    cal.set(SECOND, 0);
    cal.set(MILLISECOND, 0);
    return cal.getTime();


The code above generates the following result.