我想在我的項目中使用喬達時間。我很想知道如果我有一個好主意如何使用它。首先 - 我想創建一個進度條,然後每秒鐘計算其值,然後顯示完成過程的剩餘時間。喬達時間複雜操作準時
在初始化期間,我只有格式爲「HH:mm:ss」的字符串(偶爾「D天HH:mm:ss」)表示剩餘時間和百分比 - 進度條的初始狀態。我就是這樣。
現在我想創建一個表示任務完成時刻的DateTime對象。
PeriodFormatter timeFormatter = new PeriodFormatterBuilder()
.appendHours().appendSeparator(":").appendMinutes()
.appendSeparator(":").appendSeconds().toFormatter();
DateTime endDate_ = new Date();
Period periodLeft = null;
String[] parsedInput = timeLeft.split(" ");
if (parsedInput != null) {
switch (parsedInput.length) {
case 1: {
periodLeft = timeFormatter.parsePeriod(parsedInput[0]);
endDate_.plus(periodLeft);
break;
}
case 3: {
periodLeft = timeFormatter.parsePeriod(parsedInput[2]);
periodLeft.plusDays(Integer.parseInt(parsedInput[0]));
endDate_.plus(periodLeft);
break;
}
default:
break;
}
}
據我瞭解,現在我有我想要的東西,對吧?現在我想計算過程的總持續時間。這就是爲什麼我轉換這個時期毫秒和進步基礎上計算總工期:
long duration_ = (periodLeft.toStandardDuration().getMillis() * 100)/(progress == 0 ? 1 : progress);
現在我要實現上的當前時間基礎上返回過程中的實際狀態的方法。我怎樣才能做到這一點?我知道持續時間,所以我可以開始DateTime。然後,我可以簡單地將當前日期與開始日期和計數百分比進行比較:(now - start)/ duration_ * 100.但是,如何獲取開始日期?
該解決方案是錯誤。正如我所說 - 我沒有開始時間。即使我的應用程序關閉,進程也會啓動。我解決了我的問題。 – karex
@ user1849493那麼你在哪裏可以找到開始時間?如果你不知道什麼時候開始,那麼你不能可靠地說你有一個可靠的百分比。雖然就像你說你解決了它。那很好。 – Bwire