Java - Using Patterns to Format Dates and Times

Description

Using Patterns to Format Dates and Times

Demo

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;
import java.util.Locale;

public class Main {
  public static void main(String[] args) {
    LocalDate ld = LocalDate.of(2018, Month.APRIL, 30);
    System.out.println("Formatting date: " + ld);
    format(ld, "M/d/yyyy");
    format(ld, "MM/dd/yyyy");
    format(ld, "MMM dd, yyyy");
    format(ld, "MMMM dd, yyyy");
    format(ld, "EEEE, MMMM dd, yyyy");
    format(ld, "'Month' q 'in' QQQ");
    format(ld, "[MM-dd-yyyy][' at' HH:mm:ss]");

    LocalTime lt = LocalTime.of(16, 30, 5, 78899);
    System.out.println("\nFormatting time:" + lt);
    format(lt, "HH:mm:ss");
    format(lt, "KK:mm:ss a");
    format(lt, "[MM-dd-yyyy][' at' HH:mm:ss]");

    ZoneId usCentral = ZoneId.of("America/Chicago");
    ZonedDateTime zdt = ZonedDateTime.of(ld, lt, usCentral);
    System.out.println("\nFormatting zoned datetime:" + zdt);
    format(zdt, "MM/dd/yyyy HH:mm:ssXXX");
    format(zdt, "MM/dd/yyyy VV");
    format(zdt, "[MM-dd-yyyy][' at' HH:mm:ss]");
  }/*from   w w  w  . ja v a  2 s .c om*/

  public static void format(Temporal co, String pattern) {
    DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern, Locale.US);
    String str = fmt.format(co);
    System.out.println(pattern + ": " + str);
  }
}

Result

Related Topic