第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在 Windows 機(jī)器上獲取手動(dòng)設(shè)置時(shí)區(qū)

在 Windows 機(jī)器上獲取手動(dòng)設(shè)置時(shí)區(qū)

呼喚遠(yuǎn)方 2022-10-20 15:19:15
我希望能夠獲取我的機(jī)器設(shè)置的任何時(shí)區(qū),而無(wú)需在代碼中手動(dòng)指定時(shí)區(qū)。我可能不在那個(gè)時(shí)區(qū),但目標(biāo)是使用計(jì)算機(jī)設(shè)置的時(shí)區(qū)。在位置上,我的時(shí)區(qū)區(qū)域是 America/New_York,使用 ZoneId.systemDefault() (EDT)。但是,我已將計(jì)算機(jī)的時(shí)區(qū)設(shè)置為太平洋時(shí)間 (PDT)。我已經(jīng)嘗試了下面指定的代碼。在 Java 8 中實(shí)現(xiàn)這一目標(biāo)的最佳方法是什么?LocalDateTime currentDateTime = LocalDateTime.of(date.getYear(), date.getMonth(), date.getDay(), hour, minute, 0);        ZonedDateTime zonedDateTime = currentDateTime.atZone(ZoneId.systemDefault());        date = Date.from(zonedDateTime.toInstant());我希望時(shí)區(qū)是 Amrica/Dawson 或 America/Tijuana (UTC -08:00),但我得到的是 America/New_York (UTC -05:00)
查看完整描述

1 回答

?
GCT1015

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個(gè)贊

這是我為得到答案而編寫(xiě)的完整代碼:


TimeZone.setDefault(null);

System.setProperty("user.timezone", "");

//(above) force fetches and sets the JVM to the timezone the system is set to

LocalDateTime currentDateTime = LocalDateTime.of(date.getYear(), date.getMonth(), date.getDay(), hour, minute, 0);

ZonedDateTime zonedDateTime = currentDateTime.atZone(ZoneId.systemDefault());

date = Date.from(zonedDateTime.toInstant());

問(wèn)題是我可能已經(jīng)更改了@Matt 提到的系統(tǒng)時(shí)區(qū)。但是,我發(fā)現(xiàn)盡管手動(dòng)更改了時(shí)區(qū),但 JVM 時(shí)間并沒(méi)有改變。


TimeZone.setDefault(null);

System.setProperty("user.timezone", "");

(上)允許我清除之前設(shè)置的 JVM 時(shí)區(qū)。


編輯 04/26/2019


我更新了我的邏輯以支持太平洋時(shí)間的夏令時(shí)。我遇到了一個(gè)問(wèn)題,我的時(shí)間是設(shè)置 PST 而不是 PDT,所以我沒(méi)有使用上述邏輯,而是將其更改為獲取區(qū)域 ID 并將其與 Calendar 類一起使用。下面是完整的邏輯:


TimeZone.setDefault(null);

System.clearProperty("user.timezone"); //04/26/2019 EDIT This was suggested as a cleaner way of clearing the user timezone in the system.

//(above) force fetches and sets the JVM to the timezone the system is set to

System.out.println("Zone: "+ZoneId.systemDefault()+" isDaylightsavings? "+ZoneId.systemDefault().getRules().isDaylightSavings(Instant.now())+" currentDateTime: "+currentDateTime);

String timeZone = ""+ZoneId.systemDefault();

Calendar selectedDate = Calendar.getInstance(TimeZone.getTimeZone(timeZone)); // important when it comes to determining PDT or PST

date = selectedDate.getTime();


查看完整回答
反對(duì) 回復(fù) 2022-10-20
  • 1 回答
  • 0 關(guān)注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)