Using Java 8 java.time API: LocalDate, LocalTime, and LocalDateTime Examples
This article explains the shortcomings of java.util.Date, introduces the immutable java.time classes LocalDate, LocalTime, and LocalDateTime introduced in Java 8, and provides detailed code examples for obtaining, comparing, adding/subtracting, and converting dates and times.
Before Java 8, developers often used java.util.Date, which has many drawbacks such as mutability, lack of thread safety, inconsistent definitions, and no time‑zone or internationalization support.
Java 8 introduced the java.time package, providing immutable classes like LocalDate, LocalTime, and LocalDateTime for handling dates and times without time‑zone complications.
LocalDate
LocalDate represents a date without a time component. The default format is yyyy‑MM‑dd.
Getting the current date and its year, month, and day:
// Get current date and its components
LocalDate localDate = LocalDate.now();
int year = localDate.getYear();
int month = localDate.getMonthValue();
int day = localDate.getDayOfMonth();
System.out.println("Current date: " + localDate);
System.out.println("Year: " + year + " Month: " + month + " Day: " + day);Creating a specific date:
// Get a specific date
LocalDate specifiedDay = LocalDate.of(2008, 8, 18);
System.out.println("Specified date: " + specifiedDay);Comparing two dates for equality or order:
LocalDate localDate = LocalDate.now();
LocalDate otherDate = LocalDate.of(2018, 11, 11);
if (localDate.equals(otherDate)) {
System.out.println("localDate equals otherDate!");
} else {
if (localDate.isAfter(otherDate)) {
System.out.println("localDate is after otherDate!");
}
if (localDate.isBefore(otherDate)) {
System.out.println("localDate is before otherDate!");
}
}Adding and subtracting years, months, weeks, or days:
LocalDate localDate = LocalDate.now();
System.out.println("Date after 2 years: " + localDate.plusYears(2));
System.out.println("Date after 6 months: " + localDate.plusMonths(6));
System.out.println("Date after 3 weeks: " + localDate.plusWeeks(3));
System.out.println("Date after 15 days: " + localDate.plusDays(15));
System.out.println("Date 2 years ago: " + localDate.minusYears(2));
System.out.println("Date 6 months ago: " + localDate.minusMonths(6));
System.out.println("Date 3 weeks ago: " + localDate.minusWeeks(3));
System.out.println("Date 15 days ago: " + localDate.minusDays(15));Calculating the number of days between two dates:
// Get the 100th day of 2018
LocalDate specialDay = LocalDate.ofYearDay(2018, 100);
System.out.println("100th day of 2018: " + specialDay);
// Interval in days
long intervalDay = localDate.toEpochDay() - specialDay.toEpochDay();
System.out.println("Interval days: " + intervalDay);LocalTime
LocalTime represents a time without a date component. The default format is HH:mm:ss.SSS.
Getting the current time and its hour, minute, second, and creating a custom time:
// Current time
LocalTime localTime = LocalTime.now();
int hour = localTime.getHour();
int minute = localTime.getMinute();
int second = localTime.getSecond();
System.out.println("Current time: " + localTime);
System.out.println("Hour: " + hour + " Minute: " + minute + " Second: " + second);
// Custom time
LocalTime specifiedTime = LocalTime.of(15, 30, 45);
System.out.println("Custom time: " + specifiedTime);Comparing two times:
if (localTime.equals(specifiedTime)) {
System.out.println("localTime equals specifiedTime!");
} else {
if (localTime.isAfter(specifiedTime)) {
System.out.println("localTime is after specifiedTime!");
}
if (localTime.isBefore(specifiedTime)) {
System.out.println("localTime is before specifiedTime!");
}
}Adding and subtracting hours, minutes, and seconds:
System.out.println("Current time: " + localTime);
System.out.println("After 2 hours: " + localTime.plusHours(2));
System.out.println("After 30 minutes: " + localTime.plusMinutes(30));
System.out.println("After 500 seconds: " + localTime.plusSeconds(500));
System.out.println("2 hours ago: " + localTime.minusHours(2));
System.out.println("30 minutes ago: " + localTime.minusMinutes(30));
System.out.println("500 seconds ago: " + localTime.minusSeconds(500));LocalDateTime
LocalDateTime combines date and time in an immutable object. The default format is yyyy‑MM‑ddTHH:mm:ss.SSS.
Getting the current date‑time and creating a custom one from LocalDate and LocalTime:
// Current date‑time
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("Current date‑time: " + localDateTime);
// Custom date‑time
LocalDateTime specifiedDateTime = LocalDateTime.of(LocalDate.now(), LocalTime.now());
System.out.println("Custom date‑time: " + specifiedDateTime);Converting a LocalDateTime to separate LocalDate and LocalTime objects:
LocalDate datePart = localDateTime.toLocalDate();
LocalTime timePart = localDateTime.toLocalTime();
System.out.println("Current date: " + datePart);
System.out.println("Current time: " + timePart);Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.