Java – Get Current Date and Time

I found a very good example for showing current datetime in Java.

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

public class GetCurrentDatetime {

  public static String now(String dateFormat) {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
    return sdf.format(cal.getTime());
  }

  public static void  main(String arg[]) {
     System.out.println(GetCurrentDatetime.now("dd MMMMM yyyy"));
     System.out.println(GetCurrentDatetime.now("yyyyMMdd"));
     System.out.println(GetCurrentDatetime.now("dd.MM.yy"));
     System.out.println(GetCurrentDatetime.now("MM/dd/yy"));
     System.out.println(GetCurrentDatetime.now("yyyy.MM.dd G 'at' hh:mm:ss z"));
     System.out.println(GetCurrentDatetime.now("EEE, MMM d, ''yy"));
     System.out.println(GetCurrentDatetime.now("h:mm a"));
     System.out.println(GetCurrentDatetime.now("H:mm:ss:SSS"));
     System.out.println(GetCurrentDatetime.now("K:mm a,z"));
     System.out.println(GetCurrentDatetime.now("yyyy.MMMMM.dd GGG hh:mm aaa"));
  }
}

 

The console output.

28 八月 2011
20110828
28.08.11
08/28/11
2011.08.28 西元 at 12:23:18 CST
星期日, 八月 28, '11
12:23 上午
0:23:18:282
0:23 上午,CST
2011.八月.28 西元 12:23 上午

 

Done =)

Reference: Real’s How To – Get the current Date and Time

About these ads

2 thoughts on “Java – Get Current Date and Time

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s