Java Calendar format via SimpleDateFormat

Introduction

Pattern Characters Description
G Era
y Year
YWeek year
MMonth in year
w Week in year
W Week in month
D Day in year
dDay in month
FDay of week in month
E Name of day in week
u Number of day in week
a AM/PM
H Hour in day (0?23)
k Hour in day (1?24)
K Hour in AM/PM (0?11)
h Hour in AM/PM (1?12)
m Minute in hour
s Second in minute
S Millisecond
zGeneral time zone
ZRFC 822 time zone
X ISO 8601 time zone

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;// w w w.  j  a 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);

   }
}



PreviousNext

Related