Skip to content

Instantly share code, notes, and snippets.

@timmolderez
Created November 17, 2016 10:19
Show Gist options
  • Save timmolderez/534f4eb106d03012fb32e14477e6dfa1 to your computer and use it in GitHub Desktop.
Save timmolderez/534f4eb106d03012fb32e14477e6dfa1 to your computer and use it in GitHub Desktop.
Design patterns exercise
import java.text.SimpleDateFormat;
import java.util.Date;
public class Clock implements Runnable {
public static final int TIME_24 = 0;
public static final int TIME_US = 1;
public static final int TIME_UNIX = 2;
private int displayType = TIME_24;
public static void main(String[] args) {
Clock c = new Clock (); c.setDisplayType(TIME_24);
new Thread(c).start();
// After a few seconds, switch to a different display type
try {Thread.sleep(3000);} catch (InterruptedException e) {}
c.setDisplayType(TIME_UNIX); }
public void setDisplayType(int dt) {
displayType = dt;
}
public void run() {
while (true) {
String time = "";
if (displayType==TIME_24) {
time = new SimpleDateFormat("HH:mm:ss").format(new Date());
} else if (displayType==TIME_US) {
time = new SimpleDateFormat("hh:mm:ss a").format(new Date());
} else if (displayType==TIME_UNIX) {
time = String.valueOf(System.currentTimeMillis() / 1000L);
}
System.out.println(time);
try {Thread.sleep(1000);} catch (InterruptedException e) {}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment