Java Data Type How to - Create MEDIUM DateTimeFormatter








Question

We would like to know how to create MEDIUM DateTimeFormatter.

Answer

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
//from  w w  w . j  a v a 2 s .  co m
public class Main {

  public static void main(String[] args) {
    DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
    System.out.println(dateFormatter.format(LocalDate.now())); // Current Local Date
    System.out.println(dateFormatter.parse("Jan 19, 2014").getClass().getName()); //java.time.format.Parsed
    System.out.println(dateFormatter.parse("Jan 19, 2014", LocalDate::from)); // Jan 19, 2014

  }
}

The code above generates the following result.