Java 8 – How to Format LocalDateTime

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

 

Introduction

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. Let’s take a look at the following example

package com.javadevjournal;

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

public class StringToLocalDateTIme {

    public static void main(String[] args) {

        String dateTime = "2018-12-11 17:30";

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        LocalDateTime formatDateTime = LocalDateTime.parse(dateTime, formatter);

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

Here is the output of the program:

Parsed Date : 2018-12-11T17:30

Keep in Mind that commonly used date/time formats predefined as constants in DateTimeFormatter.

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);
    }
}

 

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.

Umesh

Hello!! I am Umesh- an engineer by profession and a photographer by passion.I like to build stuff on the web using OSS and love to capture the world through my lens.

follow me on:

Leave a Reply

avatar

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

  Subscribe  
Notify of