Java Date and Time

Java offers a comprehensive API for handling dates and times.

Java provides a comprehensive API to handle date and time through the java.util.Date class, as well as the Java 8 Date and Time API (java.time package), which is more modern, precise, and flexible.

The Date class in Java was used to represent a particular point in time before Java 8, but it was ill-designed and unclear when handling date and time operations.

Working with date and time is now much simpler and more dependable thanks to the new Date and Time API (java.time) that was introduced with Java 8.

java.util.Date (Pre-Java 8):

  1. Represents a specific point in time, including both date and time.
  2. It provides basic methods like getTime(), setTime(), and toString() for interacting with dates.

The java.util.Date class marks a specific moment in time, down to the millisecond. However, this class is somewhat outdated and has issues with time zone handling and mutability.


//Main.java file
import java.util.Date;  // Import Date class from java.util package

public class Main {
    public static void main(String[] args) {
        // Create a Date object representing the current date and time
        Date currentDate = new Date();
        
        // Print the current date and time
        System.out.println("Current Date and Time: " + currentDate);

        // Create a Date object for a specific time (in milliseconds)
        long millisecondsSinceEpoch = 1735130928642L;  // Wed Dec 25 12:48:48 GMT 2024
        Date specificDate = new Date(millisecondsSinceEpoch);
        System.out.println("Specific Date: " + specificDate);
    }
}

Output:

Current Date and Time: Wed Dec 25 12:49:16 GMT 2024
Specific Date: Wed Dec 25 12:48:48 GMT 2024

Explanation:

  1. new Date() creates a Date object that represents the current date and time.
  2. The Date constructor can also accept a long value representing the number of milliseconds since January 1, 1970 (the Unix epoch).

java.time (Java 8 and later):

  1. A modern, unchangeable, and flexible API for date and time.
  2. It includes classes like LocalDate, LocalTime, LocalDateTime, ZonedDateTime, and Instant.

ava 8 introduced a new Date and Time API that is more powerful and flexible. It has various classes that make it easier to manage dates, times, durations, and time zones.

There are many methods for java.time

1. Use of LocalDate (Date without Time)


import java.time.LocalDate;  // Import LocalDate class from java.time package

public class Main {
    public static void main(String[] args) {
        // Get the current date
        LocalDate currentDate = LocalDate.now();
        System.out.println("Current Date: " + currentDate);  // Output: Current Date: 2020-01-01
        
        // Create a specific date (Year, Month, Day)
        LocalDate specificDate = LocalDate.of(2020, 1, 1);
        System.out.println("Specific Date: " + specificDate);  // Output: Specific Date: 2021-01-01
    }
}

Explanation:

  1. LocalDate.now() gets the current date.
  2. LocalDate.of(year, month, day) allows you to create a specific date.

2. Use of LocalTime (Time without Date)


import java.time.LocalTime;  // Import LocalTime class from java.time package

public class Main {
    public static void main(String[] args) {
        // Get the current time
        LocalTime currentTime = LocalTime.now();
        System.out.println("Current Time: " + currentTime);  // Output: Current Time: 12:45:23.123
        
        // Create a specific time (Hour, Minute, Second)
        LocalTime specificTime = LocalTime.of(11, 30, 0);
        System.out.println("Specific Time: " + specificTime);  // Output: Specific Time: 11:30
    }
}

Explanation:

  1. LocalTime.now() gives the current time.
  2. LocalTime.of(hour, minute, second) allows creating a specific time.

3. Use of LocalDateTime (Date and Time)


import java.time.LocalDateTime;  // Import LocalDateTime class from java.time package

public class Main {
    public static void main(String[] args) {
        // Get the current date and time
        LocalDateTime currentDateTime = LocalDateTime.now();
        System.out.println("Current Date and Time: " + currentDateTime);  // Output: Current Date and Time: 2020-12-25T13:05:21.214941871
        
        // Create a specific date and time
        LocalDateTime specificDateTime = LocalDateTime.of(2020, 1, 1, 11, 30);
        System.out.println("Specific Date and Time: " + specificDateTime);  // Output: Specific Date and Time: 2020-01-01T11:30
    }
}

Explanation:

  1. LocalDateTime.now() gets the current date and time.
  2. LocalDateTime.of(year, month, day, hour, minute) creates a specific date and time.

4. Use of ZonedDateTime (Date, Time, and Time Zone)


import java.time.ZonedDateTime;  // Import ZonedDateTime class from java.time package
import java.time.ZoneId;  // Import ZoneId class for time zone handling

public class ZonedDateTimeExample {
    public static void main(String[] args) {
        // Get the current date and time with time zone
        ZonedDateTime currentZonedDateTime = ZonedDateTime.now();
        System.out.println("Current Zoned Date and Time: " + currentZonedDateTime);  // Output: Current Zoned Date and Time: 2020-01-01T11:45:23.123+01:00[Europe/Paris]
        
        // Create a specific ZonedDateTime with a time zone
        ZonedDateTime specificZonedDateTime = ZonedDateTime.of(2020, 1, 1, 11, 30, 0, 0, ZoneId.of("America/New_York"));
        System.out.println("Specific Zoned Date and Time: " + specificZonedDateTime);  // Output: Specific Zoned Date and Time: 2020-01-01T11:30-05:00[America/New_York]
    }
}

Explanation:

  1. ZonedDateTime.now() gets the current date, time, and time zone.
  2. ZonedDateTime.of(year, month, day, hour, minute, second, nano, zoneId) creates a ZonedDateTime with a specific time zone.

Java Date and Time – Questions and Answers

Q 1: Which package is used for date and time?

Ans: java.time package.

Q 2: What is LocalDate?

Ans: Represents date without time.

Q 3: What is LocalTime?

Ans: Represents time without date.

Q 4: What is LocalDateTime?

Ans: Represents both date and time.

Q 5: Why is java.time preferred?

Ans: It is immutable and thread-safe.

Java Date and Time – Objective Questions (MCQs)

Q1. Which of the following classes is part of the java.time package introduced in Java 8?






Q2. Which method of LocalDate is used to get the current date?






Q3. What is the correct way to create a LocalDateTime object representing 2025-10-11 15:30?






Q4. Which class is used to represent a specific instant in time, with nanosecond precision?






Q5. What does the following code output?

LocalTime time = LocalTime.of(10, 30);
System.out.println(time.plusHours(2));






Related Java Date and Time Topics