New Date Time API in Java
The new date-time API was introduced with Java 8 to avoid the main drawbacks of the old date-time API. Following are the main drawbacks that have been fixed in the new date-time API.
=> Thread Safety: The old date-time API is not thread-safe, and the new date-time API is thread-safe. Also, the new date-time API is immutable and doesn’t have setter methods. So it helps to fix the concurrent issues.
=> Enchased Operations: The old API contains only a few date-time-related operations, while the new API provides large-scale operations in date-time.
Now let’s understand the few basic components or classes in the new date-time API, which is included in package java.time.
01. Local date-time: simple date time, which does not have to handle the time zones. In this component, we can mainly use the LocalDate, LocalTime, and LocalDateTime. Let’s discuss this type of date-time component with the following example.
/**
* Main class for the tutorial.
*
* @author Sachith Ariyathilaka
* @version 1.0.0
* @since 2024/05/25
*/
public class JavaDateTimeAPITutorial {
public static void main(String[] args) {
localDateTime();
}
/**
* Local date time.
*/
private static void localDateTime() {
// local date
LocalDate localDate = LocalDate.now();
Month month = localDate.getMonth();
int date = localDate.getDayOfMonth();
print("Local date is: " + localDate);
print("Current month is: " + month);
print("Current date is: " + date);
// local time
LocalTime localTime = LocalTime.now();
int hour = localTime.getHour();
int minute = localTime.getMinute();
int second = localTime.getSecond();
print("Local time is: " + localTime);
print("Current hour is: " + hour);
print("Current minute is: " + minute);
print("Current second is: " + second);
// local date time
LocalDateTime localDateTime = LocalDateTime.now();
print("Local date time is: " + localDateTime);
}
/**
* Print the output string.
*
* @param output the output
*/
private static void print(String output) {
System.out.println(output);
}
}In here, we can see that the current date, time, date-time, month, day, hour, minute, and second can be easily fetched using the given predefined methods.
02. Zoned date-time: This is used when the time zone needs to be considered in the date time. In here, we’ll use the ZonedDateTime. Let’s discuss this type of date-time component with the following example.
/**
* Main class for the tutorial.
*
* @author Sachith Ariyathilaka
* @version 1.0.0
* @since 2024/05/25
*/
public class JavaDateTimeAPITutorial {
public static void main(String[] args) {
zonedDateTime();
}
/**
* Zoned date time.
*/
private static void zonedDateTime() {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZoneId timeZone = zonedDateTime.getZone();
Month month = zonedDateTime.getMonth();
int date = zonedDateTime.getDayOfMonth();
int hour = zonedDateTime.getHour();
int minute = zonedDateTime.getMinute();
int second = zonedDateTime.getSecond();
print("Zoned date time is: " + zonedDateTime);
print("Time Zone is: " + timeZone);
print("Current month is: " + month);
print("Current date is: " + date);
print("Current hour is: " + hour);
print("Current minute is: " + minute);
print("Current second is: " + second);
}
/**
* Print the output string.
*
* @param output the output
*/
private static void print(String output) {
System.out.println(output);
}
}In here, we can see that, just like the local date time, we can easily fetch the current date, time, date-time, month, day, hour, minute, and second using the given predefined methods.
Get Sachith Ariyathilaka’s stories in your inbox
Join Medium for free to get updates from this writer.
03. Period: This is used to maintain the operations based on the date values. Let’s discuss this component by using the following example.
/**
* Main class for the tutorial.
*
* @author Sachith Ariyathilaka
* @version 1.0.0
* @since 2024/05/25
*/
public class JavaDateTimeAPITutorial {
public static void main(String[] args) {
period();
}
/**
* Period.
*/
private static void period() {
LocalDate currentDate = LocalDate.now();
LocalDate independentDate = LocalDate.of(1948, Month.FEBRUARY, 4);
Period gap = Period.between(independentDate, currentDate);
print("Independent date is: " + independentDate);
print("Current date is: " + currentDate);
print("Gap between independent date and current date is: " + gap.getYears() + " Years " + gap.getMonths() + " Months " + gap.getDays() + " Days");
}
/**
* Print the output string.
*
* @param output the output
*/
private static void print(String output) {
System.out.println(output);
}
}Here, we can see that we can manipulate the dates by using the period component. It provides the days, months, and years units as primitive time units. It is easy to do some operations related to the date.
04. Duration: This is used to maintain the operations based on the time values. Let’s discuss this component by using the following example.
/**
* Main class for the tutorial.
*
* @author Sachith Ariyathilaka
* @version 1.0.0
* @since 2024/05/25
*/
public class JavaDateTimeAPITutorial {
public static void main(String[] args) {
duration();
}
/**
* Duration.
*/
private static void duration() {
LocalTime currentTime = LocalTime.now();
LocalTime midNight = LocalTime.of(0, 0, 0);
Duration duration = Duration.between(midNight, currentTime);
long seconds = duration.getSeconds() % 60;
long minutes = (duration.getSeconds() / 60) % 60;
long hours = duration.getSeconds() / 3600;
print("mid night time is: " + midNight);
print("Current time is: " + currentTime);
print("Duration between mid night and current time is: " + hours + " Hours " + minutes + " Minutes " + seconds + " Seconds");
}
/**
* Print the output string.
*
* @param output the output
*/
private static void print(String output) {
System.out.println(output);
}
}Here, we can see that we can manipulate the times by using the duration component. But in here, it only provides seconds and nanoseconds as the primitive units.
05. Chrono Units Enum: This enum is used to represent time units such as seconds, days, months, years, etc. Let’s discuss this component by using the following example.
/**
* Main class for the tutorial.
*
* @author Sachith Ariyathilaka
* @version 1.0.0
* @since 2024/05/25
*/
public class JavaDateTimeAPITutorial {
public static void main(String[] args) {
chronosUnits();
}
/**
* Chrono units.
*/
private static void chronosUnits() {
LocalDate currentDate = LocalDate.now();
LocalDate futureDate = currentDate.plus(1, ChronoUnit.YEARS).plus(2, ChronoUnit.MONTHS).plus(5, ChronoUnit.DAYS);
print("Current date is: " + currentDate);
print("Future date is: " + futureDate);
LocalTime currentTime = LocalTime.now();
LocalTime futureTime = currentTime.plus(2, ChronoUnit.HOURS).plus(20, ChronoUnit.MINUTES).plus(10, ChronoUnit.SECONDS);
print("Current time is: " + currentTime);
print("Future time is: " + futureTime);
}
/**
* Print the output string.
*
* @param output the output
*/
private static void print(String output) {
System.out.println(output);
}
}In this example, we can observe that it is easy to perform operations on dates and times by using these Enum values.
06. Temporal Adjusters: This component is used to do various date-related operations. The following example helps you understand a few of the date-related operations.
/**
* Main class for the tutorial.
*
* @author Sachith Ariyathilaka
* @version 1.0.0
* @since 2024/05/25
*/
public class JavaDateTimeAPITutorial {
public static void main(String[] args) {
temporalAdjusters();
}
/**
* Temporal adjusters.
*/
private static void temporalAdjusters() {
LocalDate currentDate = LocalDate.now();
LocalDate firstDayOfNextMonth = currentDate.with(TemporalAdjusters.firstDayOfNextMonth());
LocalDate nextFriday = currentDate.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
LocalDate lastDayOfYear = currentDate.with(TemporalAdjusters.lastDayOfYear());
LocalDate lastSundayOfMonth = currentDate.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY));
print("Current date is: " + currentDate);
print("First date of next month is: " + firstDayOfNextMonth);
print("Next friday is: " + nextFriday);
print("Last day of the year is: " + lastDayOfYear);
print("Last sunday of the month is: " + lastSundayOfMonth);
}
/**
* Print the output string.
*
* @param output the output
*/
private static void print(String output) {
System.out.println(output);
}
}Here we have discussed a few operations in the temporal adjusters. As per those examples, we can observe that we can do date-related operations very easily using these components. The following GitHub repository will help you find the source code for this story.











