Shiklo Programming
EN
  • English
  • Español
  • Português
  • Deutsch
  • Français
  • 日本語
  • हिन्दी

Java Date and Time

Dates and times are surprisingly tricky: months have different lengths, leap years add a day, and the time depends on where in the world you are. Java's java.time package handles all of this for you. Its classes are easy to read and, like strings, they never change: every calculation returns a new value.

Today and now

LocalDate is a date, LocalTime a time of day, and LocalDateTime both, all without a time zone. now() gives the current value, which changes on every run, so the first example only prints whether it worked:

import java.time.LocalDate;
import java.time.LocalTime;

public class Main {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        LocalTime time = LocalTime.now();
        System.out.println(today.getYear() >= 2025);
        System.out.println(time.getHour() < 24);
    }
}
Output
true
true

In your own programs, simply print today to see the date, for example 2025-08-15.

Creating and reading dates

of creates a specific date or time, and getter methods read its parts:

import java.time.LocalDate;
import java.time.LocalDateTime;

public class Main {
    public static void main(String[] args) {
        LocalDate launch = LocalDate.of(2023, 8, 23);
        System.out.println(launch);
        System.out.println(launch.getDayOfWeek());
        System.out.println(launch.getMonth());
        System.out.println(launch.getDayOfYear());
        System.out.println(launch.isLeapYear());
        LocalDateTime meeting = LocalDateTime.of(2025, 3, 14, 15, 30);
        System.out.println(meeting);
    }
}
Output
2023-08-23
WEDNESDAY
AUGUST
235
false
2025-03-14T15:30

Calculating with dates

plusDays, minusMonths and similar methods return new dates. Period measures the difference in years, months and days, and ChronoUnit counts it in one unit:

import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;

public class Main {
    public static void main(String[] args) {
        LocalDate start = LocalDate.of(2025, 1, 31);
        System.out.println(start.plusDays(30));
        System.out.println(start.plusMonths(1));
        LocalDate birthday = LocalDate.of(2000, 5, 20);
        LocalDate on = LocalDate.of(2025, 8, 15);
        Period age = Period.between(birthday, on);
        System.out.println(age.getYears() + " years, " + age.getMonths() + " months, " + age.getDays() + " days");
        System.out.println(ChronoUnit.DAYS.between(birthday, on) + " days");
        System.out.println(on.isAfter(birthday));
    }
}
Output
2025-03-02
2025-02-28
25 years, 2 months, 26 days
9218 days
true

Notice that one month after 31 January is 28 February: Java chooses the last valid day.

Formatting and parsing

A DateTimeFormatter turns dates into text in any pattern and reads text back. dd is the day, MM the month as a number, MMM its short name, yyyy the year, HH:mm the time:

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter indian = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        LocalDate d = LocalDate.parse("26/01/2025", indian);
        System.out.println(d);
        System.out.println(d.format(DateTimeFormatter.ofPattern("d MMM yyyy")));
        LocalDateTime t = LocalDateTime.of(2025, 1, 26, 9, 5);
        System.out.println(t.format(DateTimeFormatter.ofPattern("EEEE, HH:mm")));
    }
}
Output
2025-01-26
26 Jan 2025
Sunday, 09:05

Durations and time zones

Duration measures time in hours, minutes and seconds. ZonedDateTime attaches a time zone, so the same moment can be shown in different places:

import java.time.Duration;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        Duration film = Duration.between(LocalTime.of(18, 45), LocalTime.of(21, 20));
        System.out.println(film.toMinutes() + " minutes");
        System.out.println(film.toHours() + "h " + film.toMinutesPart() + "m");
        ZonedDateTime delhi = ZonedDateTime.of(2025, 6, 1, 9, 0, 0, 0, ZoneId.of("Asia/Kolkata"));
        ZonedDateTime london = delhi.withZoneSameInstant(ZoneId.of("Europe/London"));
        System.out.println(london.toLocalTime());
    }
}
Output
155 minutes
2h 35m
04:30

At 9:00 in Delhi in June it is 4:30 in London, because of the time difference and British summer time.

Practice exercises

  1. Print the day of the week of your birthday this year.
  2. Print how many days are left from 15 August 2025 until 31 December 2025.
  3. Read a date typed as "dd-MM-yyyy" and print it as "dd MMMM yyyy".