久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Calendar.getInstance().getTime() 在“GMT"中返回日期

Calendar.getInstance().getTime() returning date in quot;GMTquot; instead of Default TimeZone(Calendar.getInstance().getTime() 在“GMT中返回日期而不是默認(rèn)時(shí)區(qū))
本文介紹了Calendar.getInstance().getTime() 在“GMT"中返回日期而不是默認(rèn)時(shí)區(qū)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..
Calendar c = Calendar.getInstance();
System.out.println(c.getTime());
c.set(2007, 0, 1);
System.out.println(c.getTime());

輸出:

2017 年 9 月 12 日星期二 12:36:24 IST

Tue Sep 12 12:36:24 IST 2017

2007 年 1 月 1 日星期一 12:36:24 IST

Mon Jan 01 12:36:24 IST 2007

但是,當(dāng)我在不同的環(huán)境中使用相同的代碼時(shí),輸??出變?yōu)橐韵?

But, When I use the same code in a different environment, Output changes to below:

輸出:

2017 年 9 月 12 日星期二 12:36:24 IST

Tue Sep 12 12:36:24 IST 2017

格林威治標(biāo)準(zhǔn)時(shí)間 2007 年 1 月 1 日星期一 12:36:24

Mon Jan 01 12:36:24 GMT 2007

僅供參考,我嘗試在設(shè)置值之前和之后打印日歷實(shí)例的時(shí)區(qū),并且兩者都在IST"中.

FYI, I tried to print the timezone of the calendar instance, before and after setting the values and both are in "IST".

我想知道造成這種情況的根本原因.

I want to know the root cause of this.

推薦答案

您問(wèn)題中的第二個(gè)輸出是運(yùn)行愛(ài)爾蘭時(shí)間(歐洲/都柏林)的 JVM 上的正確和預(yù)期行為.2017 年 9 月 12 日,愛(ài)爾蘭處于夏令時(shí) (DST).雖然沒(méi)有明確記錄,但 Date.toString() (您在打印從 c.getTime() 獲得的 Date 時(shí)隱式調(diào)用) 打印 JVM 時(shí)區(qū)中的日期和時(shí)間,該時(shí)區(qū)在 9 月呈現(xiàn)為愛(ài)爾蘭夏令時(shí)的 IST.

The second output in your question is the correct and expected behaviour on a JVM running Irish time (Europe/Dublin). On September 12, 2017 Ireland is on summer time (DST). While it is not clearly documented, Date.toString() (which you invoke implicitly when printing the Date you get from c.getTime()) prints the date and time in the JVM’s time zone, which in September is rendered as IST for Irish Summer Time.

當(dāng)您在 Calendar 對(duì)象上同時(shí)使用愛(ài)爾蘭時(shí)間設(shè)置日期時(shí),會(huì)保留一天中的小時(shí);在您的情況下,您將獲得 2007 年 1 月 1 日 12:36:24 愛(ài)爾蘭標(biāo)準(zhǔn)時(shí)間.現(xiàn)在想象一下,如果愛(ài)爾蘭夏令時(shí)間和愛(ài)爾蘭標(biāo)準(zhǔn)時(shí)間都被呈現(xiàn)為 IST,會(huì)造成什么混亂.你將無(wú)法區(qū)分.相反,由于愛(ài)爾蘭標(biāo)準(zhǔn)時(shí)間與格林威治標(biāo)準(zhǔn)時(shí)間重合,當(dāng)日期在一年中的夏季時(shí)間部分(即 1 月是't).

When you set the date on the Calendar object also using Irish time, the hour of day is preserved; in your case you get Jan 01 2007 12:36:24 Irish standard time. Now imagine the confusion if both Irish Summer Time and Irish Standard Time were rendered as IST. You would not be able to distinguish. Instead, since Irish standard time coincides with GMT, this is what Date.toString() prints when the date is not in the summer time part of the year (which January isn’t).

我的猜測(cè)是您的第一個(gè)輸出來(lái)自運(yùn)行印度時(shí)間的 JVM.它也被呈現(xiàn)為 IST,并且由于印度不使用夏令時(shí),因此夏季和冬季使用相同的縮寫(xiě).

My guess is that your first output is from a JVM running India time. It too is rendered as IST, and since India doesn’t use summer time, the same abbreviation is given summer and winter.

在理解您觀察到的行為的解釋之前,我發(fā)布了關(guān)于過(guò)時(shí)和現(xiàn)代 Java 日期和時(shí)間類(lèi)的評(píng)論.不過(guò),我仍然不認(rèn)為評(píng)論是錯(cuò)誤的.這是您的代碼的現(xiàn)代等價(jià)物:

Before understanding the explanation for the behaviour you observed, I posted a comment about the outdated and the modern Java date and time classes. I still don’t think the comment is way off, though. This is the modern equivalent of your code:

    ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("Europe/Dublin"));
    System.out.println(zdt);
    zdt = zdt.with(LocalDate.of(2007, Month.JANUARY, 1));
    System.out.println(zdt);

打印出來(lái)

2017-09-12T11:45:33.921+01:00[Europe/Dublin]
2007-01-01T11:45:33.921Z[Europe/Dublin]

如果要使用 JVM 的時(shí)區(qū)設(shè)置,請(qǐng)使用 ZoneId.systemDefault() 而不是 ZoneId.of("Europe/Dublin").正如名稱(chēng)所述,與 Date 不同,ZonedDateTime 確實(shí)包含時(shí)區(qū).它更多地對(duì)應(yīng)于舊的 Calendar 類(lèi).如您所見(jiàn),它的 toString 方法打印與 UTC 的偏移量(Z 表示零偏移量)和明確的 region/city 格式.我相信這會(huì)減少混亂的空間.如果要以特定格式打印日期,請(qǐng)使用 DateTimeFormatter.

If you want to use the JVM’s time zone setting, use ZoneId.systemDefault() instead of ZoneId.of("Europe/Dublin"). As the name states, contrary to Date, ZonedDateTime does include a time zone. It corresponds more to the old Calendar class. As you can see, its toString method prints the offset from UTC (Z meaning zero offset) and the time zone name in the unambiguous region/city format. I believe that this leaves a lot less room for confusion. If you want to print the date in a specific format, use a DateTimeFormatter.

為了完整起見(jiàn),以下是您的代碼在運(yùn)行可能呈現(xiàn)為 IST 的不同時(shí)區(qū)時(shí)的輸出:

For the sake of completeness, here are the outputs from your code when running different time zones that may be rendered as IST:

  • 歐洲/都柏林(同意您的第二個(gè)輸出)

  • Europe/Dublin (agrees with your second output)

Tue Sep 12 11:19:28 IST 2017
Mon Jan 01 11:19:28 GMT 2007

  • 亞洲/特拉維夫

  • Asia/Tel_Aviv

    Tue Sep 12 13:19:28 IDT 2017
    Mon Jan 01 13:19:28 IST 2007
    

  • Asia/Kolkata(同意你的第一個(gè)輸出)

  • Asia/Kolkata (agrees with your first output)

    Tue Sep 12 15:49:28 IST 2017
    Mon Jan 01 15:49:28 IST 2007
    

  • 這篇關(guān)于Calendar.getInstance().getTime() 在“GMT"中返回日期而不是默認(rèn)時(shí)區(qū)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

    【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

    相關(guān)文檔推薦

    Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時(shí)間,就像在 UTC 中一樣)
    How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉(zhuǎn)換為公歷?)
    Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
    Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉(zhuǎn)換.公歷到儒略歷切換)
    java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
    Java: getting current Day of the Week value(Java:獲取當(dāng)前星期幾的值)
    主站蜘蛛池模板: 九九精品网 | 久久精品国产免费 | 中文字幕欧美在线观看 | 精品欧美一区二区在线观看欧美熟 | 自拍 亚洲 欧美 老师 丝袜 | 亚洲欧美成人影院 | 中文字幕 国产精品 | 在线免费看黄 | 激情三区 | 精品视频一区二区三区在线观看 | 精品国产一区二区三区日日嗨 | 五月综合激情在线 | 四虎在线播放 | 国产成人区 | 精品欧美乱码久久久久久 | 精品国产一区二区三区免费 | 一级黄色片一级黄色片 | 成年人在线观看视频 | 羞羞网站免费 | 91欧美精品成人综合在线观看 | 日韩欧美在线观看一区 | 亚洲一区二区三区久久久 | 国产成人精品免费视频大全最热 | 日韩在线视频一区 | 国产成人免费视频网站高清观看视频 | 黄色网络在线观看 | 日日干日日操 | 国产精品久久久久久久免费观看 | 91亚洲精选 | 本地毛片| 久久毛片 | 五月激情综合网 | 午夜男人天堂 | 在线免费av观看 | av福利网站| av夜夜操| 国产在线精品一区二区三区 | 成人区一区二区三区 | 九九热最新地址 | 91精品久久久久 | 一区二区三区四区电影 |