Java Data Type How to - Format LocalDateTime with short German date/time formatting as 01.04.14 10:45








Question

We would like to know how to format LocalDateTime with short German date/time formatting as 01.04.14 10:45.

Answer

import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;
//from  ww w .j  a va2 s.c o  m
public class Main {
  public static void main(String[] args) {
    // 2014-04-01 10:45
    LocalDateTime dateTime = LocalDateTime.of(2014, Month.APRIL, 1, 10, 45);

    // using short german date/time formatting (01.04.14 10:45)
    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(new Locale("de"));
    String germanDateTime = dateTime.format(formatter);
    
    System.out.println(germanDateTime);
  }
}

The code above generates the following result.