

out.println( "Add 23 Hours : - " + usHours(23)) out.println( "Subtract 90 Days " + localDateTime.minus(90, ChronoUnit. out.println( "LocalDateTime : - " + localDateTime) Its also possible to add or subtract durations from Date in java 8. out.println( "Local date : " + localDate) LocalDate localDate = LocalDate.of(2017, 05, 14) This can be used to verify details such as credit card expiry. In java 8 we can also create a date from any arbitrary date from any given year, month and date. out.println( "ZonedDateTime = " + zonedDateTime)

out.println( "epochSeconds = " + epochSeconds) OffsetDateTime.now(ZoneId.systemDefault()).getOffset()).atZone(ZoneId.systemDefault()) ZonedDateTime zonedDateTime = LocalDateTime.ofEpochSecond(epochSeconds, 0, Long epochSeconds = instant.atZone(ZoneId.systemDefault()).toEpochSecond() Following is the way to convert Date to EpochSeconds.įollowing is the way to convert Date to EpochSeconds. 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. ZonedDateTime zonedDateTime = localDate.atStartOfDay(zoneId)

out.println( "LocalDate = " + localDate) ĭuring this conversion the LocalDate should be converted to ZonedDateTime first and then to Date. LocalDate localDate = instant.atZone(zoneId).toLocalDate() Here also first we need to convert the Date to ZonedDateTime and then to LocalDate. ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId) ĭate date = om(zonedDateTime.toInstant()) LocalDateTime localDateTime = LocalDateTime.now() out.println( "LocalDateTime = " + localDateTime) ĭuring this conversion the LocalDateTime should be converted to ZonedDateTime first and then to Date. LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime() To convert date to LocalDateTime, first we need to convert the Date to ZonedDateTime and then use toLocalDateTime() method to convert it to LocalDateTime. out.println(dateAndTime.getDayOfMonth()) LocalDateTime dateAndTime = LocalDateTime.now() In java 8 its very simple to get current date and time using the method now() defined in LocalDateTime class.Following is the example. Other Interesting Posts Java 8 Lambda Expression Java 8 Stream Operations Java 8 Parallel Streams Todays Date and Time in Java 8 OffsetDateTime - It represents a date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as T10:15:30+01:00. OffsetTime - It represents a time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 10:15:30+01:00 ZonedDateTime - It represents a date-time with a time-zone in the ISO-8601 calendar system, such as T10:15:30+01:00 Europe/Paris. LocalDateTime - It represents a date-time without a time-zone in the ISO-8601 calendar system, such as T10:15:30 LocalTime - It represents a time without a time-zone in the ISO-8601 calendar system, such as 10:15:30 LocalDate - It represents a date without a time-zone in the ISO-8601 calendar system such as. Instant - It represents an instantaneous point on the time-line like timestamp. 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.
