所以我試圖將時間從納秒轉換爲分鐘。 我的代碼看起來像這樣:爲什麼TimeUnit.convert()不能在Java中正確轉換?
long startTime=System.nanoTime();
//some code...
long endTime=System.nanoTime();
long estimatedTime=endTime-startTime;
long estimatedTime2=TimeUnit.MINUTES.convert(estimatedTime, TimeUnit.NANOSECONDS);
System.out.println(estimatedTime2 +","+estimatedTime);
輸出:
estimatedTime is always in nanoseconds
estimatedTime2 is always 0;
根據的JavaDoc - TimeUnit:
public long convert(long sourceDuration, TimeUnit sourceUnit)
想返回long
到estimatedTime2的方法。
什麼是使用此轉換類型的正確形式?
謝謝你!
[MCVE]或[SSCCE ](http://sscce.org)以及觀察和預期的行爲描述。 –
estimatedTime2始終爲0的可能性可能是由於計算的估計時間爲0,因爲在startTime和endTime之間沒有足夠的時間已過去 –
在您嘗試解決估計的時間2並打印出預計的時間之前打印出估計的時間 –