How to Format LocalDateTime in Java 8

In this short post, we are going to discuss how to format LocalDateTime in Java 8.

 

Format LocalDateTime in Java 8

Java 8 brought a number of new features and one of the important features was the Java 8 Date Time. This new API is based on the JSR 310. converting Date and Time into a different format is a very common day to day requirement. In this post, we will discuss the following two important points

  1. Format LocalDateTime in Java 8.
  2. Convert String to LocalDateTime.

 

1. DateTimeFormatter

To format LocalDateTime, use DateTimeFormatter available in the new time API.

package com.javadevjournal;

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

public class FormatDateTimeExample {

    private static final String DATE_FORMATTER= "yyyy-MM-dd HH:mm:ss";

    public static void main(String[] args) {

        LocalDateTime localDateTime = LocalDateTime.now(); //get current date time
        System.out.println("Current Time " + localDateTime);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMATTER);
        String formatDateTime = localDateTime.format(formatter);

        System.out.println("Formatted Time :" +formatDateTime);

    }
}

Here is the output of the program:

Current Time 2018-11-12T21:19:52.642
Formatted Time :2018-11-12 21:19:52

 

2. String to LocaDateTime

To convert String to LocaDateTime, use the static parse() method available with LocalDateTime class. This method takes 2 inputs

  1. A string input – the date which needs formatting.
  2. The DateTimeFormatter – To specify the date/time pattern or format.

Let’s take a look at the following example

package com.javadevjournal;

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

public class CommonTest {

    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

    public static void main(String[] args) {

        String dateTime = "2020-12-11 17:30";
        LocalDateTime formatDateTime = LocalDateTime.parse(dateTime, FORMATTER);

        System.out.println("Parsed Date : " + formatDateTime);
    }
}

Here is the output of the program:

Parsed Date : 2020-12-11T17:30

DateTimeFormatter is immutable and thread-safe, and thus the recommended approach is to store it in a static constant where possible

In case you like to use this predefined format, you can skip creating the DateTimeFormatter and can use the built-in formatter.

package com.javadevjournal;

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

public class BuiltInFormatterExample {

    public static void main(String[] args) {


        String dateTime = "2018-09-25T10:13:14.743";
        LocalDateTime localDateTime = LocalDateTime.parse(dateTime, DateTimeFormatter.ISO_DATE_TIME);
        System.out.println("ISO Date Time is "+localDateTime);
    }
}

 

3. Parsing String with ISO-8601 Format

If the String is ISO-8601 Format, we don’t need to specify the pattern while using the LocalDateTime.parse() method. The LocaDateTime.parse() method will automatically use the DateTimeFormatter.ISO_LOCAL_DATE_TIME as the default formatter.

public static void main(String[] args) {

     String dateTime = "2020-12-11T17:30";
     LocalDateTime formatDateTime = LocalDateTime.parse(dateTime);

     System.out.println("Parsed Date : " + formatDateTime);
}

 

Summary

In this quick post, we got an understanding of how to format LocalDateTime in Java 8. We learned how to format LocalDateTime object and what are the options to parse string date back into the LocalDateTime object.

Subscribe
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments