Comparison in the Old Java Date APIĭate firstDate = toDate(LocalDateTime.of(2019, 8, 10, 0, 00, 00)) ĭate secondDate = toDate(LocalDateTime.of(2019, 8, 15, 0, 00, 00)) ĪssertThat(firstDate.after(secondDate), is(false)) ĪssertThat(firstDate.before(secondDate), is(true)) ĪssertThat(pareTo(secondDate), is(-1)) ĪssertThat(firstDate.equals(secondDate), is(false)) įor more complex comparisons, we can use DateUtils from the Apache Commons Lang library. minute(d), hour(d), day(d), month(d), year(d) minelement(c), maxelement(c), minindex(c), maxindex(c), elements(c), indices(c) str(x) Most of these Hibernate. IsSameHour(zonedTimestamp, zonedTimestampToCompare), is(true)) 5. We can see that two ZonedDateTime objects are actually happening within the same hour, even if their local times are different (8:30 and 14:00, respectively): ZonedDateTime zonedTimestamp = isEqual(uncatedTo(HOURS)) įinally, in a similar way, we can check if two ZonedDateTime instances happen within the same hour: public static boolean isSameHour(ZonedDateTime zonedTimestamp, Thirdly, we can implement a comparison at the level of an hour: public static boolean isSameHour(LocalDateTime timestamp, The truncatedTo(TemporalUnit) method truncates a date on the given level, which in our example is a day. Secondly, we’ll check if two instances of LocalDateTime are on the same day: public static boolean isSameDay(LocalDateTime timestamp, The int compareTo (otherDateTime) method returns: 0 (Zero) if both the date-times represent the same time instance of the day. These methods compare the instances without considering the timezone or calendar information. Overview In this tutorial, we’ll show how to map temporal column values in Hibernate, including the classes from java.sql, java.util and java.time packages. Return timestamp.toLocalDate().isEqual(localDateToCompare) The method compareTo () compares two local date-time objects and returns an integer value based on the comparison. Public static boolean isSameDay(LocalDateTime timestamp, They have different LocalDateTime and ZoneId fields internally: assertThat(timeInNewYork.equals(timeInBerlin), is(false)) ĪssertThat(pareTo(timeInBerlin), is(-1)) 4. ZonedDateTime.of(2019, 8, 10, 14, 0, 0, 0, ZoneId.of("Europe/Berlin")) ĪssertThat(timeInNewYork.isAfter(timeInBerlin), is(false)) ĪssertThat(timeInNewYork.isBefore(timeInBerlin), is(false)) ĪssertThat(timeInNewYork.isEqual(timeInBerlin), is(true)) Īlthough both ZonedDateTime instances represent the same moment in time, they do not represent equal Java objects. Let’s compare 8:00 local time in New York and 14:00 local time in Berlin, on the same day: ZonedDateTime timeInNewYork = Likewise, we can use the same methods for comparing two ZonedDateTime instances. Additionally, equals() and compareTo() can be used in a similar fashion as described for LocalDate. Similarly to LocalDate, we’re comparing two LocalDateTime instances with the methods isAfter(), isBefore() and isEqual().
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |