Java8 Overview of the java.time package

Category : Java | Sub Category : Java8 Features | By Prasad Bonam Last updated: 2023-11-13 05:45:24 Viewed : 234


The java.time package, introduced in Java 8, provides a comprehensive set of classes for handling date and time in a more modern and sophisticated way than the previous java.util.Date and java.util.Calendar classes. The new API in java.time is part of the Java Date and Time API (JSR-310) and is inspired by the popular Joda-Time library. The primary classes in the java.time package are in the java.time subpackage.

Here is an overview of some key classes and concepts in the java.time package:

1. LocalDate:

  • Represents a date without a time component.
  • Useful for dealing with birthdays, anniversaries, etc.
java
LocalDate today = LocalDate.now();

2. LocalTime:

  • Represents a time without a date component.
  • Useful for representing times of day, such as business opening hours.
java
LocalTime now = LocalTime.now();

3. LocalDateTime:

  • Combines LocalDate and LocalTime to represent both date and time.
  • Represents a point in time without a time zone.
java
LocalDateTime dateTime = LocalDateTime.now();

4. ZonedDateTime:

  • Extends LocalDateTime to include time zone information.
  • Useful for representing instants in time in a specific time zone.
java
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("America/New_York"));

5. Duration:

  • Represents a duration of time, such as "2 hours" or "30 seconds."
java
Duration duration = Duration.ofHours(2);

6. Period:

  • Represents a period of time in terms of years, months, and days.
java
Period period = Period.ofMonths(3);

7. Instant:

  • Represents an instantaneous point in time.
  • Useful for converting between different time representations.
java
Instant instant = Instant.now();

8. ZoneId:

  • Represents a time zone.
  • Used to convert between ZonedDateTime and Instant.
java
ZoneId zoneId = ZoneId.of("America/Los_Angeles");
ZonedDateTime losAngelesTime = ZonedDateTime.now(zoneId);9. DateTimeFormatter:
  • Formats and parses dates and times.
  • Allows customization of the output format.
java
LocalDateTime dateTime = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = dateTime.format(formatter);

10. TemporalAdjusters:

  • Provides common date adjustments, such as finding the first or last day of the month.
java
LocalDate firstDayOfMonth = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());

11. ChronoUnit:

  • Represents units of time, such as days or hours.
  • Used with Duration to perform time-based arithmetic.
java
LocalDateTime now = LocalDateTime.now(); LocalDateTime future = now.plus(3, ChronoUnit.DAYS);

12. Clock:

  • Represents the current instant in time.
  • Useful for obtaining the current date and time in a specific time zone.
java
Clock clock = Clock.systemDefaultZone(); LocalDateTime now = LocalDateTime.now(clock);

Example:

java
import java.time.*; public class DateTimeExample { public static void main(String[] args) { // LocalDate LocalDate today = LocalDate.now(); System.out.println("Todays date: " + today); // LocalTime LocalTime now = LocalTime.now(); System.out.println("Current time: " + now); // LocalDateTime LocalDateTime dateTime = LocalDateTime.now(); System.out.println("Date and time: " + dateTime); // ZonedDateTime ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("America/New_York")); System.out.println("New York time: " + zonedDateTime); // Duration Duration duration = Duration.ofHours(2); System.out.println("Duration: " + duration); // Period Period period = Period.ofMonths(3); System.out.println("Period: " + period); // Instant Instant instant = Instant.now(); System.out.println("Instant: " + instant); // ZoneId ZoneId zoneId = ZoneId.of("Europe/Paris"); ZonedDateTime parisTime = ZonedDateTime.now(zoneId); System.out.println("Paris time: " + parisTime); // DateTimeFormatter DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = dateTime.format(formatter); System.out.println("Formatted date and time: " + formattedDateTime); // TemporalAdjusters LocalDate firstDayOfMonth = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()); System.out.println("First day of the month: " + firstDayOfMonth); // ChronoUnit LocalDateTime future = dateTime.plus(

Search
Related Articles

Leave a Comment: