In joda time how to convert time zone without changing time

后端 未结 7 2001
一生所求
一生所求 2020-12-04 19:06

I am getting UTC timestamp from database which is I am setting into a JodaTime DateTime instance

DateTime dt = new DateTime(timestamp.getTime())         


        
7条回答
  •  盖世英雄少女心
    2020-12-04 19:25

    If your timestamp is: 2015-01-01T00:00:00.000-0500 (this is local time [for me])

    Try this:

    DateTime localDt = new DateTime(timestamp.getTime())
        .withZoneRetainFields(DateTimeZone.UTC)
        .withZone(DateTimeZone.getDefault());
    

    2014-12-31T19:00:00.000-05:00

    Breaking it down: This gives you a DateTime corresponding to your timestamp, specifying that it is in UTC:

    new DateTime(timestamp.getTime())
        .withZoneRetainFields(DateTimeZone.UTC)
    

    2015-01-01T00:00:00.000Z

    This gives you a DateTime but with the time converted to your local time:

    new DateTime(timestamp.getTime())
        .withZoneRetainFields(DateTimeZone.UTC)
        .withZone(DateTimeZone.getDefault());
    

    2014-12-31T19:00:00.000-05:00

提交回复
热议问题