Java代碼的如何在java中輪迴一毫秒?
public static long round(long millis , TimeUnit unit){
Calendar calendar = Calendar.getInstance();
switch(unit){
case DAYS:
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 0);
return calendar.getTimeInMillis();
case HOURS:
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
return calendar.getTimeInMillis();
case MINUTES:
calendar.set(Calendar.MILLISECOND , 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTimeInMillis();
case SECONDS:
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
case MILLISECONDS:
default:
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
}
}
目前,我正在使用此代碼一輪毫秒值。在這種方法中,代碼冗餘是一個大問題。
有沒有更好的解決方案或數學公式來計算?