How to get current date and time using JAVA (SimpleDateFormat)


Here is the code example for getting the current date and time.

In this example java.text.SimpleDateFormat class is used for formatting the Date.

CurrentDateFormatter.java

package currentdateformattingexample;

/**
 *
 * @author tunatore
 */
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class CurrentDateFormatter {

    public static void main(String[] args) {
       
        System.out.println(“dd/MM/yyyy HH:mm:ss –> ” 
       + CurrentDateFormatter.getFormattedNow(“dd/MM/yyyy HH:mm:ss“));
        System.out.println(“MM/dd/yyyy HH:mm:ss –> ”
      + CurrentDateFormatter.getFormattedNow(“MM/dd/yyyy HH:mm:ss“));
        System.out.println(“MM/dd/yyyy HH:mm:ss:S –> ”
      + CurrentDateFormatter.getFormattedNow(“MM/dd/yyyy HH:mm:ss:S“));
        System.out.println(“yyyy/MM/dd HH:mm:ss –> ”
      + CurrentDateFormatter.getFormattedNow(“yyyy/MM/dd HH:mm:ss“));
        System.out.println(“dd.MM.yyyy HH:mm:ss –> ”
      + CurrentDateFormatter.getFormattedNow(“dd.MM.yyyy HH:mm:ss”));
        System.out.println(“dd.MM.yy –> ”
       + CurrentDateFormatter.getFormattedNow(“dd.MM.yy”));
        System.out.println(“MM/dd/yy –> ”
       + CurrentDateFormatter.getFormattedNow(“MM/dd/yy“));
        System.out.println(“dd/MM/yyyy HH:mm –> ”
       + CurrentDateFormatter.getFormattedNow(“dd/MM/yyyy HH:mm“));
        System.out.println(“dd MMMM yyyy –> ”
      + CurrentDateFormatter.getFormattedNow(“dd MMMM yyyy“));
        System.out.println(“yyyyMMMMdd –> ”
       + CurrentDateFormatter.getFormattedNow(“yyyyMMMMdd”));
        System.out.println(“yyMMddHHmmssZ –> ”
       + CurrentDateFormatter.getFormattedNow(“yyMMddHHmmssZ“));       
        System.out.println(“hh ‘o”clock’ a, zzzz –> ”
       + CurrentDateFormatter.getFormattedNow(“hh ‘o”clock’ a, zzzz”));
        System.out.println(“yyyy.MMMMM.dd GGG hh:mm aaa –> ”
       + CurrentDateFormatter.getFormattedNow(“yyyy.MMMMM.dd GGG hh:mm aaa“));
        System.out.println(“EEE, d MMM yyyy HH:mm:ss Z –> ”
       + CurrentDateFormatter.getFormattedNow(“EEE, d MMM yyyy HH:mm:ss Z“));      
        System.out.println(“yyyy.MM.dd G ‘@’ hh:mm:ss z –> ”
       + CurrentDateFormatter.getFormattedNow(“yyyy.MM.dd G ‘@’ hh:mm:ss z“));
        System.out.println(“EEE, MMM d, ‘year is ->’ yy”
       + CurrentDateFormatter.getFormattedNow(“EEE, MMM d, ‘year is ->’ yy“));
        System.out.println(“h:mm a –> ”
       + CurrentDateFormatter.getFormattedNow(“h:mm a”));
        System.out.println(“H:mm:ss:S –> ”
       + CurrentDateFormatter.getFormattedNow(“H:mm:ss:S”));
        System.out.println(“K:mm a,z –> ”
       + CurrentDateFormatter.getFormattedNow(“K:mm a,z“));
      

       }
    public static String getFormattedNow(String format) {
        Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ENGLISH);
        return sdf.format(calendar.getTime());

    }
}

The Output of the program;

run:
dd/MM/yyyy HH:mm:ss –> 20/05/2011 16:02:26
MM/dd/yyyy HH:mm:ss –> 05/20/2011 16:02:26
MM/dd/yyyy HH:mm:ss:S –> 05/20/2011 16:02:26:591
yyyy/MM/dd HH:mm:ss –> 2011/05/20 16:02:26
dd.MM.yyyy HH:mm:ss –> 20.05.2011 16:02:26
dd.MM.yy –> 20.05.11
MM/dd/yy –> 05/20/11
dd/MM/yyyy HH:mm –> 20/05/2011 16:02
dd MMMM yyyy –> 20 May 2011
yyyyMMMMdd –> 2011May20
yyMMddHHmmssZ –> 110520160226+0300
hh ‘o”clock’ a, zzzz –> 04 o’clock PM, Eastern European Summer Time
yyyy.MMMMM.dd GGG hh:mm aaa –> 2011.May.20 AD 04:02 PM
EEE, d MMM yyyy HH:mm:ss Z –> Fri, 20 May 2011 16:02:26 +0300
yyyy.MM.dd G ‘@’ hh:mm:ss z –> 2011.05.20 AD @ 04:02:26 EEST
EEE, MMM d, ‘year is ->’ yyFri, May 20, year is -> 11
h:mm a –> 4:02 PM
H:mm:ss:S –> 16:02:26:607
K:mm a,z –> 4:02 PM,EEST
BUILD SUCCESSFUL (total time: 1 second)

Download Source

Advertisements

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