我有一個函式,它回傳物件中的當前 UTC 時間Date,很簡單。我們所做的是找到當前Instant的UTC,將其放入Date物件并回傳。注意:這里的本地主機時間是紐約/美國。
我們現在面臨的問題是 Date 拒絕存盤March 13 2:02 AM,因為時間不存在(紐約三月第二個星期日從凌晨 2 點到凌晨 3 點的時鐘跳過一個小時),但同樣存在在 UTC 中,我們想要 UTC 時間。
有什么方法可以"20220313 02:02:00.000"在 java Date 物件中表示。
這是紐約時間(本地)的時間"20220312 21:02.00.000"
public static Date getCurrUtc() throws ParseException {
Instant instant = Instant.now();
LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);
Date date = new Date();
int year = ldt.getYear(),
month = ldt.getMonthValue() - 1,
day = ldt.getDayOfMonth(),
hour = ldt.getHour(),
minute = ldt.getMinute(),
second = ldt.getSecond(),
milliseconds = (int) date.getTime() % 1000;
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
isoFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
date = isoFormat.parse(String.format("%d-%d-%dT%d:%d:%d.%d", year, month, day, hour, minute, second, milliseconds));
return date;
}
uj5u.com熱心網友回復:
您目前正在混合呼叫兩個非常不同的 API(舊的和過時java.util.Date的以及來自 的類java.time,如LocalDateTime)。我會堅持使用較新的 API,因為如果您想在兩個不同的時區表達相同的瞬間,它會讓生活變得更輕松。
您仍然可以使用LocalDateTime來決議來自 的值String,然后添加 aZoneId以使其代表一個真實的時刻。
這是一個例子:
public static void main(String[] args) {
// your example datetime String
String datetime = "20220313 02:02:00.000";
// a pattern representing the format of your datetime String
String formatPattern = "uuuuMMdd HH:mm:ss.SSS";
// a formatter using that pattern (can parse and format)
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(formatPattern);
// the two time zones involved
ZoneId americaNewYork = ZoneId.of("America/New_York");
ZoneId utc = ZoneId.of("UTC");
// the date and time of day without any zone or offset
LocalDateTime zonelessDateTime = LocalDateTime.parse(datetime, dtf);
// that date and time of day in New York
ZonedDateTime newYorkTime = zonelessDateTime.atZone(americaNewYork);
System.out.println(newYorkTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
// the same instant in UTC (same date, different time of day, no offset
ZonedDateTime utcTime = zonelessDateTime.atZone(utc);
System.out.println(utcTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
}
正如您在輸出中看到的,一天中的時間不同,區域也不同:
2022-03-13T03:02:00-04:00[America/New_York]
2022-03-13T02:02:00Z[UTC]
如果您必須生產/消費java.util.Dates,您可以使用由于您的原因而實作的兼容性方法:處理大量遺留代碼。
簡而言之:Ajava.time.ZonedDateTime和 ajava.time.OffsetDateTime是時間中的瞬間、瞬間的表示。好在還有s a java.time.Instant , too, and you can convert a java.util.Date from/to anInstant . There's Date.from(Instant) andDate.toInstant() . Here's how you would use the results of the code example above in order to have the values as Date`:
// convert the Instants represented by the ZonedDateTimes to Dates
Date newYorkDate = Date.from(newYorkTime.toInstant());
Date utcDate = Date.from(utcTime.toInstant());
// print the Date values
System.out.println(newYorkDate);
System.out.println(utcDate);
這些行將產生以下輸出:
Sun Mar 13 08:02:00 CET 2022
Sun Mar 13 03:02:00 CET 2022
Please have a closer look at the values of the java.util.Dates.
Zones implicitly changed and values adjusted (even though a Date does not really have a zone). You have basically no real control over zone shifts and time conversions.
There are several reasons for a totally new and different datetime API introduced in Java 8… The mentioned is just one of them.
Is there any way to represent "20220313 02:02:00.000" in java Date object?
Yes, there is… You could create the Date and return it. How this Date instance will be represented as String depends on the TimeZone used. See this:
// create the date
Date date = Date.from(
LocalDateTime.parse("20220313 02:02:00.000", dtf)
.atZone(utc)
.toInstant()
);
// get the milliseconds since 1970-01-01, the only numerical value stored by a Date
long epochMillis = date.getTime();
// create a format for visualization
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
// add a time zone to the format
isoFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
// and use it to print the date in that zone
System.out.println(epochMillis " in New York: " isoFormat.format(date));
// set the time zone of the format to UTC
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
// and use it to print the date in a different zone
System.out.println(epochMillis " in UTC: " isoFormat.format(date));
The output is the following, mind that the same Date is used:
1647136920000 in New York: 2022-03-12T21:02:00.000
1647136920000 in UTC: 2022-03-13T02:02:00.000
OK, the format is not stored in the Date variable, but the underlying value is, at least, which makes it representable in different formats that have different time zones.
Thing to think about:
Do you think the time zone should be part of the format rather than part of a datetime object itself? The answer to this question could be mapped to the question Do you want to use java.util.Date or java.time? ;-)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/442959.html
上一篇:SpringMVC中的控制器建議
