
备注:
精度最大为时,最小为毫秒
方式三并未实现
import java.time.Duration;
import java.time.Instant;
public class MyTime {
public static void main(String[] args) throws Exception{
Instant int1 = Instant.now();
Long start = System.currentTimeMillis();
String res = "";
String res2 = "";
while (true){
/**
* 方式一
*/
Duration du = Duration.between(int1, Instant.now());
if (du.toHours()>0){
res = du.toHours()+"时"+(du.toMinutes()-du.toHours()*60)+"分"+(du.getSeconds()-du.toMinutes()*60+"秒")+(du.toMillis()-du.getSeconds()*1000)+"毫秒";
} else if (du.toMinutes()>0) {
res = (du.toMinutes()-du.toHours()*60)+"分"+(du.getSeconds()-du.toMinutes()*60+"秒")+(du.toMillis()-du.getSeconds()*1000)+"毫秒";
}else if (du.getSeconds()>0) {
res = du.getSeconds()+"秒"+(du.toMillis()-du.getSeconds()*1000)+"毫秒";
}else {
res =du.toMillis()+"毫秒";
}
System.out.println("Duration====>"+res);
Thread.sleep(5347);
/**
* 方式二
*/
Long end = System.currentTimeMillis();
if ((end-start)>=60*60*1000){
long h = (end-start)/(1000*60*60);
long m = (end-start)/(1000*60)-h*60;
long s = (end-start)/1000-h*60*60-m*60;
long ms = (end-start) - (h*60*60+m*60+s)*1000;
res2 = h+"时"+m+"分"+s+"秒"+ms+"毫秒";
} else if ((end-start)>=60*1000) {
long m = (end-start)/(1000*60);
long s = (end-start)/1000-(m*60);
long ms = (end-start)-(m*60+s)*1000;
res2 = m+"分"+s+"秒"+ms+"毫秒";
} else if ((end-start)>=1000) {
long s = (end-start)/1000;
long ms = (end-start)-s*1000;
res2 = s+"秒"+ms+"毫秒";
} else {
// end-start < 1000
res2 = (end-start)+"毫秒";
}
System.out.println("System.currentTimeMillis()====>"+res2);
}
}
}

