forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDigitalClock.java
74 lines (59 loc) · 1.92 KB
/
DigitalClock.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//Digital Clock mini project
import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Clock extends JFrame {
Calendar calendar;
SimpleDateFormat timeFormat;
SimpleDateFormat dayFormat;
SimpleDateFormat dateFormat;
JLabel timeLabel;
JLabel dayLabel;
JLabel dateLabel;
String time;
String day;
String date;
Clock() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Digital Clock");
this.setLayout(new FlowLayout());
this.setSize(350, 220);
this.setResizable(false);
timeFormat = new SimpleDateFormat("hh:mm:ss a");
dayFormat=new SimpleDateFormat("EEEE");
dateFormat=new SimpleDateFormat("dd MMMMM, yyyy");
timeLabel = new JLabel();
timeLabel.setFont(new Font("SANS_SERIF", Font.PLAIN, 59));
timeLabel.setBackground(Color.BLACK);
timeLabel.setForeground(Color.WHITE);
timeLabel.setOpaque(true);
dayLabel=new JLabel();
dayLabel.setFont(new Font("Ink Free",Font.BOLD,34));
dateLabel=new JLabel();
dateLabel.setFont(new Font("Ink Free",Font.BOLD,30));
this.add(timeLabel);
this.add(dayLabel);
this.add(dateLabel);
this.setVisible(true);
setTimer();
}
public void setTimer() {
while (true) {
time = timeFormat.format(Calendar.getInstance().getTime());
timeLabel.setText(time);
day = dayFormat.format(Calendar.getInstance().getTime());
dayLabel.setText(day);
date = dateFormat.format(Calendar.getInstance().getTime());
dateLabel.setText(date);
try {
Thread.sleep(1000);
} catch (Exception e) {
e.getStackTrace();
}
}
}
public static void main(String[] args) {
new Clock();
}
}