簡體   English   中英

嘗試將時間/日期更改為 UTC-1 時出錯

[英]Error when I try to change my time/date to UTC-1

我有一個日期與我的系統的實際時間(我住在西班牙)。 我需要將其更改為 UTC-1,但無論我寫“UTC-1”還是“UTC-2”,它總是給我相同的時間少於 2 小時,我的意思是:

我的系統時間(time_utc):11:00 13/04/2021 嘗試 UTC-1(時間):09:00 13/04/21 嘗試 UTC-2(時間):09:00 13/04/21

我有這個代碼:

    Date time_utc = new Date();
    DateFormat convertidor = new SimpleDateFormat("yyyy-MM-dd HH:00:00.000");    
    
    convertidor.setTimeZone(TimeZone.getTimeZone("UTC-1"));     

    time = convertidor.format(time_utc);    

為什么它不起作用? 任何人都可以幫助我嗎? 非常感謝!

你好!

您可以使用java.time以非常短的方式執行此操作(如果您被允許並願意這樣做)。

有一些特殊的類代表不同時區偏移量中的某個時刻。 其中之一是OffsetDateTime ,請參見此示例:

public class Main {

    public static void main(String[] args) {
        // create one of your example date times in UTC
        OffsetDateTime utcOdt = OffsetDateTime.of(2021, 4, 13, 11, 0, 0, 0, ZoneOffset.UTC);
        // and print it
        System.out.println(utcOdt);
        /*
         * then create another OffsetDateTime 
         * representing the very same instant in a different offset
         */
        OffsetDateTime utcPlusTwoOdt = utcOdt.withOffsetSameInstant(ZoneOffset.ofHours(2));
        // and print it
        System.out.println(utcPlusTwoOdt);
        // do that again to see "the other side" of UTC (minus one hour)
        OffsetDateTime utcMinusOneOdt = utcOdt.withOffsetSameInstant(ZoneOffset.ofHours(-1));
        // and print that, too.
        System.out.println(utcMinusOneOdt);
    }
}

它輸出以下三行:

2021-04-13T11:00Z
2021-04-13T13:00+02:00
2021-04-13T10:00-01:00

如您所見,一天中的時間根據偏移量進行調整。

如果需要,可以將 output 格式化為您想要的樣式(目前僅使用OffsetDateTimetoString()方法)。

更新

使用 java.time.format.DateTimeFormatter 時,您可以通過將模式定義為uuuu-MM-dd HH:mm來實現按需要格式化的java.time.format.DateTimeFormatter
只需將以下行添加到上面的示例中:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm");

System.out.println(utcOdt.format(dtf));
System.out.println(utcPlusTwoOdt.format(dtf));
System.out.println(utcMinusOneOdt.format(dtf));

這將是 output

2021-04-13 11:00
2021-04-13 13:00
2021-04-13 10:00

如果你真的想修復秒和毫秒的零,然后像這樣創建你的DateTimeFormatter

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:00.000");

這將導致 output 像這樣:

2021-04-13 11:00:00.000
2021-04-13 13:00:00.000
2021-04-13 10:00:00.000

作為 deHaar 良好答案的補充:

  1. 正如 Matt Johnson-Pint 已經問過的那樣,您是否需要轉換到不同的時區? 這將是最典型的。 如果是這樣,請使用該時區,而不僅僅是 -1 的 UTC 偏移量。 很可能時區過去使用過其他偏移量,並且將來很可能會這樣做。 所以 -01:00 不安全。 像 Atlantic/Cape_Verde 這樣的實時區域 ID 更安全。
  2. 您不需要 go 通過您自己時區的當前時間並進行轉換。 java.time 可以直接為您提供另一個時區或特定 UTC 偏移量的當前時間。
  3. java.time 也可以將時間截斷為整小時。

例如:

    DateTimeFormatter formatter
            = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS");
    
    ZoneId zone = ZoneId.of("Atlantic/Cape_Verde");
    ZonedDateTime nowInCaboVerde = ZonedDateTime.now(zone);
    
    System.out.println(nowInCaboVerde);
    System.out.println(nowInCaboVerde.truncatedTo(ChronoUnit.HOURS)
            .format(formatter));

Output:

 2021-04-14T03:12:28.272010-01:00[Atlantic/Cape_Verde] 2021-04-14 03:00:00.000

PS Cabo Verde/Cape Verde 在偏移量 -02:00 直到 1975 年。

你的代碼出了什么問題?

這就是舊的TimeZone class 的行為方式令人困惑,也是您永遠不應使用它的原因之一:當給定一個它無法識別的時區 ID 時,它會返回 GMT 並假裝一切正常。 UTC-1不是公認的時區 ID。 如果引用實時時區沒有意義,並且您需要從 UTC 偏移 -01:00,您可能使用了GMT-1GMT-01:00 是的, TimeZone將 UTC 稱為 GMT,即使它們在嚴格意義上並不相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM