Home | Java Menu | Java Applets | Java Tips | E-mail Us
View Java Applet


/* DateTime2.java
  *
  * A Java Application and Applet in one demonstrating different ways of
  * getting the date, time, and day of the week.
  *
  * Khim Theng
  * khim_theng@completeesolutions.com
  *
  * Last modified on 05/16/2000
  */

import java.applet.Applet;
import java.awt.*;
import java.util.*;  //Used for Date and Calendar
import java.text.*;  //Used for DateFormat and TimeFormat
import java.awt.event.*;  //Needed for WindowAdapter and Listener

public class DateTime2 extends Applet
{
    private static Date now;  //Date variable

    //Add a main method to enable the applet to run as an application
    public static void main(String[] args)
    {
        Frame f = new Frame();  //Create a new frame object

        DateTime2 dateTime = new DateTime2();  //Create an instance of DateTime2

        //Add the new instance into the frame
        f.setLayout(new BorderLayout());
        f.add("Center", dateTime);
        f.setSize(200,255);
        f.setVisible(true);

        //An "anonymous" inner class used to close the window
        f.addWindowListener( new WindowAdapter()
           {
               public void windowClosing(WindowEvent we)
               {
                   System.exit(0);
               }
           }
        );

       dateTime.init();  //Call the applet's init method
    }  //End of main

    public void paint(Graphics g)
    {
        Calendar today;  //Calendar variable for day of the week
        String dateTimeString;  //String variable for date and time
        String dateString;  //String variable for date
        String timeString;  //String variable for time
        String dayOfWeekString;  //String variable for day of the week

        int positionX = 20;  //Variable to hold the x coordinate
        int positionY = 20;  //Variables to hold the y coordinate

        setBackground(Color.white);  //Set the background of the applet to white
        now = new Date();  //Set Date variable now to the current date and time
        dateTimeString = DateFormat.getDateTimeInstance().format(now);  //Command to format both the date and time
        dateString = DateFormat.getDateInstance().format(now);  //Command to format the date
        timeString = DateFormat.getTimeInstance().format(now);  //Command to format the time

        today = new GregorianCalendar();  //Create a new Gregorian Calendar
        today.setTime(now);  //Set the time to the present
        String weekDay[] = new DateFormatSymbols().getWeekdays();  //Capture the days of the week in an array
        dayOfWeekString = weekDay[today.get(Calendar.DAY_OF_WEEK)];  //Get the current day of the week

        g.setColor(Color.black);  //Set the forground color to black
        g.drawString("Display the date and time:", positionX, positionY);
        positionY+=20;  //Increment position y by 20

        g.setColor(Color.blue);  //Set the forground color to blue
        g.drawString(dateTimeString, positionX, positionY);  //Draw the datetime string at position x and y
        positionY+=40;

        g.setColor(Color.black);
        g.drawString("Display the date separately:", positionX, positionY);
        positionY+=20;

        g.setColor(Color.blue);
        g.drawString(dateString, positionX, positionY);  //Draw the date string at position x and y
        positionY+=40;

        g.setColor(Color.black);
        g.drawString("Display the time separately:", positionX, positionY);
        positionY+=20;

        g.setColor(Color.blue);
        g.drawString(timeString, positionX, positionY);  //Draw the time string at position x and y
        positionY+=40;

        g.setColor(Color.black);
        g.drawString("Display the day of the week:", positionX, positionY);
        positionY+=20;

        g.setColor(Color.blue);
        g.drawString(dayOfWeekString, positionX, positionY);  //Draw the dayOfWeek string at position x and y
    }  //End of paint

}  //End of DateTime2
                
View Java Applet

Home | Java Menu | Java Applets | Java Tips | Legal
© 1999-2003 Complete e Solutions, Inc.