Using Patterns to Format Dates and Times - Java Date Time

Java examples for Date Time:Date Time Format

Description

Using Patterns to Format Dates and Times

Demo Code

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(2019, 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]");
    /*w  w w  .j  av a  2 s  . c o  m*/
    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]");
  }
    
  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 Tutorials