Skip to content

Instantly share code, notes, and snippets.

@JonghyuKim
Created March 13, 2017 05:53
Show Gist options
  • Save JonghyuKim/dc7ed87e473c1fb37c1ba38283324ec4 to your computer and use it in GitHub Desktop.
Save JonghyuKim/dc7ed87e473c1fb37c1ba38283324ec4 to your computer and use it in GitHub Desktop.
android TimeCalculater
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
public void testDate(){
System.out.println("Current : " + currentDate());
System.out.println("Diff : " + calculateDiffDate(1489382310900L));
System.out.println("CountTime : " + parseCountTime(40));
System.out.println("CountTime : " + parseCountTime(60 * 1 + 2));
System.out.println("CountTime : " + parseCountTime(60 * 60 * 1));
System.out.println("CountTime : " + parseCountTime(60 * 60 * 1 + 60 * 20 + 30));
}
public String calculateDiffDate(long lastDate) {
Date diffTime = new Date(new Date().getTime() - lastDate);
SimpleDateFormat diffFormat = new SimpleDateFormat("HH:mm:ss");
diffFormat.setTimeZone(TimeZone.getTimeZone ("UTC"));
return diffFormat.format(diffTime);
}
public String currentDate(){
Date time = new Date();
SimpleDateFormat diffFormat = new SimpleDateFormat("HH:mm:ss");
return diffFormat.format(time);
}
public String parseCountTime(int countTime){
String timeString = "";
int tempTime = countTime;
int time = 0;
if((tempTime / (60 * 60)) > 0){
time = tempTime / (60 * 60);
timeString += String.format("%02d", time) + ":";
tempTime = tempTime % (60 * 60);
}
if((tempTime / 60) > 0){
time = tempTime / 60;
timeString += String.format("%02d", time) + ":";
tempTime = tempTime % 60;
}
else{
timeString += "00:";
}
timeString += String.format("%02d", tempTime);
return timeString;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment