This post contains a total of 13+ Hand-picked Java Clock Examples, all the clocks are made purely using Java Programming Language.
You can use the source codes of these Java Clocks for educational use ( With Credits to Original Owner ), if you want to modify the code and use it for some other use then you can contact the original owner for it.
Related Posts
Click a Code to Copy it, and Hover over a video to play it.
1. By _Sau_rab_h
Java clock by Saurabh. The clock displays the current time in the format of HH/MM/SS. ( Source )
import java.time.LocalTime;
public class Example {
public static void main(String[] args) {
LocalTime currentTime = LocalTime.now(); System.out.println(currentTime);
System.out.println("Format is in Hour Minutes and seconds");
System.out.println("_____________________________________________");
System.out.println("_____________________________________________");
System.out.println("If you like then please give up a upvote ");
}
}
2. By lTheCrow
Made by lTheCrow. The clock displays the current time and the time updates automatically. ( Source )
import java.time.OffsetTime;
import java.util.concurrent.*;
public class ClockThreads
{
public static void main(String[] args) {
Hour time = new Hour();
System.out.println("Executing clock...");
ExecutorService executeSubprocess = Executors.newCachedThreadPool();
executeSubprocess.execute(time);
executeSubprocess.shutdown();
}
}
class Hour implements Runnable{
OffsetTime date = OffsetTime.now();
@Override
public void run(){
//while(true){
date = OffsetTime.now();
System.out.println("Time: " + date.getHour() + "hs " + date.getMinute() + "min " + date.getSecond() + "sec");
System.out.println("Use an IDE to use the while cycle");
try {
Thread.sleep(1000); // We wait 1 second
// I don't want make to explode your computer xdxd
}catch(InterruptedException e){
System.err.println("The process was stopped.");
}
//}
}
}
3. By cHiRaG GhOsH
Made by cHiRaG GhOsH. The clock displays the current Greenwich mean time, it is also styled a little bit using asterisks. ( Source )
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class Program
{
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String s = sdf.format(cal.getTime());
int x=0;
for(int i=1;i<=7;i++)
{
for(int j=1;j<=16;j++)
{
if(i<=2||i>=6||j<=3||j>=14)
System.out.print("*");
else if(i==4&&j>=5&&j<=12)
System.out.print(s.charAt(x++));
else
System.out.print(" ");
}
System.out.println();
}
}
}
4. By Nicholas
Made by Nicholas it shows the current day, date and the current time. ( Source )
import java.util.*;
public class Program
{
public static void main(String[] args) {
Date goodmorning = new Date();
System.out.format("Today is %tA, the %td of %tB. It's %tr", goodmorning , goodmorning , goodmorning , goodmorning );
}
}
5. By Null
Java clock by Null. The clocks shows the date and the time. Change UTF to 8. ( Source )
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public final class Clock {
private static final long min;
private static final long current;
private static final long max;
static {
min = Long.MIN_VALUE;
max = Long.MAX_VALUE;
current = System.currentTimeMillis();
}
public static void main(String[] args) throws Exception {
final OutputStream a = System.out;
a.write(new byte[] {0x43, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x20, 0x54, 0x69, 0x6d, 0x65, 0x3a, 0x20});
a.write(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.getDefault()).format(new Date(current)).getBytes("UTF-8"));
a.flush();
a.close();
}
}
6. By Rodrigi
Made by Rodrigo. It shows you the time and the date. ( Source )
import java.lang.Thread;
import java.util.Calendar;
public class Program
{
public static void main(String[] args) {
Thread t = new Thread();
while(true){
try{
Calendar c = Calendar.getInstance();
System.out.println(c.getTime());
t.sleep(1000);
}catch(Exception e){
}
}
}
}
7. By Yaser Haidar
Made by Yaser Haidar. It displays the current time of Beirut. ( Source )
public class main{
public static void main(String[] args) {
long totalMilliseconds = System.currentTimeMillis();
long totalSeconds = totalMilliseconds / 1000;
long currentSecond = totalSeconds % 60;
long totalMinutes = totalSeconds / 60;
long currentMinute = totalMinutes % 60;
long totalHours = totalMinutes / 60;
long currentHour = totalHours % 24;
System.out.println("Current time is " + (currentHour + 3) + ":" + currentMinute + ":" + currentSecond + " Beirut");
}
}
8. By Pratham Shah
Made by Pratham Shah. It displays the current day, current year, current date and the current time. ( Source )
import java.util.*;
public class Program
{
public static void main(String[] args) {
Date sc =new Date ();
String s=sc.toString();
System.out.println(s.substring (0));
}
}
9. By GreyWolf
JMade by GreyWolf it displays the time using clock hand. The clock hand is a asterisk. ( Source )
import java.io.*;
import java.util.*;
import java.text.*;
public class Program
{
public static void main(String[] args) {
Date d = new Date();
String z = d.toString().charAt(11) + "" + d.toString().charAt(12);
int time = Integer.parseInt(z);
char[] t = new char[12];
for(int i=0;i<12;i++){
t[i]='.';
}
if(time<=11){
t[time]='*';
}else if(time<=23&&time>=12){
t[time-12]='*';
}else{
t[time]='.';
}
System.out.format("-----------------\n|.......0.......|\n|...11.."+t[0]+"...1...|\n|.10."+t[11]+"....."+t[1]+"..2.|\n|.."+t[10]+"..|---|.."+t[2]+"..|\n|9"+t[9]+"...|UTC|..."+t[3]+"3|\n|.."+t[8]+"..|---|.."+t[4]+"..|\n|.8.."+t[7]+"....."+t[5]+"..4.|\n|...7..."+t[6]+"...5...|\n|.......6.......|\n-----------------");
}
}
/*
("
-----------------
|.......0.......|
|...11..*...1...|
|.10.*.....*..2.|
|..*..|---|..*..|
|9*...|---|...*3|
|..*..|---|..*..|
|.8..*.....*..4.|
|...7...*...5...|
|.......6.......|
-----------------
")*/
10. By DallenPL
A basic Java clock by DallenPL. ( Source )
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class currentTime {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
System.out.println( sdf.format(cal.getTime()) );
}
}
11. By Alireza Javadzadeh
Made by Alireza Javadzadeh. It displays the current time and it updates the time automatically. ( Source )
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Program
{
public static void main(String[] args) {
try{
Date date;
while(true){
date = new Date();
String s = String.valueOf(date.getSeconds());
if(Integer.parseInt(s)<10)s="0"+s;
String m = String.valueOf(date.getMinutes());
if(Integer.parseInt(m)<10)m="0"+m;
int h = date.getHours();
String status;
if(h<12)status="A.M";
else{status="P.M";h-=12;}
System.err.println(h+":"+m+":"+s+" "+status);
TimeUnit.SECONDS.sleep(1);
}
}
catch(InterruptedException ex){
Logger.getLogger(Program.class.getName())
.log(Level.SEVERE,null,ex);
}
}
}
12. By Shaik Nabeen
Made by Shaik Nabeen. The clock displays the date, time and day. ( Source )
Mon Jul 11 08:33:06 GMT 2022 11/07/2022 08/33/06 Monday 8:33 AM
import java.util.Date;
import java.text.SimpleDateFormat;
public class Program
{
public static void main(String[] args) {
Date currentdate = new Date();
System.out.println(currentdate);
SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(dateformat.format(currentdate));
SimpleDateFormat timeformat = new SimpleDateFormat("hh/mm/ss");
System.out.println(timeformat.format(currentdate));
SimpleDateFormat weekformat = new SimpleDateFormat("EEEE");
System.out.println(weekformat.format(currentdate));
SimpleDateFormat ttformat = new SimpleDateFormat("h:mm a");
System.out.println(ttformat.format(currentdate));
}
}
13. By Komodo64
Made by Komodo64. ( Source )
Time: 08:34:01
Time: 08:34:02
Time: 08:34:03
Time: 08:34:04
Time: 08:34:05
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
public class SimpleClock
{
private int hour = 0;
private int minute = 0;
private int second = 0;
// Tick the time 5 times faster than a second
// so we don't get a "time limit exceeded" message :-)
private final int delayTime = 1000 / 5;
// We cannot keep the clock running on SoloLearn
// so we end it after 10 seconds.
private int stopCounter = 10;
private boolean signalToStop = false;
public SimpleClock()
{
Timer timer = new Timer();
TimerTask task;
setCurrentTime();
task = new TimerTask() {
@Override
public void run() {
traverseTime();
}
};
timer.schedule(task, 0, delayTime);
}
public void setTime( int hour, int minute, int seconds )
{
// Make sure time format is valid
if (hour >= 0 && hour < 24 &&
minute >= 0 && minute < 60 &&
second >= 0 && second < 60)
{
this.hour = hour;
this.minute = minute;
this.second = second;
}
else
{
System.err.printf("Invalid time format!");
}
}
public void setCurrentTime()
{
Calendar currTime = Calendar.getInstance();
setTime(currTime.get(Calendar.HOUR_OF_DAY),
currTime.get(Calendar.MINUTE),
currTime.get(Calendar.SECOND));
}
public void tick()
{
// Increment second
second++;
if (second == 60) { second = 0; minute++; }
if (minute == 60) { minute = 0; hour++; }
if (hour == 24) { hour = 0; }
stopCounter--;
if (stopCounter == 0)
signalToStop = true;
}
public void traverseTime()
{
tick(); // Tick the clock
showClockTime(); // Display our time
if (signalToStop) // Stop the clock
System.exit(0); // Exit the program
}
//
// Display formatted time.
//
public void showClockTime()
{
System.out.printf("Time: %02d:%02d:%02d%n",
hour, minute, second);
}
//
// Main Entry Point
//
public static void main(String[] argv)
{
new SimpleClock();
}
}
14. By vatsal k
Made by vatsal k. Displays the current date and time. ( Source )
Current date is => 2022-07-11 Current Time is => 8 Hours 35 Minutes 34 Seconds 18153624 Nano Seconds
import java.time.*;
public class Time {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
System.out.println("Current date is => " + date);
System.out.println("Current Time is => " + time.getHour() + " Hours " + time.getMinute() + " Minutes " + time.getSecond() + " Seconds " + time.getNano() + " Nano Seconds ");
}
}