Java Date and Time (java.time)
The modern date and time API is in the package java.time (Java 8+). Its objects are immutable and easy to use. Do not use the old java.util.Date and Calendar in new code.
The main classes
| Class | Keeps |
|---|---|
| LocalDate | A date, with no time: 2026-03-15 |
| LocalTime | A time, with no date: 14:30:00 |
| LocalDateTime | Date and time, no time zone |
| ZonedDateTime | Date, time and time zone |
| Instant | A moment on the time line (UTC) |
| Period | An amount of time in years, months, days |
| Duration | An amount of time in hours, minutes, seconds |
Create dates
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, 3, 15);
LocalTime time = LocalTime.of(14, 30);
LocalDateTime both = LocalDateTime.of(date, time);
System.out.println(date);
System.out.println(time);
System.out.println(both);
System.out.println(date.getDayOfWeek());
System.out.println(date.getMonth() + " " + date.getYear() + " " + date.getDayOfYear());
System.out.println(date.isLeapYear());
System.out.println(LocalDate.now().isAfter(date)); // now() is the real today
}
}
Output
2026-03-15
14:30
2026-03-15T14:30
SUNDAY
MARCH 2026 74
false
true
Change and compare dates
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class DateMath {
public static void main(String[] args) {
LocalDate d = LocalDate.of(2026, 1, 31);
System.out.println(d.plusDays(10));
System.out.println(d.plusMonths(1)); // Feb has no 31st: goes to the last day
System.out.println(d.minusYears(1));
System.out.println(d.withDayOfMonth(1));
System.out.println(d.isBefore(LocalDate.of(2026, 2, 1)));
LocalDate start = LocalDate.of(2026, 1, 1);
LocalDate end = LocalDate.of(2026, 12, 25);
System.out.println(ChronoUnit.DAYS.between(start, end));
}
}
Output
2026-02-10
2026-02-28
2025-01-31
2026-01-01
true
358
Period and Duration
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Period;
public class Span {
public static void main(String[] args) {
Period p = Period.between(LocalDate.of(2000, 5, 20), LocalDate.of(2026, 3, 15));
System.out.println(p.getYears() + " years, " + p.getMonths() + " months, " + p.getDays() + " days");
Duration d = Duration.between(LocalTime.of(9, 15), LocalTime.of(17, 45));
System.out.println(d);
System.out.println(d.toHours() + " h " + d.toMinutesPart() + " min");
}
}
Output
25 years, 9 months, 23 days
PT8H30M
8 h 30 min
Format and parse
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Fmt {
public static void main(String[] args) {
LocalDateTime dt = LocalDateTime.of(2026, 3, 5, 9, 7, 30);
DateTimeFormatter f = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
System.out.println(dt.format(f));
System.out.println(dt.format(DateTimeFormatter.ofPattern("EEEE, d MMMM yyyy", java.util.Locale.ENGLISH)));
System.out.println(dt.format(DateTimeFormatter.ISO_LOCAL_DATE));
LocalDate parsed = LocalDate.parse("15-08-2026", DateTimeFormatter.ofPattern("dd-MM-yyyy"));
System.out.println(parsed);
System.out.println(LocalDate.parse("2026-08-15").plusDays(1));
}
}
Output
05/03/2026 09:07:30
Thursday, 5 March 2026
2026-03-05
2026-08-15
2026-08-16
| Letter | Meaning |
|---|---|
| yyyy | Year (4 digits) |
| MM | Month number (01 to 12) |
| MMM, MMMM | Month name (short, long) |
| dd | Day of month |
| HH | Hour (00 to 23) |
| mm | Minute |
| ss | Second |
| EEEE | Day name |
Time zones
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.LocalDateTime;
public class Zone {
public static void main(String[] args) {
LocalDateTime local = LocalDateTime.of(2026, 3, 15, 12, 0);
ZonedDateTime india = local.atZone(ZoneId.of("Asia/Kolkata"));
ZonedDateTime london = india.withZoneSameInstant(ZoneId.of("Europe/London"));
ZonedDateTime newYork = india.withZoneSameInstant(ZoneId.of("America/New_York"));
System.out.println(india);
System.out.println(london);
System.out.println(newYork);
}
}
Output
2026-03-15T12:00+05:30[Asia/Kolkata]
2026-03-15T06:30Z[Europe/London]
2026-03-15T02:30-04:00[America/New_York]
A wrong date
import java.time.LocalDate;
public class Bad {
public static void main(String[] args) {
LocalDate d = LocalDate.of(2026, 2, 30);
}
}
Output
Exception in thread "main" java.time.DateTimeException: Invalid date 'FEBRUARY 30'
at java.base/java.time.LocalDate.create(LocalDate.java:461)
at java.base/java.time.LocalDate.of(LocalDate.java:277)
at Bad.main(Bad.java:5)
Note:
Keep times in UTC (Instant) in your database and show them in the user’s time zone only when you display them. Next: Java File Handling.
Congratulations! Chapter Finished. Learn more about the similar topics:
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |