Java Date and Time
100%

Java Date and Time

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.

1. Introduction to Java Date and Time

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.
2. Using LocalDate

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);
    }
}
Sample Output:
Today's Date: 2026-10-17
The output depends on the date when the program runs.
3. Creating a Specific Date

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);
    }
}
Output:
Birthday: 2000-05-15

The arguments represent the year, month number, and day of the month.

4. Getting Date Information

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());
    }
}
Output:
Year: 2026
Month: OCTOBER
Month Number: 10
Day: 17
Day of Week: SATURDAY
5. Using LocalTime

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);
    }
}
Sample Output:
Current Time: 11:30:45.123

The displayed time and precision vary when the program runs.

6. Creating a Specific Time

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);
    }
}
Output:
Class Time: 09:30

You can also provide seconds: LocalTime.of(9, 30, 15).

7. Using LocalDateTime

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);
    }
}
Sample Output:
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.

8. Adding and Subtracting Dates

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));
    }
}
Output:
2026-10-22
2026-10-12
2026-12-17
2027-10-17
9. Comparing Dates

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));
    }
}
Output:
true
false
false
10. Formatting Dates with DateTimeFormatter

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));
    }
}
Output:
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
Uppercase and lowercase pattern letters have different meanings. For example, MM means month, while mm means minute.
11. Calculating the Period Between Dates

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());
    }
}
Output:
Years: 3
Months: 3
Days: 5

The period represents the date-based difference between the two dates.

12. Java Date and Time Best Practices
  • Prefer the java.time API for modern Java applications.
  • Use LocalDate for dates without times.
  • Use LocalTime for times without dates.
  • Use LocalDateTime when both date and time are needed but no time zone is required.
  • Use ZonedDateTime when a time zone matters.
  • Use DateTimeFormatter for formatting and parsing.
  • Remember that modern date-time objects are immutable; date calculations return new objects.
13. Java Date and Time Assignments
  1. Display today's date using LocalDate.now().
  2. Create your birthday using LocalDate.of().
  3. Display the current time using LocalTime.now().
  4. Create a specific time for a class or meeting.
  5. Display the current date and time using LocalDateTime.
  6. Add 10 days to a given date.
  7. Subtract 3 months from a given date.
  8. Compare two dates using isBefore() and isAfter().
  9. Format a date as dd/MM/yyyy.
  10. Calculate the period between two dates using Period.between().

Quick Quiz: Java Date and Time

Select the correct answer for each question.

1. Which class stores a date without a time?



2. Which method adds days to a LocalDate?



3. Which class is used to format date-time values?