Tutorial by Examples

Get the current date. LocalDate.now() Get yesterday's date. LocalDate y = LocalDate.now().minusDays(1); Get tomorrow's date LocalDate t = LocalDate.now().plusDays(1); Get a specific date. LocalDate t = LocalDate.of(1974, 6, 2, 8, 30, 0, 0); In addition to the plus and minus methods, ...
Date and time without time zone information LocalDateTime dateTime = LocalDateTime.of(2016, Month.JULY, 27, 8, 0); LocalDateTime now = LocalDateTime.now(); LocalDateTime parsed = LocalDateTime.parse("2016-07-27T07:00:00"); Date and time with time zone information ZoneId zoneId = Zon...
LocalDate tomorrow = LocalDate.now().plusDays(1); LocalDateTime anHourFromNow = LocalDateTime.now().plusHours(1); Long daysBetween = java.time.temporal.ChronoUnit.DAYS.between(LocalDate.now(), LocalDate.now().plusDays(3)); // 3 Duration duration = Duration.between(Instant.now(), ZonedDateTime.par...
Represents an instant in time. Can be thought of as a wrapper around a Unix timestamp. Instant now = Instant.now(); Instant epoch1 = Instant.ofEpochMilli(0); Instant epoch2 = Instant.parse("1970-01-01T00:00:00Z"); java.time.temporal.ChronoUnit.MICROS.between(epoch1, epoch2); // 0
Following example also have explanation required for understanding example within it. import java.time.Clock; import java.time.Duration; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneId; import java.time...
Before Java 8, there was DateFormat and SimpleDateFormat classes in the package java.text and this legacy code will be continued to be used for sometime. But, Java 8 offers a modern approach to handling Formatting and Parsing. In formatting and parsing first you pass a String object to DateTimeFor...
Use LocalDate and ChronoUnit: LocalDate d1 = LocalDate.of(2017, 5, 1); LocalDate d2 = LocalDate.of(2017, 5, 18); now, since the method between of the ChronoUnit enumerator takes 2 Temporals as parameters so you can pass without a problem the LocalDate instances long days = ChronoUnit.DAYS.betw...

Page 1 of 1