Skip to content

Instantly share code, notes, and snippets.

@jukbot
Created January 22, 2015 08:47
Show Gist options
  • Save jukbot/2ca50e8eab837621f40b to your computer and use it in GitHub Desktop.
Save jukbot/2ca50e8eab837621f40b to your computer and use it in GitHub Desktop.
Display time with interval 1 second loop
import java.util.Date; //import Date class
class GetLoopTime {
public static void main (String [] args) {
while (true) { // Run infinity print
try {
Date now = new Date();
String ab;
switch (Integer.parseInt(String.format("%te",now))%10) { // Find case of some days that ended with 1, 2, or 3
case 1 : ab = "st";
break;
case 2 : ab = "nd";
break;
case 3 : ab = "rd";
break;
default : ab = "th";
}
System.out.printf("Today is: %te%2s of %tB in %tY at %tr\n", now, ab, now, now, now); // Print each line
Thread.sleep(1000); //stop the program for 1000 milliseconds
} catch (Exception e) {
System.out.println("Exception Error!"); // print when the program has an error
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment