Using an Optional Section in a Datetime Formatting Pattern - Java Date Time

Java examples for Date Time:Date Time Format

Description

Using an Optional Section in a Datetime Formatting Pattern

Demo Code

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(2012, 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);//from   ww  w.  j a  v  a2  s .co m

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

Result


Related Tutorials