Formatting Dates for Display using Calendar and SimpleDateFormat - Java Date Time

Java examples for Date Time:Date Time Format

Description

Formatting Dates for Display using Calendar and SimpleDateFormat

Demo Code

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Main {

  public static void main(String[] args) {
    // Create new calendar
    Calendar cal = Calendar.getInstance();

    // Create instance of SimpleDateFormat class using pattern
    SimpleDateFormat dateFormatter1 = new SimpleDateFormat("MMMMM dd yyyy");
    String result = null;//from w  w  w. ja  v  a 2  s  .  c  o  m

    result = dateFormatter1.format(cal.getTime());
    System.out.println(result);

    dateFormatter1.applyPattern("MM/dd/YY hh:mm:ss");
    result = dateFormatter1.format(cal.getTime());
    System.out.println(result);

    dateFormatter1.applyPattern("hh 'o''clock' a, zzzz");
    result = dateFormatter1.format(cal.getTime());
    System.out.println(result);
  }
}

Result


Related Tutorials