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("dd MMMMM yyyy"));
     System.out.println("yyyy.MM.dd G 'at' hh:mm:ss z"));
     System.out.println("EEE, MMM d, ''yy"));
     System.out.println("h:mm a"));
     System.out.println("K:mm a,z"));
     System.out.println("yyyy.MMMMM.dd GGG hh:mm aaa"));


The console output.

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


Done =)

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

2 thoughts on “Java – Get Current Date and Time”

Leave a Reply

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

You are commenting using your 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