// Today.java // // Applet to display the day of week, month, day, and year based on the current date. // Created November 7, 2012, by Rick Wagner. // // Last updated November 9, 2012 by Rick Wagner. // Version history: // Version 1.01, original applet. // Version 1.02, Simplifies code by using WEEK_OF_YEAR field of Calendar. November 8, 2012. // Version 1.03, Simplifies code by consolidating use of some objects. November 9, 2012. // // Copyright 2012, all rights reserved. import java.applet.*; import java.awt.*; // For Graphics import java.util.*; // For Calendar import java.text.*; // For DateFormat public class Today extends Applet { // Only constructors can run here private String sVersionNumber = "1.03"; // String constructor. private String sCompiledDate = "November 9, 2012"; private Dimension dApplet = null; // Size of the applet (set in HTML code). private String sDate = null; // Date string for writing in a label private String sWeekOfYear = null; // Labels for the GUI: private Label lblDisplay01 = null; // First display label private Label lblDisplay02 = null; // Second display label private Label lblDisplay03 = null; // Third display label private Calendar cToday = Calendar.getInstance(); // Calendar initialized with now information. private final int iWeek = cToday.get(Calendar.WEEK_OF_YEAR); // Week of the year (1 - 52) private Date dtDate = cToday.getTime(); // getTime() returns a Date object for the format() functions private DateFormat df = DateFormat.getDateInstance(); // For the date string, e.g. "Nov 7, 2012" private SimpleDateFormat sdf = null; // For getting the day of the week in string format private String sDateFormat = "EEEE"; // Four E format for day of week //Initialize the applet and set some object instances: public void init() { // Any code can run here System.out.println("'Today' applet version number " + sVersionNumber); // For the Java console System.out.println("Compiled " + sCompiledDate); System.out.println("Copyright 2012 by Rick Wagner, all rights reserved"); // Set the applet background color to blend with light gray (25% = c0c0c0) browser background: setBackground(Color.lightGray); dApplet = getSize(); // So the applet knows how big it is, from the calling HTML code // Convert the week of the year from integer to String: sWeekOfYear = Integer.toString(iWeek); sDate = df.format(dtDate); // sdf = new SimpleDateFormat(sDateFormat); // Four E format (for full word, e.g. "Wednesday") lblDisplay01 = new Label("Today is " + sdf.format(dtDate), Label.CENTER); lblDisplay02 = new Label(sDate, Label.CENTER); lblDisplay03 = new Label("This is week " + sWeekOfYear + " of the year", Label.CENTER); add(lblDisplay01); add(lblDisplay02); add(lblDisplay03); } // End of init() // Code for displaying images or drawing in the applet frame public void paint(Graphics g) { // Draw a recessed frame around the applet border. Designed for gray-on-gray browser background. g.setColor(Color.black); g.drawLine(0, 0, dApplet.width - 1, 0); g.drawLine(0, 0, 0, dApplet.height - 1); g.setColor(Color.white); g.drawLine(0, dApplet.height - 1, dApplet.width - 1, dApplet.height - 1); g.drawLine(dApplet.width - 1, 1, dApplet.width - 1, dApplet.height - 1); } // End of paint() } // End of Applet class Today