- https://www.baeldung.com/java-8-date-time-intro
- Issues With the Existing Date/Time APIS:
- Thread safety
- API design and ease of understanding
- ZonedDate and Time
- Working With LocalDate
The LocalDate represents a date in ISO format(yyyy-MM-dd) without time. We can use it to store dates like birthdays and paydays.
And we can get the localDate representing a specific day, month and year by using of method or the parse method.LocalDate localDate = LocalDate.now();
The LocalDate provides various utility methods to obtain a variety of informationLocalDate.of(2015, 02, 20); LocalDate.parse("2015-02-20");
subtracts one month. Note how it accepts an enum as the time unit:LocalDate tomorrow = LocalDate.now().plusDays(1); // get tomorrow date
the relationship of a date to another can be determined to occur before or after date:LocalDate previousMonthSameDay = LocalDate.now().minus(1, ChronoUnit.MONTHS);
boolean notBefore = LocalDate.parse("2016-06-12").isBefore(LocalDate.parse("2016-06-11")); boolean isAfter = LocalDate.parse("2016-06-12").isAfter(LocalDate.parse("2016-06-11"));
- Working With LocalTime
The LocalTime represent time without a date.
Similar to LocalDate, we can create an instance of LocalTime from the system clock or by using parse and of method.LocalTime now = LocalTime.now(); LocalTime sixThirty = LocalTime.parse("06:30"); LocalTime sixThirty1 = LocalTime.of(6, 30);
- Working With LocalDateTime
LocalDateTime is used to represent a combination of date and time. This is the most commonly used class when we need a combination of date and time.LocalDateTime.now(); LocalDateTime.of(2015, Month.FEBRUARY, 20, 06, 30); // 2015-02-20 06:30 // addition and subtration of specific units of time like days, months, years and minutes localDateTime.plusDays(1); localDateTime.minusHours(2); // Getter methods are also available to extract specific units similar to the date and times localDateTime.getMonth();
- Using Period and Duration
The Period class represents a quantity of time in terms of years, months and days, and the Duration class represents a quantity of time in terms of seconds and nanoseconds.LocalDate today = LocalDate.now(); LocalDate finalDate = today.plus(Period.ofDays(5)); long five = ChronoUnit.DAYS.between(today, finalDate);
- Compatibility With Date and Calendar
Java 8 has added the toInstant() method, which helps to convert existing Date and Calendar instance to new Date and Time API:
https://stackoverflow.com/questions/19431234/converting-between-java-time-localdatetime-and-java-util-date// date to LocalDateTime LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); LocalDateTime ldt = LocalDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault()); // LocalDateTime to date Date out = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
- Backport and Alternate Options
For organizations the are on the path of moving to Java 8 from Java 7 or Java 6 and that want to use date and time API, use Joda-Time library<dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.9.4</version> </dependency>