Java 8将UTC时间转换为EDT / EST,以便日期保持不变

时间:2018-10-02 04:47:02

标签: java datetime java-8 timezone zoneddatetime

我在Java中使用ZonedDateTime作为变量。

我想将变量的值(默认UTC时区)转换为“美国/纽约”时区,以使日期保持不变。

示例UTC凌晨4:00 = EST凌晨12:00。从ZonedDateTime变量中添加或减去小时,以便不更改日期。

我们如何实现这种转换?

4 个答案:

答案 0 :(得分:2)

您可以通过转换为LocalDateTime并返回指定时区的ZonedDateTime来实现:

ZonedDateTime zoned = ZonedDateTime.now();
LocalDateTime local = zoned.toLocalDateTime();
ZonedDateTime newZoned = ZonedDateTime.of(local, ZoneId.of("America/New_York"));

答案 1 :(得分:1)

如果您要合并UTC的日期和EST的时间,可以这样:

iap_verifier

答案 2 :(得分:0)

保持相同的日期,我认为这可能有效

    ZonedDateTime utc = ZonedDateTime.now(ZoneOffset.UTC);
    ZonedDateTime est = utc.plusHours(5); //normally est is 5 hours ahead

答案 3 :(得分:0)

如果在您的UTC时间不需要区域信息,那么最好使用Instant类。使用ZonedDateTime对象,您可以轻松地在指定时区切换到Instant instant = Instant.parse("2018-10-02T04:00:00.0Z"); ZonedDateTime nyTime = instant.atZone(ZoneId.of("America/New_York")); //2018-10-02 00:00:00

import wx    

class SomeDialog(wx.Dialog):

    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, title='SomeDialog',
                           style=wx.DEFAULT_DIALOG_STYLE)
        self.button_ok = wx.Button(self, wx.ID_OK, size=(120,-1))
        hsizer = wx.BoxSizer(wx.HORIZONTAL)
        hsizer.Add(self.button_ok, 0, wx.ALL|wx.ALIGN_CENTER, 10)
        self.SetSizer(hsizer)
        self.SetSize(self.BestSize)
        self.Layout()

class TestFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, -1, size=(400, 400))
        self.button_progress = wx.Button(self, -1, 'Show Progress')
        self.button_modal = wx.Button(self, -1, 'Show Modal')
        self.button_both = wx.Button(self, -1, 'Show Both')
        self.Bind(wx.EVT_BUTTON, self.on_button, self.button_progress)
        self.Bind(wx.EVT_BUTTON, self.on_button, self.button_modal)
        self.Bind(wx.EVT_BUTTON, self.on_button, self.button_both)

        sizer = wx.BoxSizer()
        sizer.Add(self.button_progress)
        sizer.Add(self.button_modal)
        sizer.Add(self.button_both)
        self.SetSizer(sizer)

    def on_button(self, event):
        if event.EventObject is self.button_progress:
            self._show_progress_dialog()
        elif event.EventObject is self.button_modal:
            self._show_modal_dialog()
        else:
            self._show_progress_dialog()
            self._show_modal_dialog()

    def _show_progress_dialog(self):
        max = 10
        dlg = wx.ProgressDialog('Progress dialog example', 'Some message',
                                maximum=max, parent=self, 
                                style=wx.PD_APP_MODAL|wx.PD_AUTO_HIDE)
        keepGoing = True
        count = 0
        while keepGoing and count < max:
            count += 1
            wx.MilliSleep(250)
            wx.Yield()
            (keepGoing, skip) = dlg.Update(count)
        dlg.Destroy()

    def _show_modal_dialog(self):
        with SomeDialog(self) as dlg:
            dlg.CenterOnParent()
            dlg.ShowModal()

if __name__ == '__main__':
    app = wx.App()
    frame = TestFrame()
    frame.Show()
    app.MainLoop()