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




Saturday, September 27, 2014

September 28, 2014 Quiz

1. Identify all errors in the following program
/*
Program Exercises1
Attempting to display a frame window
//
import swing.JFrame;
class Exercise 1 {
      public void Mail( ){
           JFrame frame;
           frame.setVisible(TRUE);

    }

}
2. Identify all errors in the following program
//
Program Exercise 2
Attempting to display a frame of size 300 by 200 pixels.
//
import Javax.Swing.*;
class two{
        public static void main method( ){
              myFrame JFrame;
              myFrame = new JFrame( );
              myFrame.setSize(300,200);
              myFrame.setVisible( );
      }
}


3. Identify all errors in the following program
/*
Program Exercise3
Attempting to display the number of characters in a given input.
*/
class three{
     public static void main( ) {
         String input;
        input=JOptionPane("input:");
    JOptionPane.showMessageDialog(null, "Input has " + input.length( ) + "characters");
   }
}


4. For each of these expressions, determine its result. Assume the value of text is a string Java Programming.
String text = "Java Programming";
a.) text.substring(0,4);
b.) text.length( );
c.) text.substring(8,12);
d.) text.substring(0,1) + text.substring(7,9)
e.)text.substring(5,6) + text.substring(text.length()-3,text.length( ))


5.) Write a Java application that displays the two messages I CAN DESIGN AND I CAN PROGRAM, using two separate lines.