Week 37 — What is the `java.time` API and how can it be used?

Question of the Week #37
What is the java.time API and how can it be used?
3 Replies
Eric McIntyre
Eric McIntyre15mo ago
The java.time API was introduced in Java 8. It offers classes like LocalDate, LocalTime, and ZonedDateTime for precise date and time representation. With features like formatting, parsing, and duration calculations, it improves accuracy and simplifies complex operations.
Submission from m4ziogra
Eric McIntyre
Eric McIntyre15mo ago
The package java.time contains useful classes for dealing with dates, times, timezones etc. Classes in this package are immutable meaning that their instances cannot be modified upon creation. However, it is possible to create slightly different instances of these immutable classes. One of the most important classes is LocalDateTune which represents a specific date and time. Similarly, the classes LocalDate and LocalTime represent a date without a time and a time without a date respectively. These classes also allow to do date/time calculations.
LocalDateTime currentDateTime = LocalDateTime.now();
LocalDate currentDate = currentDateTime.toLocalDate();//date only, doesn't contain time
LocalTime currentTime = currentDateTime.toLocalTime();//time only, doesn't contain any date
LocalDateTime reconstructed = LocalDateTime.of(currentDate, currentTime);

LocalDateTime startOfNextDay = currentDate.plusDays(1).atStartOfDay();//when the next day starts
LocalDateTime inOneHour = currentDateTime.plusHours(1);//one hour after the current time

LocalDate someGivenDate = LocalDate.of(1996, 1, 23);//January 23rd, 1996
LocalDateTime currentDateTime = LocalDateTime.now();
LocalDate currentDate = currentDateTime.toLocalDate();//date only, doesn't contain time
LocalTime currentTime = currentDateTime.toLocalTime();//time only, doesn't contain any date
LocalDateTime reconstructed = LocalDateTime.of(currentDate, currentTime);

LocalDateTime startOfNextDay = currentDate.plusDays(1).atStartOfDay();//when the next day starts
LocalDateTime inOneHour = currentDateTime.plusHours(1);//one hour after the current time

LocalDate someGivenDate = LocalDate.of(1996, 1, 23);//January 23rd, 1996
Another import class is Instant. This class represents a specific point in time without any additional information.
Instant currentInstant = Instant.now();
Instant shortlyAfterUNIXEpoch = Instant.ofEpochMilli(1337);//1.337 seconds after the UNIX Epoch
LocalDateTime instantAsLocalDateTime = LocalDateTime.ofInstant(shortlyAfterUNIXEpoch, ZoneId.systemDefault());//it is possible to convert an instant to a LocalDateTime as well
LocalDateTime instantAsLocalDateTime = LocalDateTime.ofInstant(shortlyAfterUNIXEpoch, ZoneOffset.UTC);//use UTC instead, this should result in exactly 1970-01-01T00:00:01.337
Instant currentInstant = Instant.now();
Instant shortlyAfterUNIXEpoch = Instant.ofEpochMilli(1337);//1.337 seconds after the UNIX Epoch
LocalDateTime instantAsLocalDateTime = LocalDateTime.ofInstant(shortlyAfterUNIXEpoch, ZoneId.systemDefault());//it is possible to convert an instant to a LocalDateTime as well
LocalDateTime instantAsLocalDateTime = LocalDateTime.ofInstant(shortlyAfterUNIXEpoch, ZoneOffset.UTC);//use UTC instead, this should result in exactly 1970-01-01T00:00:01.337
If a datetime with a timezone is required, it is possible to use ZonedDateTime.
LocalDateTime currentDateTime = LocalDateTime.now();
ZonedDateTime currentDateTimeWithTimezone = currentDateTime.atZone(ZoneId.systemDefault());//current date in the default timezone
ZonedDateTime currentDateTimeWithOtherTimezone = currentDateTimeWithTimezone.withZoneSameInstant(ZoneId.of("UTC+12"));//current date in a different timezone (UTC+12)
LocalDateTime currentDateTime = LocalDateTime.now();
ZonedDateTime currentDateTimeWithTimezone = currentDateTime.atZone(ZoneId.systemDefault());//current date in the default timezone
ZonedDateTime currentDateTimeWithOtherTimezone = currentDateTimeWithTimezone.withZoneSameInstant(ZoneId.of("UTC+12"));//current date in a different timezone (UTC+12)
Eric McIntyre
Eric McIntyre15mo ago
For calculating with durations, the class Duration can be used. It represents a specific amount of time as opposed to a timestamp. For example, a Duration could be 13 hours and 37 minutes.
Duration someDuration = Duration.ofHours(13).plusMinutes(37);//13 hours and 37 minutes
LocalDateTime currentDateTimeWithThatDurationAdded = LocalDateTime.now().plus(someDuration);//add the duration to the current datetime obtaining a new datetime
Duration timespanOf2022 = Duration.between(LocalDateTime.of(2022,1,1,0,0),LocalDateTime.of(2023,1,1,0,0));//a duration with the time between the start and end of 2022
Duration someDuration = Duration.ofHours(13).plusMinutes(37);//13 hours and 37 minutes
LocalDateTime currentDateTimeWithThatDurationAdded = LocalDateTime.now().plus(someDuration);//add the duration to the current datetime obtaining a new datetime
Duration timespanOf2022 = Duration.between(LocalDateTime.of(2022,1,1,0,0),LocalDateTime.of(2023,1,1,0,0));//a duration with the time between the start and end of 2022
⭐ Submission from dan1st
Want results from more Discord servers?
Add your server