Java - New way to use date and time

By xngo on June 15, 2019

In Java 8, new Date and Time APIs are introduced to address the shortcomings of the older classes such as java.util.Date and java.util.Calendar. Among the issues with the older classes are:

  • They are not thread safe. Debugging concurrency issues are a pain.
  • They are poorly designed with inadequate methods to perform common operations.

The new Date and Time APIs are immutable and thread safe. And, they follow ISO-8601 domain models for date, time, duration and periods.

Now, let's look at some of the core classes of the new Date and Time APIs that are part of the java.time package such as LocalDate, LocalDateTime and ZonedDateTime. If you don't need to handle timezone information, then use LocalDate and LocalDateTime. Otherwise, use ZonedDateTime.

Here are some examples using the new APIs.

Get current date & time

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
 
public class DateTime8Current {
 
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate.toString());
 
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime.toString());
 
        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        System.out.println(zonedDateTime.toString());
 
        // Convert current date time to different formats than ISO-8601.
        // **************************************************************
        System.out.println(
                localDate.format(DateTimeFormatter.ofPattern("E, dd MMMM yyyy")));
 
        System.out.println(
                localDateTime.format(DateTimeFormatter.ofPattern("yyyy/MM/dd hh:mm:ss a")));
 
        System.out.println(
                zonedDateTime.format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss z")));
    }
}

Output

2019-06-15
2019-06-15T15:48:02.455
2019-06-15T15:48:02.456Z[Etc/UTC]
Sat, 15 June 2019
2019/06/15 03:48:02 PM
2019/06/15 15:48:02 UTC

Set specific date & time

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.ZoneId;
 
public class DateTime8Specific {
 
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.of(2019, 10, 27);
        System.out.println(localDate.toString());
 
        LocalDateTime localDateTime = LocalDateTime.of(2019, 10, 27, 7, 59, 59);
        System.out.println(localDateTime.toString());
 
        ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("UTC-5"));
        System.out.println(zonedDateTime.toString());
    }
}

Output

2019-10-27
2019-10-27T07:59:59
2019-10-27T07:59:59-05:00[UTC-05:00]

Parse date & time from string

import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
 
public class DateTime8Parse {
 
    public static void main(String[] args) {
 
        String str = "2016/03/27, 11:37:59";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd, HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
 
        System.out.println(dateTime.toString());
    }
}

Output

2016-03-27T11:37:59

Github

  • https://github.com/xuanngo2001/java-small/blob/master/src/net/openwritings/java/time/DateTime8Current.java
  • https://github.com/xuanngo2001/java-small/blob/master/src/net/openwritings/java/time/DateTime8Specific.java
  • https://github.com/xuanngo2001/java-small/blob/master/src/net/openwritings/java/time/DateTime8Parse.java

Reference

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.