Date and Time API

The Date and Time API in Java, introduced in Java 8, makes working with dates and times easier, more intuitive, and less error-prone. It replaces the older Date and Calendar classes, which were confusing and hard to work with. This new API is part of the java.time package.

Why Use the Date and Time API?

The new Date and Time API provides:

  • Simpler Code: Easier to understand and less error-prone.
  • Immutability: Date and time objects are immutable, meaning they cannot be changed, which makes code safer.
  • Clarity: Clearer class names and methods that better reflect date and time operations.

Main Classes in the Date and Time API

The Date and Time API has several main classes that help you handle dates, times, and durations:

  • LocalDate: Represents a date (year, month, day) without a time zone.
  • LocalTime: Represents a time (hours, minutes, seconds) without a date or time zone.
  • LocalDateTime: Represents both date and time without a time zone.
  • ZonedDateTime: Represents date and time with a specific time zone.

Basic Usage Examples

Working with LocalDate

LocalDate is used for representing just a date, like a birthday or an event date.

Unknown
Copy
import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();               // Current date
        LocalDate birthday = LocalDate.of(1990, 5, 15);  // Specified date

        System.out.println("Today: " + today);
        System.out.println("Birthday: " + birthday);
    }
}

Working with LocalTime

LocalTime is for representing just a time, such as store opening hours or an alarm time.

Java
Copy
import java.time.LocalTime;

public class Main {
    public static void main(String[] args) {
        LocalTime now = LocalTime.now();            // Current time
        LocalTime alarmTime = LocalTime.of(7, 30);  // Specified time

        System.out.println("Now: " + now);
        System.out.println("Alarm Time: " + alarmTime);
    }
}

Working with LocalDateTime

LocalDateTime is used when you need both date and time, but not the time zone.

Java
Copy
import java.time.LocalDateTime;

public class Main {
    public static void main(String[] args) {
        LocalDateTime currentDateTime = LocalDateTime.now();           // Current date and time
        LocalDateTime meeting = LocalDateTime.of(2023, 10, 10, 14, 0); // Specified date and time

        System.out.println("Current Date and Time: " + currentDateTime);
        System.out.println("Meeting Date and Time: " + meeting);
    }
}

Working with ZonedDateTime

ZonedDateTime adds a time zone to your date and time, which is helpful for handling global applications.

Java
Copy
import java.time.ZonedDateTime;
import java.time.ZoneId;

public class Main {
    public static void main(String[] args) {
        ZonedDateTime currentDateTime = ZonedDateTime.now();                          // Current date and time with time zone
        ZonedDateTime tokyoDateTime = ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));     // Date and time in Tokyo

        System.out.println("Current Date and Time with Zone: " + currentDateTime);
        System.out.println("Tokyo Date and Time with Zone: " + tokyoDateTime);
    }
}

Manipulating Dates and Times

The Date and Time API allows easy manipulation of dates and times. For example, you can add or subtract days, months, or years:

Java
Copy
LocalDate today = LocalDate.now();
LocalDate nextWeek = today.plusWeeks(1);    // Adds 1 week
LocalDate lastMonth = today.minusMonths(1); // Subtracts 1 month

System.out.println("Today: " + today);
System.out.println("Next Week: " + nextWeek);
System.out.println("Last Month: " + lastMonth);

Formatting Dates and Times

You can format dates and times to make them more readable or suitable for display:

Java
Copy
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");

        String formattedDateTime = now.format(formatter);
        System.out.println("Formatted Date and Time: " + formattedDateTime);
    }
}