How do I format a date into dd/mm/yyyy?



package org.kodejava.example.text;

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

public class DateFormatExample {
 public static void main(String[] args) {
 Date date = Calendar.getInstance().getTime();

 //
 // Display a date in day, month, year format
 //
 DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
 String today = formatter.format(date);
 System.out.println("Today : " + today);

 //
 // Display date with day name in a short format
 //
 formatter = new SimpleDateFormat("EEE, dd/MM/yyyy");
 today = formatter.format(date);
 System.out.println("Today : " + today);

 //
 // Display date with a short day and month name
 //
 formatter = new SimpleDateFormat("EEE, dd MMM yyyy");
 today = formatter.format(date);
 System.out.println("Today : " + today);

//
 // Formatting date with full day and month name and show time up to
 // milliseconds with AM/PM
 //
 formatter = new SimpleDateFormat("EEEE, dd MMMM yyyy, hh:mm:ss.SSS a");
 today = formatter.format(date);
 System.out.println("Today : " + today);
 }
}</pre>
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
 String strDate = "31-July-2013";
 Date date = formatter.parse(strDate);
 System.out.println(date);
 System.out.println(formatter.format(date));


 //=================================================================
 formatter = new SimpleDateFormat("dd/MM/yyyy");
 strDate = "10/10/1991";
 date = formatter.parse(strDate);
 System.out.println(date);
 System.out.println(formatter.format(date));

 //=================================================================

 formatter = new SimpleDateFormat("MMM dd, yyyy");
 strDate = "July 31, 2013";
 date = formatter.parse(strDate);
 System.out.println(date);
 System.out.println(formatter.format(date));

//=================================================================

 formatter = new SimpleDateFormat("E, MMM dd yyyy");
 strDate = "Wed, July 31 2013";
 date = formatter.parse(strDate);
 System.out.println(date);
 System.out.println(formatter.format(date));

 //=================================================================
 formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a");
 strDate = "Wednesday, July 31, 2013 09:07:50 AM";
 date = formatter.parse(strDate);
 System.out.println(date);
 System.out.println(formatter.format(date));
<pre>

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

%d bloggers like this: