Date and Time in Java

The java.time package in Java provides classes to represent and manipulate dates and times.

Some of the most commonly used Date and Time classes are :
  • LocalDate: Represents a date without a time or time zone.
  • LocalTime: Represents a time without a date or time zone.
  • LocalDateTime: Represents a date and time without a time zone.
  • ZonedDateTime: Represents a date and time with a time zone.
  • DateTimeFormatter: Used to format and parse dates and times.

Here's a simple program that demonstrates how to use the LocalDate and LocalTime classes in Java to represent and manipulate dates and times.

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

public class DateTimeExample {
   public static void main(String[] args) {
      // Representing a date
      LocalDate today = LocalDate.now();
      System.out.println("Today's date is: " + today);
      
      // Representing a time
      LocalTime time = LocalTime.now();
      System.out.println("Current time is: " + time);
      
      // Combining date and time
      LocalDateTime dateTime = LocalDateTime.of(today, time);
      System.out.println("Date and time: " + dateTime);
   }
}
Output
Today's date is: 2023-01-31
Current time is: 10:48:08.130879800
Date and time: 2023-01-31T10:48:08.130879800

In this example, we use the now() method to get the current date and time. Then we use the of() method to combine the date and time into a single LocalDateTime object. The output shows the current date and time.


Parsing and Formatting Date and Time in Java

The DateTimeFormatter class in Java is part of the java.time package, and it is used for formatting and parsing dates and times in various formats. It provides a flexible and customizable way to format and parse dates and times in a thread-safe manner.

With the DateTimeFormatter class, you can format and parse LocalDate, LocalTime, LocalDateTime, ZonedDateTime, and other date-time objects. You can specify the format using a pattern string or by using predefined constants such as ISO_DATE, ISO_TIME, ISO_DATE_TIME, etc.

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

public class DateTimeFormattingExample {
   public static void main(String[] args) {
      LocalDate today = LocalDate.now();
      DateTimeFormatter formatter = DateTimeFormatter
          .ofPattern("dd/MM/yyyy");
      String formattedDate = today.format(formatter);
      System.out.println("Formatted date: " + formattedDate);
   }
}
Output
Formatted date: 31/01/2023

Similarly, the following code parses a string into a LocalDate object.

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

public class DateTimeParsingExample {
   public static void main(String[] args) {
      String date = "31/01/2023";
      DateTimeFormatter formatter = DateTimeFormatter
          .ofPattern("dd/MM/yyyy");
      LocalDate parsedDate = LocalDate.parse(date, formatter);
      System.out.println("Parsed date: " + parsedDate);
   }
}
Output
Parsed date: 2023-01-31

Manipulating Date and Time in Java

The java.time package provides many methods for manipulating dates and times, including adding or subtracting days, weeks, months, etc. For example, the following code adds one month to a LocalDate object.

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

public class DateTimeManipulationExample {
   public static void main(String[] args) {
      LocalDate today = LocalDate.now();
      LocalDate nextMonth = today.plus(1, ChronoUnit.MONTHS);
      System.out.println("Next month: " + nextMonth);
   }
}
Output
Next month: 2023-02-28

In this example, we use the plus() method to add one month to the LocalDate object. The ChronoUnit.MONTHS constant is used to specify the unit of time to be added.