Java 8 Date Time API Example

In this article, we will see newly introduced Date Time API’s in Java 8.

Drawbacks with Old Date & Time API (java.util)

  1. Ambiguity in package selection
    • Difficult to identify which package to use as same class name is available in multiple packages.
  2. Not a Thread Safe
    • The Older java.util.Date is not thread safe which can creates problem in multi threaded environment.
  3. Formatting requires additional package
    • To format the Date or Time in custom format it requires another additional package (java.text package) which is not available in java.util
  4. Difficult to handle Time Zone
    • No provision to use Time Zone operations in existing API. Developer needs to add additional code to handle time zone related operations.

Why new Date Time API?

  1. API Clarity
    • Only single package is used (java.time) with crystal clear class names. For example LocalDate, LocalTime, LocalDateTime etc.
  2. Immutable and Thread Safe
    • New API is immutable and Thread Safe
  3. Method Chaining
    • Method Chaining is a provide for better code structure
  4. Flexibility
    • New API is very flexible in terms of its usage & representation

Available classes in new package java.time

  • Instant – represents a point in time (timestamp)
  • LocalDate – represents a date (year, month, day)
  • LocalDateTime – same as LocalDate, but includes time with nanosecond precision
  • OffsetDateTime – same as LocalDateTime, but with time zone offset
  • LocalTime – time with nanosecond precision and without date information
  • ZonedDateTime – same as OffsetDateTime, but includes a time zone ID
  • OffsetLocalTime – same as LocalTime, but with time zone offset
  • MonthDay – month and day, without year or time
  • YearMonth – month and year, without day or time
  • Duration – amount of time represented in seconds, minutes and hours. Has nanosecond precision
  • Period – amount of time represented in days, months and years

Get Local Date (only Date)

LocalDate localDate = LocalDate.now();
System.out.println("Local Date : "+localDate);
Local Date : 2021-11-21

Get Local Time (only Time)

LocalTime localTime = LocalTime.now();
System.out.println("Local Time : "+localTime);
Local Time : 12:29:39.953

Get Local Time (only Time)

LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("Local Date & Time : "+localDateTime);
Local Date & Time : 2021-11-21T12:34:35.778

Get Specific Date (using of() method)

LocalDate localDate = LocalDate.of(2015, 10, 20);
System.out.println("Local Date (month with number) : "+localDate);
		
localDate = LocalDate.of(2015, Month.OCTOBER, 20);
System.out.println("Local Date (month with enum) : "+localDate);
Local Date (month with number) : 2015-10-20
Local Date (month with enum) : 2015-10-20

Working with Specific TimeZone

To get the local time zone you just need to use the now() method. This method will get the default time zone (of your server or machine).

ZonedDateTime zoneDateTime = ZonedDateTime.now();
System.out.println("Zoned Date Time (default) : "+zoneDateTime);
Zoned Date Time (default) : 2021-11-21T12:44:50.039+05:30[Asia/Calcutta]

To get the custom time zone you need to pass the appropriate zone id.

ZonedDateTime zCentralEstern = LocalDateTime.now().atZone(ZoneId.of("CET"));
System.out.println("Zoned Date Time (Central Estern) : "+zCentralEstern);
Zoned Date Time (CentralEstern) : 2021-11-21T12:44:50.039+01:00[CET]

You can get all available zone id from ZoneId class.

for (String str : ZoneId.getAvailableZoneIds()) {
	System.out.println(str);
}
Asia/Aden
America/Cuiaba
Etc/GMT+9
Etc/GMT+8
Africa/Nairobi
America/Marigot
Asia/Aqtau
Pacific/Kwajalein
America/El_Salvador
...

Formatting Date & Time

LocalDate now = LocalDate.now();
System.out.println("Simple Date :"+ now);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MMM-dd");
		
String strFormattedDate = now.format(dtf);
System.out.println("Formatted Date : "+strFormattedDate);
		
LocalDate parsedDate = LocalDate.parse(strFormattedDate, dtf);
System.out.println("Parsed LocalDate from String : "+parsedDate);
Simple Date :2021-11-21
Formatted Date : 2021-Nov-21
Parsed LocalDate from String : 2021-11-21

Leave a comment

Design a site like this with WordPress.com
Get started