1
+ import javax .swing .*;
2
+ import java .awt .*;
3
+ import java .text .SimpleDateFormat ;
4
+ import java .util .Calendar ;
5
+
6
+ public class Clock extends JFrame {
7
+
8
+ Calendar calendar ;
9
+ SimpleDateFormat timeFormat ;
10
+ SimpleDateFormat dayFormat ;
11
+ SimpleDateFormat dateFormat ;
12
+
13
+ JLabel timeLabel ;
14
+ JLabel dayLabel ;
15
+ JLabel dateLabel ;
16
+ String time ;
17
+ String day ;
18
+ String date ;
19
+ Clock () {
20
+ this .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
21
+ this .setTitle ("Java Digital Clock" );
22
+ this .setLayout (new FlowLayout ());
23
+ this .setSize (350 , 300 );
24
+ this .setResizable (false );
25
+
26
+ timeFormat = new SimpleDateFormat ("hh:mm:ss a" );
27
+ dayFormat =new SimpleDateFormat ("EEEE" );
28
+ dateFormat =new SimpleDateFormat ("MMMMM, yyyy" );
29
+ timeLabel = new JLabel ();
30
+ timeLabel .setFont (new Font ("SANS_SERIF" , Font .PLAIN , 59 ));
31
+ timeLabel .setBackground (Color .CYAN );
32
+ timeLabel .setForeground (Color .BLACK );
33
+ timeLabel .setOpaque (true );
34
+ dayLabel =new JLabel ();
35
+ dayLabel .setFont (new Font ("SANS_SERIF" ,Font .BOLD ,34 ));
36
+
37
+ dateLabel =new JLabel ();
38
+ dateLabel .setFont (new Font ("SANS_SERIF" ,Font .BOLD ,30 ));
39
+
40
+
41
+ this .add (timeLabel );
42
+ this .add (dayLabel );
43
+ this .add (dateLabel );
44
+ this .setVisible (true );
45
+
46
+ setTimer ();
47
+ }
48
+
49
+ public void setTimer () {
50
+ while (true ) {
51
+ time = timeFormat .format (Calendar .getInstance ().getTime ());
52
+ timeLabel .setText (time );
53
+
54
+ day = dayFormat .format (Calendar .getInstance ().getTime ());
55
+ dayLabel .setText (day );
56
+
57
+ date = dateFormat .format (Calendar .getInstance ().getTime ());
58
+ dateLabel .setText (date );
59
+
60
+ try {
61
+ Thread .sleep (1000 );
62
+ } catch (Exception e ) {
63
+ e .getStackTrace ();
64
+ }
65
+ }
66
+ }
67
+ public static void main (String [] args ) {
68
+ new Clock ();
69
+ }
70
+ }
0 commit comments