1.前言
工作中通常会将时间转化为几天几时几分几秒的格式,撸了一个工具类,懒得每次都使用一样的代码
public class FormatTime {
public static final int MUNITE_MODE = 1;
public static final int HOUR_MODE = 2;
public static final int DAY_MODE = 3;
private int mMode = MUNITE_MODE;
private long mTotalTimeSeconde=0;
public long res_day;
public long res_hour;
public long res_munite;
public long res_second;
public FormatTime setMode(int mode) {
mMode = mode;
return this;
}
public FormatTime setTimemillisecond(long totalTime) {
mTotalTimeSeconde = totalTime / 1000;
return this;
}
public FormatTime setTimeSecond(long totalTime) {
mTotalTimeSeconde = totalTime;
return this;
}
public FormatTime calculate() {
switch (mMode) {
case MUNITE_MODE:
toMunite(mTotalTimeSeconde);
break;
case HOUR_MODE:
toHour(mTotalTimeSeconde);
break;
case DAY_MODE:
toDay(mTotalTimeSeconde);
break;
}
return this;
}
/**
* 转化为 几分几秒
*/
private void toMunite(long totalTime) {
res_munite = totalTime / 60;
res_second = totalTime % 60;
}
/**
* 转化为 几时几分几秒
* @return
*/
private void toHour(long totalTime) {
res_hour = totalTime / 60 / 60;
res_munite = totalTime / 60 % 60;
res_second = totalTime % 60;
}
/**
* 转化为 几天几时几分几秒
* @return
*/
private void toDay(long totalTime) {
res_day = totalTime / (60 * 60 * 24);
res_hour = (totalTime % (60 * 60 * 24)) / (60 * 60);
res_munite = (totalTime % (60 * 60)) / (60);
res_second = (totalTime % (60));
}
}
2.使用方式(非常装逼的还是流式API哦)
mFormatTime=new FormatTime();
mFormatTime
.setTimemillisecond(millisUntilFinished)
.setMode(mMode)
.calculate();
long day=mFormatTime.res_day;
long munite=mFormatTime.res_munite;
long second=mFormatTime.res_second;