Java Reference
In-Depth Information
Here the last number,
0500, indicates a time zone 5 hours earlier than GMT.
The
JPanel
subclass
SimpleDateFormatPanel
shown below displays the
date and time whenever the panel is repainted:
−
import java.swing.*;
import java.awt.*;
import java.text.*;
import java.util.*;
public class
SimpleDateFormatPanel
extends JPanel
{
SimpleDateFormat fSDateFormat;
boolean fFirstPass = true;
int fMsgX = 0, fMsgY = 0;
Font fFont = new Font ("Serif", Font.BOLD, 24);
/** Use SimpleDateFormat with the given date & time
* style. **/
SimpleDateFormatPanel ()
{
fSDateFormat
=
new SimpleDateFormat (
"
EEE, d MMM yyyy HH:mm:ss Z
"
);
}
/** Paint the date and time at center of panel. **/
public void paintComponent (Graphics g)
{
super.paintComponent (g);
// Get current date
Date now = new Date ();
// And create a date/time string in the desired format
String date
-
out = fSDateFormat.format (now);
...
rest of the code is the same as in
DateFormatPanel...
Another time related tool is the static method
System.currentTime-
Millis()
.Itprovides the current time as a
long
value containing the number
of milliseconds since midnight, January 1, 1970 UTC. This is not very informa-
tive on its own but usually you look at the difference between two values. It is
particularly useful for program timing and performance studies.
10.13 Arbitrary precision numbers
The 64 bits in the
long
type correspond to about 19 decimal digits. There are
applications, however, that deal with numbers containing many more digits than
Search WWH ::
Custom Search