我有兩個java.time.Instant對象如何在java.time.Instant中獲取和設置指定的時間?
Instant dt1;
Instant dt2;
我想從DT2得到的時間(只有小時和分鐘無日期),將其設置爲DT1。 這是什麼最好的方法? dt2.get(ChronoField.HOUR_OF_DAY)
throws java.time.temporal.UnsupportedTemporalTypeException
我有兩個java.time.Instant對象如何在java.time.Instant中獲取和設置指定的時間?
Instant dt1;
Instant dt2;
我想從DT2得到的時間(只有小時和分鐘無日期),將其設置爲DT1。 這是什麼最好的方法? dt2.get(ChronoField.HOUR_OF_DAY)
throws java.time.temporal.UnsupportedTemporalTypeException
Instant
沒有任何小時/分鐘。請閱讀即時類的文檔:https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
如果你使用系統時區的即時轉換,你可以使用這樣的事情:
LocalDateTime ldt1 = LocalDateTime.ofInstant(dt1, ZoneId.systemDefault());
LocalDateTime ldt2 = LocalDateTime.ofInstant(dt2, ZoneId.systemDefault());
ldt1 = ldt1
.withHour(ldt2.getHour())
.withMinute(ldt2.getMinute())
.withSecond(ldt2.getSecond());
dt1 = ldt1.atZone(ZoneId.systemDefault()).toInstant();
轉換第一瞬間LOCALDATE的,並使用UTC作爲其時區,那麼你可以得到它的時間。
import java.time.*
LocalDateTime.ofInstant(Instant.now, ZoneOffset.UTC).getHour
Instant.now()? –
知道瞬間不是特定於任何時區或偏移量。您需要在參考時區時解釋即時,以確定一天中的小時。 – bowmore