Date & Time
Java has two APIs for dates and times: java.util.Date/Calendar (legacy, avoid) and java.time (modern, recommended since Java 8). The java.time API was designed based on Joda-Time and fixes all the shortcomings of the old API — it is immutable, thread-safe, and has intuitive methods.
Key classes in java.time: LocalDate (date only), LocalTime (time only), LocalDateTime (date + time), ZonedDateTime (date + time + time zone), Instant (timestamp), and Duration/Period (time intervals). All of these classes are immutable — methods such as plusDays() return a new object.
DateTimeFormatter is used to format a date into a string, and LocalDate.parse() parses a string into a date. Format patterns use specific letters: yyyy (year), MM (month), dd (day), HH (hour), mm (minute), ss (second).
Date-Time Classes:
| Class | Description | Example |
|---|---|---|
LocalDate |
Date only | 2026-07-13 |
LocalTime |
Time only | 14:30:00 |
LocalDateTime |
Date + time | 2026-07-13T14:30:00 |
ZonedDateTime |
+ Time zone | -05:00[Chicago] |
Instant |
UTC timestamp | 2026-07-13T07:30:00Z |
Period |
Interval (years/months/days) | P1Y2M3D |
Duration |
Interval (hours/minutes/seconds) | PT2H30M |
Code Example
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
public class DateTime {
public static void main(String[] args) {
// Current date and time
LocalDate today = LocalDate.now();
LocalTime now = LocalTime.now();
LocalDateTime datetime = LocalDateTime.now();
System.out.println("Date: " + today);
System.out.println("Time: " + now);
System.out.println("DateTime: " + datetime);
// Create a specific date
LocalDate birthday = LocalDate.of(2000, 6, 15);
System.out.println("Birthday: " + birthday);
// Date operations
LocalDate tomorrow = today.plusDays(1);
LocalDate mingguDepan = today.plusWeeks(1);
long daysUntilBirthday = today.until(birthday, ChronoUnit.DAYS);
System.out.println("Tomorrow: " + tomorrow);
System.out.println("Days until birthday: " + Math.abs(daysUntilBirthday));
// Format
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd MMMM yyyy");
System.out.println("Formatted: " + today.format(fmt));
// Parsing
LocalDate parsed = LocalDate.parse("2026-12-25");
System.out.println("Parsed: " + parsed);
// Period
Period age = Period.between(birthday, today);
System.out.println("Age: " + age.getYears() + " years, "
+ age.getMonths() + " months");
}
}
Code Explanation
LocalDate.now()gets the current system date.LocalTime.now()andLocalDateTime.now()get the time and the combination of both.LocalDate.of(year, month, day)creates a specific date. Months use numbers (1-12), not names.plusDays(),plusWeeks(),plusMonths()return a new LocalDate (immutable).until()computes the interval between two dates in a given unit.DateTimeFormatter.ofPattern()defines the output format.ddfor the day,MMMMfor the full month name,yyyyfor the year.Period.between()computes an interval in units of years, months, and days.
Everyday Example
A bill due date reminder program:
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class PengingatTagihan {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate dueDate = LocalDate.of(2026, 7, 20);
long daysRemaining = ChronoUnit.DAYS.between(today, dueDate);
System.out.println("Today: " + today);
System.out.println("Due date: " + dueDate);
if (daysRemaining < 0) {
System.out.println("BILL IS OVERDUE!");
} else if (daysRemaining <= 3) {
System.out.println("PAY NOW! Only " + daysRemaining + " days left");
} else {
System.out.println("You still have " + daysRemaining + " days");
}
}
}
Summary
The java.time API is the modern standard for dates and times in Java. Always use these classes rather than the outdated Date/Calendar. All java.time classes are immutable and thread-safe, so they are safe to use anywhere without worrying about concurrent modification.