In this tutorial we will be dicussing about different datetime conversions provided in java 8. Here we will implement different examples to convert Date to LocalDate and LocalDateTime, LocalDateTime to Date, LocalDate to Date,LocalDateTime to ZonedDateTime and vice versa, Date to EpochSeconds, Instants etc. Also, we will take a look into different ways to manipulate Date object in java 8 by adding durations such as Day, Month etc. Also we will take a look into different Date comparisons provided in Java 8.
java.time package in Java 8
The different new date and time API is defined inside java.time package and some useful classes defined in this package are Instant, LocalDate, LocalTime, LocalDateTime, ZonedDateTime, OffsetTime, OffsetDateTime etc.
Instant - It represents an instantaneous point on the time-line like timestamp.
LocalDate - It represents a date without a time-zone in the ISO-8601 calendar system such as 2017-12-05.LocalTime - It represents a time without a time-zone in the ISO-8601 calendar system, such as 10:15:30
LocalDateTime - It represents a date-time without a time-zone in the ISO-8601 calendar system, such as 2017-12-03T10:15:30
ZonedDateTime - It represents a date-time with a time-zone in the ISO-8601 calendar system, such as 2017-12-03T10:15:30+01:00 Europe/Paris.
OffsetTime - It represents a time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 10:15:30+01:00
OffsetDateTime - It represents a date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 2017-12-03T10:15:30+01:00.
Other Interesting Posts Java 8 Lambda Expression Java 8 Stream Operations Java 8 Parallel Streams
Todays Date and Time in Java 8
In java 8 its very simple to get current date and time using the method now()
defined in LocalDateTime class.Following is the example.
LocalDateTime dateAndTime = LocalDateTime.now(); System.out.println(dateAndTime); System.out.println(dateAndTime.getDayOfMonth()); System.out.println(dateAndTime.getDayOfYear()); System.out.println(dateAndTime.getHour());
Output
2017-05-12T22:49:21.026 12 132 22
Date to LocalDateTime Conversion
To convert date to LocalDateTime, first we need to convert the Date to ZonedDateTime and then use toLocalDateTime()
method to convert it to LocalDateTime.
Date date = new Date(); Instant instant = date.toInstant(); ZoneId zoneId = ZoneId.systemDefault(); LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime(); System.out.println("Date = " + date); System.out.println("LocalDateTime = " + localDateTime);Output
Date = Sat May 13 20:59:24 IST 2017 LocalDateTime = 2017-05-13T20:59:24.140
LocalDateTime to Date Conversion
During this conversion the LocalDateTime should be converted to ZonedDateTime first and then to Date. Following is the example.
ZoneId zoneId = ZoneId.systemDefault(); LocalDateTime localDateTime = LocalDateTime.now(); ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId); Date date = Date.from(zonedDateTime.toInstant()); System.out.println("LocalDateTime = " + localDateTime); System.out.println("Date = " + date);Output
LocalDateTime = 2017-05-13T21:17:19.206 Date = Sat May 13 21:17:19 IST 2017
Date to LocalDate Conversion
Here also first we need to convert the Date to ZonedDateTime and then to LocalDate. Following is the example.
Date date = new Date(); Instant instant = date.toInstant(); ZoneId zoneId = ZoneId.systemDefault(); LocalDate localDate = instant.atZone(zoneId).toLocalDate(); System.out.println("Date = " + date); System.out.println("LocalDate = " + localDate);Output
Date = Sat May 13 21:14:42 IST 2017 LocalDate = 2017-05-13
LocalDate to Date Conversion
During this conversion the LocalDate should be converted to ZonedDateTime first and then to Date. Following is the example.
ZoneId zoneId = ZoneId.systemDefault(); LocalDate localDate = LocalDate.now(); ZonedDateTime zonedDateTime = localDate.atStartOfDay(zoneId); Date date = Date.from(zonedDateTime.toInstant()); System.out.println("LocalDate = " + localDate); System.out.println("Date = " + date);Output
LocalDate = 2017-05-13 Date = Sat May 13 00:00:00 IST 2017
Date to EpochSeconds Conversion
Unix time (also known as POSIX time or epoch time) is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, not counting leap seconds. Following is the way to convert Date to EpochSeconds.
ZoneId zoneId = ZoneId.systemDefault(); LocalDate localDate = LocalDate.now(); ZonedDateTime zonedDateTime = localDate.atStartOfDay(zoneId); Date date = Date.from(zonedDateTime.toInstant()); System.out.println("LocalDate = " + localDate); System.out.println("Date = " + date);Output
Date = Sat May 13 21:21:57 IST 2017 epochSeconds = 1494690717
EpochSeconds to ZonedDateTime Conversion
Following is the way to convert Date to EpochSeconds.
Date date = new Date(); Instant instant = date.toInstant(); long epochSeconds = instant.atZone(ZoneId.systemDefault()).toEpochSecond(); ZonedDateTime zonedDateTime = LocalDateTime.ofEpochSecond(epochSeconds, 0, OffsetDateTime.now(ZoneId.systemDefault()).getOffset()).atZone(ZoneId.systemDefault()); System.out.println("epochSeconds = " + epochSeconds); System.out.println("ZonedDateTime = " + zonedDateTime);Output
epochSeconds = 1494691023 ZonedDateTime = 2017-05-13T21:27:03+05:30[Asia/Calcutta]
Arbitrary Date Conversion
In java 8 we can also create a date from any arbitrary date from any given year, month and date. Following is an example. This can be used to verify details such as credit card expiry.
LocalDate localDate = LocalDate.of(2017, 05, 14); System.out.println("Local date : " + localDate);Output
Local date : 2017-05-14
Add Subtract Duration in LocalDateTime
Its also possible to add or subtract durations from Date in java 8.
LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("LocalDateTime : - " + localDateTime); System.out.println("Subtract 90 Days " + localDateTime.minus(90, ChronoUnit.DAYS)); System.out.println("Add 23 Hours : - " + localDateTime.plusHours(23)); System.out.println("Add 1 Month : - " + localDateTime.plus(1, ChronoUnit.MONTHS));Output
LocalDateTime : - 2017-05-13T21:37:38.775 Subtract 90 Days 2017-02-12T21:37:38.775 Add 23 Hours : - 2017-05-14T20:37:38.775 Add 1 Month : - 2017-06-13T21:37:38.775
Comparing Date in Java 8
Java 8 also provides convenient ways to compare dates in simple manner. Following is an example.
LocalDateTime currentDateTime = LocalDateTime.now(); LocalDateTime beforeNow = currentDateTime.minus(12,ChronoUnit.HOURS); if(currentDateTime.isAfter(beforeNow)){ System.out.println(true); }Output
true
Conclusion
I hope this article served you that you were looking for. If you have anything that you want to add or share then please share it below in the comment section.