Java Date and Time
Java date and time handling means representing calendar dates, clock times, time zones, and time differences in a program. Modern Java code uses the java.time package for this work.
The older Date and Calendar classes still exist, but java.time is clearer, safer, and preferred for new code.
Main Date and Time Classes
The java.time package has different classes for different kinds of time data. Choose the class that matches what you need to store.
| Class | Use it for |
|---|---|
LocalDate |
A date without a time, such as 2026-07-17 |
LocalTime |
A time without a date, such as 09:30 |
LocalDateTime |
A date and time without a time zone |
ZonedDateTime |
A date and time in a specific time zone |
Instant |
A moment on the global timeline |
Example: Creating Date and Time Values
This program creates fixed date and time values, combines them, and reads individual parts from the result.
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2026, 7, 17);
LocalTime time = LocalTime.of(9, 30);
LocalDateTime lessonStarts = LocalDateTime.of(date, time);
System.out.println("Date: " + date);
System.out.println("Time: " + time);
System.out.println("Starts: " + lessonStarts);
System.out.println("Year: " + lessonStarts.getYear());
}
}
Output:
Date: 2026-07-17
Time: 09:30
Starts: 2026-07-17T09:30
Year: 2026
LocalDate.of creates a calendar date, and LocalTime.of creates a clock time. LocalDateTime.of combines them into one value. These classes are immutable, so methods that change a value return a new object instead of modifying the original.
Formatting Dates
The default printed form is useful for computers, but applications often need a friendlier format. Use DateTimeFormatter to format a date or parse text into a date.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2026, 7, 17);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM d, yyyy", Locale.ENGLISH);
String formatted = date.format(formatter);
LocalDate parsed = LocalDate.parse("2026-12-25");
System.out.println(formatted);
System.out.println(parsed.getDayOfWeek());
}
}
Output:
Jul 17, 2026
FRIDAY
The pattern MMM d, yyyy prints an abbreviated month name, day of month, and four-digit year. LocalDate.parse can read the standard ISO format yyyy-MM-dd without a custom formatter.
Adding and Measuring Time
Date and time objects have methods such as plusDays, minusWeeks, and plusHours. Use Period for date-based differences and Duration for time-based differences.
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Period;
public class Main {
public static void main(String[] args) {
LocalDate startDate = LocalDate.of(2026, 7, 17);
LocalDate dueDate = startDate.plusWeeks(2).plusDays(3);
Period wait = Period.between(startDate, dueDate);
LocalTime startTime = LocalTime.of(9, 15);
LocalTime endTime = LocalTime.of(11, 45);
Duration length = Duration.between(startTime, endTime);
System.out.println("Due date: " + dueDate);
System.out.println("Days between: " + wait.getDays());
System.out.println("Minutes: " + length.toMinutes());
}
}
Output:
Due date: 2026-08-03
Days between: 17
Minutes: 150
Period.between measures calendar-based time. In this example the dates are in the same month-to-month range, so getDays() returns 17. Duration.between measures exact time between two times, and toMinutes() converts that duration to minutes.
Working With Time Zones
A LocalDateTime has no time zone. If the value represents a real event for people in different places, attach a zone with ZonedDateTime.
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
LocalDateTime meeting = LocalDateTime.of(2026, 7, 17, 14, 0);
ZonedDateTime newYork = meeting.atZone(ZoneId.of("America/New_York"));
ZonedDateTime london = newYork.withZoneSameInstant(ZoneId.of("Europe/London"));
System.out.println("New York: " + newYork.toLocalTime());
System.out.println("London: " + london.toLocalTime());
}
}
Output:
New York: 14:00
London: 19:00
withZoneSameInstant keeps the same real moment and shows what the local clock time is in another zone.
Common Tips
- Use
LocalDatefor birthdays, due dates, and calendar-only values. - Use
ZonedDateTimeorInstantfor real moments that must work across time zones. - Remember that
java.timeobjects are immutable. - Avoid mixing old
DateandCalendarcode with newjava.timecode unless you must support legacy APIs.
Takeaway: use the specific java.time class that matches your data, then format, calculate, or convert zones from that clear starting point.
