How to change the format of Date in Java?

Here is DateFormatter.java file:

import java.util.*;
import java.text.*;

class DateFormatter {
   public static void main(String args[]) {

       Date dNow = new Date( );
       SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");

       System.out.println("Current Date: " + ft.format(dNow));
   }
}

SimpleDateFormat is the predefined function in java. In this program, firstly current date is put in date variable dNow using Date() function. Specify date format in variable ft using SimpleDateFormat() function.

y is for year, M is for month, d is for day. Remember m is not for month. Its for minutes.

In the end, apply the format on the current date.

To compile the program:

$ javac DateFormatter.java

To run the program:

$ java DateFormatter

For more reference ,see:

http://www.tutorialspoint.com/java/java_date_time.htm

Advertisements

About Priyanka Kapoor

Simple, Hardworking & friendly.....
This entry was posted in Uncategorized. Bookmark the permalink.

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