Java - Using an Optional Section in a Datetime Formatting Pattern

Description

Using an Optional Section in a Datetime Formatting Pattern

Demo

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

public class Main {
  public static void main(String[] args) {
    // A pattern with an optional section
    String pattern = "MM/dd/yyyy[ 'at' HH:mm:ss]";
    DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern);

    LocalDate ld = LocalDate.of(2018, Month.MAY, 30);
    LocalTime lt = LocalTime.of(17, 30, 12);
    LocalDateTime ldt = LocalDateTime.of(ld, lt);

    // Format a date. Optional section will be skipped because a
    // date does not have time (HH, mm, and ss) information.
    String str1 = fmt.format(ld);
    System.out.println(str1);//  ww  w. j a  v  a 2s  . co  m

    // Format a datetime. Optional section will be output.
    String str2 = fmt.format(ldt);
    System.out.println(str2);
  }
}

Result

Related Topic