Skip to content

Commit 6eb4c30

Browse files
authored
Merge pull request #310 from HaripriyaB/digitalwatchjava
Digital watch using Java
2 parents d7c431e + 2fba843 commit 6eb4c30

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import javax.swing.*;
4+
import java.awt.*;
5+
import java.text.*;
6+
7+
class DigitalWatch implements Runnable {
8+
// Initialise all the components of the application window
9+
JFrame f;
10+
Thread t = null;
11+
int hours = 0, minutes = 0, seconds = 0;
12+
String timeString = "";
13+
String timePeriod = "";
14+
JButton b;
15+
16+
DigitalWatch() {
17+
// frame for outliner
18+
f = new JFrame();
19+
//thread to execute time change
20+
t = new Thread(this);
21+
t.start();
22+
// Display of time on a button for better UI
23+
b = new JButton();
24+
b.setBounds(100, 100, 100, 50);
25+
26+
f.add(b);
27+
f.setSize(300, 300);
28+
f.setLayout(null);
29+
f.setVisible(true);
30+
}
31+
32+
public void run() {
33+
try {
34+
while (true) {
35+
// hours is checked if > 12 to change the time-period of the day(AM/PM)
36+
Calendar cal = Calendar.getInstance();
37+
hours = cal.get(Calendar.HOUR_OF_DAY);
38+
if (hours > 12) {
39+
hours -= 12;
40+
timePeriod = "PM";
41+
}
42+
else{
43+
timePeriod = "AM";
44+
}
45+
minutes = cal.get(Calendar.MINUTE);
46+
seconds = cal.get(Calendar.SECOND);
47+
// SimpleDateFormat used to get customized time format
48+
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");
49+
Date date = cal.getTime();
50+
//Time-period is appended with the time
51+
timeString = formatter.format(date) +" "+ timePeriod;
52+
printTime();
53+
// interval given in milliseconds(1 sec = 1000 ms)
54+
t.sleep(1000);
55+
}
56+
} catch (Exception e) {
57+
}
58+
}
59+
60+
public void printTime() {
61+
b.setText(timeString);
62+
}
63+
64+
public static void main(String[] args) throws Exception {
65+
// Watch initialized
66+
new DigitalWatch();
67+
}
68+
}

Java/DigitalWatchJava/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Simple Digital Watch written in Java
2+
The aim of this program is to display the current running time in digital format using Java swing and awt libraries. The output of this program open as new window which shows the current time and time-period(AM/PM).
3+
4+
## Libraries Used:
5+
* [Java Swing](https://www.javatpoint.com/java-swing) - used to create window-based applications, built on the top of AWT API and entirely written in java.
6+
* [Java AWT(Abstract Windowing Toolkit)](https://www.javatpoint.com/java-awt) - an API to develop GUI or window-based applications in java.
7+
* [Java Text](http://web.deu.edu.tr/doc/oreily/java/fclass/ch16_js.htm) - used for Date format in this program.
8+
9+
## Usage:
10+
`>> javac DigitalWatch.java`
11+
12+
`>> java DigitalWatch`
13+
14+
A window pops up showing the digital watch.
15+
16+
## Output:
17+
<img src="output.png" width = 300 height=300>

Java/DigitalWatchJava/output.png

27.7 KB
Loading

0 commit comments

Comments
 (0)