System.currentTimeMillis 可以給出從 1970.1.1 0點到現(xiàn)在的毫秒數(shù)(格林威治時間),返回值類型是 long。問題是需要根據(jù)這個毫秒數(shù)來計算一下現(xiàn)在的時間(小時,分鐘,秒數(shù))
本來這個問題不難,但是我忘記了直接可以取余,我使用了一個類似于貪心的思想,寫了三個 while 循環(huán)來計算。
既然有兩種方法就順便總結(jié)一下這兩種方法,對于取余來說很適合不必關(guān)心做了多少重復(fù)工作,只關(guān)注最后一次的情況。
而使用循環(huán)可以知道做了多少次重復(fù)工作。
public class lianxi2 {
public static void main(String[] args) {
long timeMillis = System.currentTimeMillis();
long totleSeconds = timeMillis/1000;
long second = totleSeconds%60;
long totleMinutes = totleSeconds/60;
long minute = totleMinutes%60;
long totleHour = totleMinutes/60;
long hour = totleHour%24;
System.out.println((hour+8)+":"+minute);
}
}