How to parse/format dates with Java 8 LocalDateTime

Java 8 LocalDateTime is an immutable date-time object used to represent a date-time. To parse/format a LocalDateTime object, you can use the DateTimeFormatter class.
The java.time.LocalDateTime object is viewed as year-month-day-hour-minute-second.

This tutorial will teach you how to parse/format a date value with Java 8 LocalDateTime.

Using LocalDateTime.format()

The LocalDateTime.format() method formats the date-time based on the specific pattern using the DateTimeFormatter.

The DateTimeFormatter provides different Patterns for Formatting and Parsing dates.

If the format does not have time(“H:mm:ss”) definitions, then the time value will not be printed, and also DateTimeException will be thrown, if an error occurs during printing.

Code

    LocalDateTime now = LocalDateTime.now();  
    System.out.println(now);  //2022-12-10T14:17:13.618981200

    String localDateTimeText = now.format(DateTimeFormatter.ofPattern("dd/MM/yyyy  H:mm:ss"));  
    System.out.println(localDateTimeText);  //10/12/2022 14:32:10

First is the default LocalDateTime value with ISO-8601 date format. In the second example, the custom pattern is passed to DateTimeFormatter, and the same LocalDateTime value is now formatted and printed.

Check this tutorial for formatting time in 12-hour AM/PM

Using LocalDateTime.parse()

The LocalDateTime.parse() method obtains an instance of the LocalDateTime object from a text string using a specific pattern.

Date along with Time needs to be passed, else DateTimeParseException will be thrown.

Code

    final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy H:mm:ss");  
    LocalDateTime localDateTime = LocalDateTime.parse("02/01/2023 10:22:33", formatter);  
    System.out.println(localDateTime);  //2023-01-02T10:22:33

Using LocalDate.parse()

The LocalDate.parse() can be used to parse a date value, and using the.atStartOfDay() method LocalDateTime value with time starting midnight(“T00:00“) can be obtained.

Code

    final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("LLL-dd-yyyy");  
    LocalDateTime localDateTme = LocalDate.parse("Jan-02-2023", formatter).atStartOfDay(); 
    System.out.println(localDateTme);  //2023-01-02T00:00

Using PreDefined Formats

The below examples give different predefined formatting options available for the LocalDateTime object.
Code

    System.out.println(LocalDateTime.now().format(DateTimeFormatter.BASIC_ISO_DATE));  //20221210
    System.out.println(LocalDateTime.now().format(DateTimeFormatter.ISO_DATE));   //2022-12-10 
    System.out.println(LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME));  //2022-12-10T14:27:28.0532086
    System.out.println(LocalDateTime.now().format(DateTimeFormatter.ISO_TIME));   //14:27:28.0532086 
    System.out.println(LocalDateTime.now().format(DateTimeFormatter.ISO_ORDINAL_DATE));  //2022-344
    System.out.println(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME));  //14:27:28.0532086
    System.out.println(LocalDateTime.now().format(DateTimeFormatter.ISO_WEEK_DATE));  //2022-W49-6
    System.out.println(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));  //2022-12-10T14:27:28.0542085

Related Topics

Leave a Comment