Skip to content

Instantly share code, notes, and snippets.

@hadinajafi
Created June 27, 2019 10:30
Show Gist options
  • Save hadinajafi/10c59e0341bd7a2430755c8472cfaf03 to your computer and use it in GitHub Desktop.
Save hadinajafi/10c59e0341bd7a2430755c8472cfaf03 to your computer and use it in GitHub Desktop.
A Simple countdown timer to print time in "mm:ss" format.
int minutes = insertedValue; //init value
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
int seconds = minutes * 60;
String secString, minString; //two strings for printing
int ss = 0; //formatted second
int mm = minutes; //formatted minute
public void run(){
if(ss < 10)
secString = "0" + ss; //[0s] format: 01, 02, ... 09
else
secString = String.valueOf(ss);
if(mm < 10)
minString = "0" + mm; //[0s] format: 01, 02, ... 09
else
minString = String.valueOf(mm);
System.out.println(minString + ":" + secString);
seconds--;
mm = seconds/60;
ss = seconds - mm*60;
if(seconds < 0)
timer.cancel();
}
}, 0, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment