/******************************************************************************
* Example of formatting dates using SimpleDateFormat class
* You can format using various patterns                    
******************************************************************************/
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;

public class SimpleDateFormat1 {

    private static void prt() {
        System.out.println();
    }
    private static void prt(String s) {
        System.out.println(s);
    }

    public static void main(String[] args) {

        //To create a Date object for the current date and time 

        prt("Current date and time");
        prt("======================================================================");
        Date now = new Date();
        prt("new Date().............: " + now);                 //now.toString()
        prt();

        //Create a date using a different format

        prt("Create a date using a different format");
        prt("======================================================================");
        String           stringDate1 = "2010.06.14 22:30:45";
        SimpleDateFormat pattern1    = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss");
        Date date1 = null;

        try {
            date1 = pattern1.parse(stringDate1);
        } 
        catch (ParseException e) 
        {
            prt(stringDate1 + " Unparseable using : " + pattern1);
        }    

        prt("Creating using format..: " + stringDate1);                 //stringDate.toString()
        prt("The date created is....: " + date1);                       //stringDate.toString()
        prt();

        //Print a date using a different format

        prt("Print a date using a different format");
        prt("======================================================================");
        Date date2 = new Date();
        SimpleDateFormat pattern2    = new SimpleDateFormat("E yyyy/MM/dd 'at' hh:mm:ss-a zzz Z");
        String           stringDate2 = pattern2.format(date2); 
        prt("Print in a diff format.: " + stringDate2);
        prt();

        prt("Print a date using a different format");
        prt("======================================================================");
        Date date3 = new Date();
        SimpleDateFormat pattern3    = new SimpleDateFormat();
        String           stringDate3 = pattern3.format(date3); 
        prt("Print in a diff format.: " + stringDate3);
        prt();

        prt("The formats are:");
        prt("y-year, M-month, d-day, H-hour24, m-minute, s-second, S-millisecond");
        prt("w-week in year, W-week in month, D-day in year, E-day of week");
        prt("h-hour12, a-AM/PM, z-time zone string, Z-time zone offset");
        prt();

        datePrint(date2);                                       //call the datePrint method blow        
    }

    //--------------------------------------------------------------------------------------------------
    //Convert and Print all Date Fields
    //--------------------------------------------------------------------------------------------------
    public static void datePrint(Date d)
    {
        prt("Convert portion of a Date object into a string");
        prt("======================================================================");
        SimpleDateFormat pattern;
        pattern        = new SimpleDateFormat("yy");        //yy - as 09,10,11,...
        String year2   = pattern.format(d); 
        pattern        = new SimpleDateFormat("yyyy");      //yyyy - as 2009,2010,...
        String year4   = pattern.format(d); 
        pattern        = new SimpleDateFormat("M");         //M - as 1,2,3,...          
        String month1  = pattern.format(d); 
        pattern        = new SimpleDateFormat("MM");        //MM - as 01,02,03,...          
        String month2  = pattern.format(d); 
        pattern        = new SimpleDateFormat("MMM");       //MMM - as Jan,Feb,Mar,.. 
        String month3  = pattern.format(d); 
        pattern        = new SimpleDateFormat("MMMM");      //MMMM - as January,... 
        String month4  = pattern.format(d); 
        pattern        = new SimpleDateFormat("d");
        String day1    = pattern.format(d); 
        pattern        = new SimpleDateFormat("dd");
        String day2    = pattern.format(d); 
        pattern        = new SimpleDateFormat("E");         //E - as Sun,Mon,Tue,...
        String day_nm1 = pattern.format(d); 
        pattern        = new SimpleDateFormat("EEEE");      //EEEE - as Sunday,...
        String day_nm2 = pattern.format(d); 
        pattern        = new SimpleDateFormat("HH");        //HH - as hour24
        String hour24  = pattern.format(d); 
        pattern        = new SimpleDateFormat("hh");        //hh - as hour12
        String hour12  = pattern.format(d); 
        pattern        = new SimpleDateFormat("mm");        
        String minute  = pattern.format(d); 
        pattern        = new SimpleDateFormat("ss");
        String second  = pattern.format(d); 
        pattern        = new SimpleDateFormat("a");         //a - as AM/PM
        String am_pm   = pattern.format(d); 
        prt("The year numbr as yy...: " + year2);
        prt("The year numbr as yyyy.: " + year4);
        prt("The month numb as M....: " + month1);
        prt("The month numb as MM...: " + month2);
        prt("The month name as MMM..: " + month3);
        prt("The month name as MMMM.: " + month4);
        prt("The day number as d....: " + day1);
        prt("The day number as dd...: " + day2);
        prt("The day name   as E....: " + day_nm1);
        prt("The day name   as EEEE.: " + day_nm2);
        prt("The time in 24 hours is: " + hour24 +":"+ minute +":"+ second);
        prt("The time in 12 hours is: " + hour12 +":"+ minute +":"+ second +" "+ am_pm);
        prt();
    }       
}