Sunday, September 28, 2014

Chapter 2: Date and SimpleDateFormat

Date today;
today = new Date( );

JOptionPane.showMessageDialog(null, today.toString( ));

will display this current time format:
===========================
OUTPUT: Sun Sept 28 15:05:18 PDT 2014

----------------------------------------------------

If we want to display the month, day, and year in the MM/dd/YY shorthand format, such as 07/04/2013, we write


Date      today;
SimpleDateFormat    sdf;

today = new Date( );
sdf = new SimpleDateFormat("MM/dd/yy");

JOptionPane.showMessageDialog(null, sdf.format(today));
==================================
OUTPUT:
09/28/2014
----------------------------------------------------------
If we want to display September 28, 2014

Date      today;
SimpleDateFormat    sdf;

today = new Date( );
sdf = new SimpleDateFormat("MMMM dd,yyyy");

JOptionPane.showMessageDialog(null, sdf.format(today));
==================================
OUTPUT:
September 28, 2014
-------------------------------------------------------

If we want to display which day of the week today is, we can use the letter E as in

Date      today;
SimpleDateFormat    sdf;

today = new Date( );
sdf = new SimpleDateFormat("EEEE");

JOptionPane.showMessageDialog(null, sdf.format(today));
==================================
OUTPUT:
Sunday
-------------------------------------------------------
Show the output of this code:

   GregorianCalendar cal= new GregorianCalendar();
        
        System.out.print(cal.getTime());
        System.out.println(" ");
        System.out.println("\n YEAR:" + cal.get(Calendar.YEAR));
        System.out.println("\n MONTH:" + cal.get(Calendar.MONTH));
        System.out.println("\n DATE:" + cal.get(Calendar.DATE));
        System.out.println("\n Day of Year:" + cal.get(Calendar.DAY_OF_YEAR));
        System.out.println("\n Day of Month:" + cal.get(Calendar.DAY_OF_MONTH));
        System.out.println("\n Day of Week:" + cal.get(Calendar.DAY_OF_WEEK));
        System.out.println("\n Week of Month:" + cal.get(Calendar.WEEK_OF_MONTH));
        System.out.println("\n AM_PM:" + cal.get(Calendar.AM_PM));
        System.out.println("\n HOUR:" + cal.get(Calendar.HOUR));
        System.out.println("\n HOUR of Day:" + cal.get(Calendar.HOUR_OF_DAY));
        System.out.println("\n Minute:" + cal.get(Calendar.MINUTE));




No comments:

Post a Comment