Working with dates and times is something you'll commonly need in real Java programs. For example, you may want to display today's date, get the current time, calculate the difference between two dates, or format a date in a particular way.
Java provides several classes for working with date and time. In modern Java programs, the java.time package is generally the preferred choice.
Getting the Current Date
We can use LocalDate to represent a date without a time.
First, import it:
import java.time.LocalDate;Then:
LocalDate today = LocalDate.now();
System.out.println(today);The output will look something like:
2026-08-22The exact date depends on when the program is executed.
LocalDate stores information such as:
Year
Month
Daybut it does not contain a time.
Getting the Current Time
For a time without a date, we can use LocalTime.
import java.time.LocalTime;
LocalTime time = LocalTime.now();
System.out.println(time);The output might look like:
10:30:45.123456789The exact value will depend on the current time.
LocalTime stores information such as hours, minutes, seconds, and nanoseconds.
Getting the Current Date and Time
If we need both the date and time, we can use LocalDateTime.
import java.time.LocalDateTime;
LocalDateTime now = LocalDateTime.now();
System.out.println(now);Output might look like:
2026-08-22T10:30:45.123Here, T separates the date from the time.
Creating a Specific Date
We don't always need the current date. We can create a specific date using of().
import java.time.LocalDate;
LocalDate birthday = LocalDate.of(2000, 5, 15);
System.out.println(birthday);Output:
2000-05-15The values represent:
Year → 2000
Month → 5
Day → 15Getting Individual Parts of a Date
We can get the year, month, and day separately.
LocalDate date = LocalDate.of(2026, 8, 22);
System.out.println(date.getYear());
System.out.println(date.getMonth());
System.out.println(date.getDayOfMonth());Output:
2026
AUGUST
22We can also get the month as a number:
System.out.println(date.getMonthValue());Output:
8Adding and Subtracting Time
Java makes it easy to calculate new dates.
For example:
LocalDate today = LocalDate.now();
LocalDate nextWeek = today.plusDays(7);
System.out.println(nextWeek);We can also add months or years:
LocalDate date = LocalDate.now();
System.out.println(date.plusMonths(2));
System.out.println(date.plusYears(1));Similarly, we can subtract time:
LocalDate previousWeek = today.minusDays(7);This is useful when working with things such as deadlines, appointments, subscriptions, and schedules.
Comparing Dates
We can compare dates using methods such as isBefore(), isAfter(), and isEqual().
For example:
LocalDate date1 = LocalDate.of(2026, 8, 20);
LocalDate date2 = LocalDate.of(2026, 8, 25);
System.out.println(date1.isBefore(date2));
System.out.println(date1.isAfter(date2));Output:
true
falseWe can also check whether two dates are equal:
System.out.println(date1.isEqual(date2));Formatting Dates
By default, Java displays dates in a format like:
2026-08-22Sometimes we want a different format, such as:
22/08/2026For this, we can use DateTimeFormatter.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
LocalDate date = LocalDate.now();
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("dd/MM/yyyy");
String formattedDate = date.format(formatter);
System.out.println(formattedDate);Output:
22/08/2026Here:
dd → day
MM → month
yyyy → yearWe can create other formats as well.
For example:
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("dd MMM yyyy");This could produce:
22 Aug 2026Calculating the Difference Between Dates
Java also provides Period for calculating the difference between two dates.
For example:
import java.time.LocalDate;
import java.time.Period;
LocalDate start = LocalDate.of(2020, 1, 1);
LocalDate end = LocalDate.of(2026, 1, 1);
Period difference = Period.between(start, end);
System.out.println(difference.getYears());Output:
6This is useful for things such as calculating someone's age or the period between two dates.
Date and Time Classes
The java.time package contains several useful classes.
The most important ones to know initially are:
Class | Used For |
|---|---|
| Date only |
| Time only |
| Date and time |
| Date and time with a time zone |
| Difference between dates |
| Difference between times |
| Formatting dates and times |
For example, if you're building an application for users in different countries, ZonedDateTime becomes useful because the time zone matters.
A Complete Example
Let's create a simple program that displays today's date and calculates a date one week from now:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
class Main {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("dd MMM yyyy");
System.out.println("Today: " + today.format(formatter));
LocalDate nextWeek = today.plusDays(7);
System.out.println(
"Next week: " + nextWeek.format(formatter)
);
}
}The output will depend on the date when you run the program, but it will look something like:
Today: 22 Aug 2026
Next week: 29 Aug 2026The main thing to remember is that modern Java provides the java.time package for working with dates and times. The classes you'll use most often at the beginning are LocalDate, LocalTime, LocalDateTime, and DateTimeFormatter.