Compare Date object with a TimeStamp in Java

后端 未结 10 888
悲&欢浪女
悲&欢浪女 2020-12-09 02:32

When I test this code:

java.util.Date date = new java.util.Date();
java.util.Date stamp = new java.sql.Timestamp(date.getTime());

assertTrue(date.equals(sta         


        
10条回答
  •  余生分开走
    2020-12-09 03:09

    I've resolved converting both Date and TimeStamp into a Calendar object and then i've compared the single Calendar's properties:

    Calendar date = Calendar.getInstance();
    date.setTimeInMillis(dateObject.getTime());
    Calendar timestamp = Calendar.getInstance();
    timestamp.setTimeInMillis(timestampObject.getTime());
    if (effettoAppendice.get(Calendar.DAY_OF_MONTH) == timestamp.get(Calendar.DAY_OF_MONTH) &&
        effettoAppendice.get(Calendar.MONTH) == timestamp.get(Calendar.MONTH) &&
        effettoAppendice.get(Calendar.YEAR) == timestamp.get(Calendar.YEAR)) {
        System.out.println("Date and Timestamp are the same");
    } else {
        System.out.println("Date and Timestamp are NOT the same");
    }
    

    Hope this helps.

提交回复
热议问题