Skip to content

Commit

Permalink
Merge pull request #4 from wasifhussain77890/main
Browse files Browse the repository at this point in the history
Digital clock using java
  • Loading branch information
adityaSrivastava29 committed Oct 17, 2022
2 parents 6f58fc6 + d20fa59 commit 467da58
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 0 deletions.
11 changes: 11 additions & 0 deletions JAVA/Digital Clock Using Java/Digital Clock.iml
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
6 changes: 6 additions & 0 deletions JAVA/Digital Clock Using Java/README.md
@@ -0,0 +1,6 @@
This program outputs a digital clock . To run this code just copy this
folder to your pc and go to Digital Clock Using Java\src\com\practice and run Main.java in any
java compiler (e.g.聽intellij)

Tadaa , your output will be on the screen showing current time in
digital format
Binary file not shown.
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions JAVA/Digital Clock Using Java/src/com/practice/Main.java
@@ -0,0 +1,8 @@
package com.practice;

public class Main {
public static void main(String[] args) {
System.out.println("Hello");
MyWindow w = new MyWindow();
}
}
72 changes: 72 additions & 0 deletions JAVA/Digital Clock Using Java/src/com/practice/MyWindow.java
@@ -0,0 +1,72 @@
package com.practice;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MyWindow extends JFrame {
private JLabel heading;
private JLabel clockLabel;
private Font font = new Font("",Font.BOLD,35);
MyWindow(){
super.setTitle("My Clock");
super.setSize(400,400);
super.setLocation(300,50);
this.createGUI();
this.startClock();
super.setVisible(true);
}
public void createGUI(){

heading = new JLabel("My Clock");
clockLabel = new JLabel("clock");

heading.setFont(font);
clockLabel.setFont(font);

this.setLayout(new GridLayout(2,1));

this.add(heading);

this.add(clockLabel);
}
public void startClock(){
// Timer timer = new Timer(1000, new ActionListener() {
// @Override
// public void actionPerformed(ActionEvent e) {
// //String dateTime = new Date().toString();
// //String dateTime = new Date().toLocaleString();
// Date d = new Date();
//
// SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a");
// String dateTime = sdf.format(d);
//
// clockLabel.setText(dateTime);
// }
// });
// timer.start();

Thread t = new Thread(){
public void run(){
try{
while(true){
Date d = new Date();

SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a");
String dateTime = sdf.format(d);

clockLabel.setText(dateTime);

//Thread.sleep(1000);
Thread.currentThread().sleep(1000);
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
};
t.start();
}
}

0 comments on commit 467da58

Please sign in to comment.