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

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

在時(shí)區(qū)之間轉(zhuǎn)換紀(jì)元毫秒

在時(shí)區(qū)之間轉(zhuǎn)換紀(jì)元毫秒

侃侃爾雅 2023-06-04 14:57:04
我得到一個(gè)紀(jì)元格式的數(shù)字。Epoch 應(yīng)該在 UTC 中,但我在 PST 時(shí)區(qū)中獲取它。所以我必須修復(fù)這個(gè)值。我該怎么做?我最初嘗試的是:  // This number represents Tuesday, July 30, 2019 1:53:19 AM UTC,   // but it's supposed to represent PST.  // The actual PST value for this date is going to be 1564476799000   // which is the same as Tuesday, July 30, 2019 8:53:19 AM UTC.  // So I need to "pretend" that this value is actually PST   // and adjust it accordingly (including DST and friends).  Long testDateLong = 1564451599000L;  // These correctly assume that the instant is in UTC and adjust it to PST  // which is not the real intention  LocalDateTime pstL = LocalDateTime.ofInstant(Instant.ofEpochMilli(testDateLong),      ZoneId.of("America/Los_Angeles"));  ZonedDateTime pstZ = ZonedDateTime.ofInstant(Instant.ofEpochMilli(testDateLong),      ZoneId.of("America/Los_Angeles"));  System.out.println(pstL);  System.out.println(pstZ);  /*   * Output:   *   * 2019-07-29T18:53:19   * 2019-07-29T18:53:19-07:00[America/Los_Angeles]   *    * Expected to see:    *    * 2019-07-30T01:53:19   * 2019-07-30T01:53:19-07:00[America/Los_Angeles]   *    */可行的解決方案是將 epoch 值格式化為 UTC 格式的字符串,然后使用 PST 時(shí)區(qū)對(duì)其進(jìn)行解析,如下所示:  Long testDateLong = 1564451599000L;  DateTimeFormatter formatterUTC = DateTimeFormatter    .ofLocalizedDateTime(FormatStyle.SHORT)    .withZone(ZoneId.of("Etc/UTC"));  DateTimeFormatter formatterPST = DateTimeFormatter      .ofLocalizedDateTime(FormatStyle.SHORT)    .withZone(ZoneId.of("America/Los_Angeles"));  String utcString = formatterUTC.format(Instant.ofEpochMilli(testDateLong));  Instant instant = Instant.from(formatterPST.parse(utcString));  System.out.println(utcString);  System.out.println(instant);  System.out.println(instant.toEpochMilli());  /*   * Output:   *   * 7/30/19 1:53 AM   * 2019-07-30T08:53:00Z   * 1564476780000   */然而,這對(duì)我來說似乎是一個(gè)糟糕的解決方案(只是一種預(yù)感)。我想知道是否有比生成字符串并解析它更好的方法?
查看完整描述

1 回答

?
HUX布斯

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

您可以使用UTC區(qū)域進(jìn)行解析,然后更改Zone


long testDateLong = 1564451599000L;

Instant ist = Instant.ofEpochMilli(testDateLong);


ZoneId zUTC = ZoneId.of("UTC");

ZoneId zLA = ZoneId.of("America/Los_Angeles");


ZonedDateTime zdt1 = LocalDateTime.ofInstant(ist, zUTC).atZone(zLA);

ZonedDateTime zdt2 = ZonedDateTime.ofInstant(ist, zUTC).withZoneSameLocal(zLA);


System.out.println(zdt1); // 2019-07-30T01:53:19-07:00[America/Los_Angeles]

System.out.println(zdt2); // 2019-07-30T01:53:19-07:00[America/Los_Angeles]


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

添加回答

舉報(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)