Java - Date Time Format Predefined Style

Introduction

DateTimeFormatter class defines DateTimeFormatter with a predefined formatting.

You can use the following methods to obtain a reference to such formatters:

DateTimeFormatter ofLocalizedDate(FormatStyle dateStyle)
DateTimeFormatter ofLocalizedDateTime(FormatStyle dateTimeStyle)
DateTimeFormatter ofLocalizedDateTime(FormatStyle dateStyle, FormatStyle timeStyle)
DateTimeFormatter ofLocalizedTime(FormatStyle timeStyle)

FormatStyle enum type has four constants: SHORT, MEDIUM, LONG, and FULL.

These constants outputs formatted date and time with a varying degree of detail.

The following code shows how to use some predefined locale-specific formats.

It formats dates and times in US (default), German, and Indian locales.

Demo

import static java.time.format.FormatStyle.FULL;
import static java.time.format.FormatStyle.LONG;
import static java.time.format.FormatStyle.MEDIUM;
import static java.time.format.FormatStyle.SHORT;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
  public static void main(String[] args) {
    LocalDate ld = LocalDate.of(2018, Month.APRIL, 19);
    LocalTime lt = LocalTime.of(16, 30, 20);
    LocalDateTime ldt = LocalDateTime.of(ld, lt);

    DateTimeFormatter fmt = DateTimeFormatter.ofLocalizedDate(SHORT);
    System.out.println("Formatter Default Locale: " + fmt.getLocale());
    System.out.println("Short Date: " + fmt.format(ld));

    fmt = DateTimeFormatter.ofLocalizedDate(MEDIUM);
    System.out.println("Medium Date: " + fmt.format(ld));

    fmt = DateTimeFormatter.ofLocalizedDate(LONG);
    System.out.println("Long Date: " + fmt.format(ld));

    fmt = DateTimeFormatter.ofLocalizedDate(FULL);
    System.out.println("Full Date: " + fmt.format(ld));

    fmt = DateTimeFormatter.ofLocalizedTime(SHORT);
    System.out.println("Short Time: " + fmt.format(lt));

    fmt = DateTimeFormatter.ofLocalizedDateTime(SHORT);
    System.out.println("Short Datetime: " + fmt.format(ldt));

    fmt = DateTimeFormatter.ofLocalizedDateTime(MEDIUM);
    System.out.println("Medium Datetime: " + fmt.format(ldt));

    // Use German locale to format the datetime in MEDIUM style
    fmt = DateTimeFormatter.ofLocalizedDateTime(MEDIUM).withLocale(
        Locale.GERMAN);//from w  w  w .jav a2 s. co m
    System.out.println("German Medium Datetime: " + fmt.format(ldt));

    // Use Indian(English) locale to format datetime in short style
    fmt = DateTimeFormatter.ofLocalizedDateTime(SHORT).withLocale(
        new Locale("en", "IN"));
    System.out.println("Indian(en) Short Datetime: " + fmt.format(ldt));

    // Use Indian(English) locale to format datetime in medium style
    fmt = DateTimeFormatter.ofLocalizedDateTime(MEDIUM).withLocale(
        new Locale("en", "IN"));
    System.out.println("Indian(en) Medium Datetime: " + fmt.format(ldt));

  }
}

Result