Java Data Type How to - Format LocalDateTime as ISO_DATE_TIME format








Question

We would like to know how to format LocalDateTime as ISO_DATE_TIME format.

Answer

//w w w  .  j  a  va  2  s  . c  o m
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;

public class Main {
  public static void main(String[] args) {
    // 2014-04-01 10:45
    LocalDateTime dateTime = LocalDateTime.of(2014, Month.APRIL, 1, 10, 45);

    // format ISO date time (2014-02-20T20:04:05.867)
    String asIsoDateTime = dateTime.format(DateTimeFormatter.ISO_DATE_TIME);
    
    System.out.println(asIsoDateTime);
  }
}

The code above generates the following result.