Date Formatter

DateFormatter.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package passionatecodes;

import java.text.SimpleDateFormat;
import java.util.Date;
 
public class DateFormatter {
   public static void main(String[] args) {
           
      Date now = new Date();
      System.out.println("toString(): " + now); 
     
      // SimpleDateFormat can be used to control the date/time display format:
      //   E (day of week): 3E or fewer (in text xxx), >3E (in full text)
      //   M (month): M (in number), MM (in number with leading zero)
      //              3M: (in text xxx), >3M: (in full text full)
      //   h (hour): h, hh (with leading zero)
      //   m (minute)
      //   s (second)
      //   a (AM/PM)
      //   H (hour in 0 to 23)
      //   z (time zone)

      SimpleDateFormat dateFormatter = new SimpleDateFormat("E, y-M-d 'at' h:m:s a z");
      System.out.println("Format 1:   " + dateFormatter.format(now));
     
      dateFormatter = new SimpleDateFormat("MM.dd.yyyy");
      System.out.println("Format 2:   " + dateFormatter.format(now));
     
      dateFormatter = new SimpleDateFormat("EEEE, MMMM d, yyyy");
      System.out.println("Format 3:   " + dateFormatter.format(now));
     
   }
}



Output:
toString(): Sat Apr 19 01:02:36 IST 2014
Format 1:   Sat, 2014-4-19 at 1:2:36 AM IST
Format 2:   04.19.2014
Format 3:   Saturday, April 19, 2014

No comments:

Post a Comment