I have a date in timestamp format. How can I check if it is equal to the current date without regard to time? For example, 1502010000 it is equal to 6.07.2017 .

I received this date and current, but how to compare is not clear:

 Instant date= Instant.ofEpochSecond(1502010000); Instant now = Instant.now(); 

    2 answers 2

    Java 8 introduced a new mechanism for working with dates implemented API from the java.time.* . In this case, you can use the following behavior pattern to compare dates:

     Timestamp timestamp = new Timestamp(1502010000); LocalDateTime before = timestamp.toLocalDateTime(); LocalDateTime now = LocalDateTime.now(); int compareREsult = now.compareTo(before); 

    If compareResult negative number - if the compared date is later, positive - if earlier and zero if equal.

    UPD:

    Since there is an unrecorded condition in the question that the date should be compared without time, the following option is proposed:

     Timestamp timestamp = new Timestamp(1502041448453l); // System.out.println(timestamp); выведет "2017-08-06 20:44:08.453" LocalDate localDateTime = timestamp.toLocalDateTime().toLocalDate(); LocalDate now = LocalDate.now(); // System.out.println(now); выведет "2017-08-06" // Выведет 0; System.out.println(now.compareTo(localDateTime)); 
    • In this case, it should be 0, because 1502010000 - Sunday, August 6, 2017, 09:00:00 and now it is August 6, but your code is 47. - Roman Alexsandrovich
    • Probably we are talking about different timestamp. August 6, 2017 - this is 1502041448453 in the case of java.sql.Timestamp. What type are you using? Timestamp timestamp = new Timestamp (1502041448453l); System.out.println (timestamp); Displays: 2017-08-06 20: 44: 08.453 The only thing that I did not take into account in the question is that you wanted to make a comparison without time. Therefore, I updated the answer with the UPD section. - Mikita Berazouski

    I may not understand something, but:

    https://habrahabr.ru/post/61391/

    TIMESTAMP Stores a 4-byte integer equal to the number of seconds since midnight, January 1, 1970.

    And the number you give gives January 18, 1970, and not August 6, 2017

    http://www.fileformat.info/tip/java/date2millis.htm


    But if everything shows you perfectly, you can use the Calendar and just reset the time.

     private static Date dateRemoveTime(Date date){ Calendar calendar = new GregorianCalendar(); calendar.setTime(date); calendar.set(Calendar.HOUR, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime(); }