java
  1. java-date-and-time

Java Date and Time

The Java Date and Time APIs provide a suite of classes to handle date and time manipulation in Java. In this tutorial, we'll discuss how to use these APIs to work with dates, times, and time zones.

Syntax

To work with dates and times in Java, we use the following classes:

  • java.util.Date: Represents a specific moment in time, with millisecond precision.
  • java.util.Calendar: Provides methods to manipulate and format dates and times.
  • java.time.LocalDate: Represents a date, with no reference to time or time zone.
  • java.time.LocalTime: Represents a time, with no reference to a date or time zone.
  • java.time.LocalDateTime: Represents a date and time, with no reference to time zone.
  • java.time.ZonedDateTime: Represents a date and time, together with a time zone.

Example

To get the current date and time, we can use the LocalDateTime class:

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);
    }
}

To parse a string to a date, we can use the DateTimeFormatter class:

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

public class Main {
    public static void main(String[] args) {
        String dateString = "2021-11-01";
        LocalDate date = LocalDate.parse(dateString, DateTimeFormatter.ISO_DATE);
        System.out.println("Parsed date: " + date);
    }
}

To add or subtract days from a date, we can use the LocalDate class:

import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        LocalDate tomorrow = today.plusDays(1);
        LocalDate yesterday = today.minusDays(1);
        System.out.println("Today: " + today);
        System.out.println("Tomorrow: " + tomorrow);
        System.out.println("Yesterday: " + yesterday);
    }
}

Output

The output of the above examples will be:

Current date and time: 2021-11-11T20:41:57.860010
Parsed date: 2021-11-01
Today: 2021-11-11
Tomorrow: 2021-11-12
Yesterday: 2021-11-10

Explanation

Java provides a suite of classes to manipulate dates and times. The java.util.Date class represents a specific moment in time. The java.util.Calendar class provides methods to manipulate and format dates and times.

Java 8 introduced a new set of classes for handling dates and times, located in the java.time package. These classes provide better performance, accuracy, and thread-safety compared to the older Date and Calendar classes. The LocalDateTime class represents a date and time, the LocalDate class represents a date, and the LocalTime class represents a time.

Use

Java Date and Time APIs are useful when working with dates and times in Java, especially in use cases such as:

  • Calculating and comparing dates
  • Parsing dates from strings
  • Formatting dates for display
  • Working with different time zones

Important Points

  • java.util.Date and java.util.Calendar classes are legacy classes and are not recommended for use in new code.
  • The java.time package classes in Java 8 provide better performance, accuracy, and thread-safety.
  • Always use the appropriate time zone for your use case to avoid confusion.
  • Use the DateTimeFormatter class to parse dates from strings.

Summary

In this tutorial, we discussed how to use Java Date and Time APIs to work with dates, times, and time zones. We covered the syntax, example, output, explanation, use, important points, and summary of working with Java Date and Time APIs. With this knowledge, you can now handle dates and times in your Java applications while avoiding legacy classes and leveraging more efficient APIs introduced in Java 8.

Published on: