Java Reference
In-Depth Information
If you look at the Java 2 API Specifications for the Date class you will
see that most of its methods are deprecated. As of version 1.1, java.text.
DateFormat took over most of duties of the Date class, which is
now essentially relegated to simply holding a date/time value. The class
DateFormat can generate strings with many variations in the formats for
dates and times. There are also a number of methods for dealing with time zone
settings and internationalization of the strings.
The class DateFormatPanel , shown below, displays the current time. Each
time the panel is repainted, it displays the current time. In Section 8.7 we discussed
how to use timers to redraw this panel every second to create a digital clock.
import javax.swing.*;
import java.awt.*;
import java.text.*;
import java.util.*;
/** This JPanel subclass uses the DateFormat class
*todisplay the current time. **/
class DateFormatPanel extends JPanel {
DateFormat fDateFormat;
boolean fFirstPass = true;
int fMsgX = 0, fMsgY = 0;
Font fFont = new Font ("Serif", Font.BOLD, 24);
/** Get the DateFormat object with the default time
* style. **/
DateFormatPanel () {
fDateFormat =
DateFormat.getTimeInstance (DateFormat.DEFAULT);
} // ctor
/** Draw the time string on the panel center. **/
public void paintComponent (Graphics g) {
super.paintComponent (g);
// Get current date object
Date now = new Date ();
// Format the time string.
String date - out = fDateFormat.format (now);
// Use our choice for the font.
g.setFont (fFont);
// Do the size and placement calculations only for
 
Search WWH ::




Custom Search