Java provides classes to work with dates, times, and date-time
calculations. Modern Java applications commonly use the
java.time package, introduced in Java 8.
Java provides several classes for working with dates and times.
| Class | Purpose |
|---|---|
LocalDate |
Stores a date without a time. |
LocalTime |
Stores a time without a date. |
LocalDateTime |
Stores a date and time without a time zone. |
ZonedDateTime |
Stores a date and time with a time zone. |
Period |
Represents a period in years, months, and days. |
Duration |
Represents a time-based amount. |
The LocalDate class represents a date
containing the year, month, and day.
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
System.out.println("Today's Date: " + today);
}
}
Today's Date: 2026-10-17
Use LocalDate.of() to create a date
with a specific year, month, and day.
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate birthday = LocalDate.of(2000, 5, 15);
System.out.println("Birthday: " + birthday);
}
}
Birthday: 2000-05-15
The arguments represent the year, month number, and day of the month.
You can get the year, month, and day from a
LocalDate object.
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2026, 10, 17);
System.out.println("Year: " + date.getYear());
System.out.println("Month: " + date.getMonth());
System.out.println("Month Number: " + date.getMonthValue());
System.out.println("Day: " + date.getDayOfMonth());
System.out.println("Day of Week: " + date.getDayOfWeek());
}
}
Year: 2026
Month: OCTOBER
Month Number: 10
Day: 17
Day of Week: SATURDAY
The LocalTime class represents a time
without a date or time zone.
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
LocalTime time = LocalTime.now();
System.out.println("Current Time: " + time);
}
}
Current Time: 11:30:45.123
The displayed time and precision vary when the program runs.
Use LocalTime.of() to create a specific time.
The hour uses the 24-hour format.
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
LocalTime classTime = LocalTime.of(9, 30);
System.out.println("Class Time: " + classTime);
}
}
Class Time: 09:30
You can also provide seconds:
LocalTime.of(9, 30, 15).
LocalDateTime combines a date and a time.
It does not store a time zone.
import java.time.LocalDateTime;
public class Main {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println("Current Date and Time: " + now);
}
}
Current Date and Time: 2026-10-17T11:30:45.123
The letter T separates the date from the time.
The output above is an example.
Methods such as plusDays(),
minusDays(), plusMonths(),
and plusYears() help perform date calculations.
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2026, 10, 17);
System.out.println(date.plusDays(5));
System.out.println(date.minusDays(5));
System.out.println(date.plusMonths(2));
System.out.println(date.plusYears(1));
}
}
2026-10-22
2026-10-12
2026-12-17
2027-10-17
Use isBefore(), isAfter(),
and equals() to compare dates.
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate date1 = LocalDate.of(2026, 1, 10);
LocalDate date2 = LocalDate.of(2026, 5, 10);
System.out.println(date1.isBefore(date2));
System.out.println(date1.isAfter(date2));
System.out.println(date1.equals(date2));
}
}
true
false
false
The DateTimeFormatter class formats dates
into readable strings.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2026, 10, 17);
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("dd-MM-yyyy");
System.out.println(date.format(formatter));
}
}
17-10-2026
Some common format patterns:
| Pattern | Example |
|---|---|
dd-MM-yyyy |
17-10-2026 |
dd/MM/yyyy |
17/10/2026 |
yyyy-MM-dd |
2026-10-17 |
EEEE |
Saturday |
MMMM |
October |
MM means month,
while mm means minute.
The Period class calculates the difference
between two dates in years, months, and days.
import java.time.LocalDate;
import java.time.Period;
public class Main {
public static void main(String[] args) {
LocalDate start = LocalDate.of(2020, 1, 1);
LocalDate end = LocalDate.of(2023, 4, 6);
Period period = Period.between(start, end);
System.out.println("Years: " + period.getYears());
System.out.println("Months: " + period.getMonths());
System.out.println("Days: " + period.getDays());
}
}
Years: 3
Months: 3
Days: 5
The period represents the date-based difference between the two dates.
java.time API for modern Java applications.LocalDate for dates without times.LocalTime for times without dates.LocalDateTime when both date and time are needed but no time zone is required.ZonedDateTime when a time zone matters.DateTimeFormatter for formatting and parsing.LocalDate.now().LocalDate.of().LocalTime.now().LocalDateTime.isBefore() and isAfter().dd/MM/yyyy.Period.between().Select the correct answer for each question.