You can add optional section using []
in DateTimeFormatter.
import java.time.LocalDate; import java.time.LocalDateTime; 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.now(); String str1 = fmt.format(ld); System.out.println(str1);/*ww w .jav a 2 s .c om*/ LocalDateTime ldt = LocalDateTime.now(); String str2 = fmt.format(ldt); System.out.println(str2); } }