14 January 2011

In Java format a date into dd/mm/yyyy and other date formats....

/* **********************************************************************************
Formatting a display is a common requirement when creating a program. Information in a good format can be seen as an added value to the user of the program. Using Java SimpleDateFormat we can easily format a date in our program.
********************************************************************************* */


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);
   }
}


//Out Put....
Today : 02/12/2007
Today : Sun, 02/12/2007
Today : Sun, 02 Dec 2007
Today : Sunday, 02 December 2007, 08:03:17.828 AM /* **********************************************************************************
Formatting a display is a common requirement when creating a program. Information in a good format can be seen as an added value to the user of the program. Using Java SimpleDateFormat we can easily format a date in our program.
********************************************************************************* */


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);
   }
}


//Out Put....
Today : 02/12/2007
Today : Sun, 02/12/2007
Today : Sun, 02 Dec 2007
Today : Sunday, 02 December 2007, 08:03:17.828 AM

No comments:

Post a Comment

Thank you.