Converting date from UTC to EST in Java?

后端 未结 4 1970
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-01 06:14

I\'m trying to convert a long timestamp that is UTC to Eastern Standard Time and am totally lost. Any hints would be great!

Time format should be : 11/4/03 8:14 PM

4条回答
  •  长情又很酷
    2021-01-01 07:04

    Timezone conversion can be tricky. You should probably use Joda Time where timezone tables are constantly updated (and can be updated manually if needed

    Using Joda time it is pretty easy:

    public static Date convertJodaTimezone(LocalDateTime date, String localTimeZone, String destTimeZone) {
      DateTime srcDateTime = date.toDateTime(DateTimeZone.forID(localTimeZone));
      DateTime dstDateTime = srcDateTime.withZone(DateTimeZone.forID(destTimeZone));
      return dstDateTime.toLocalDateTime().toDateTime().toDate();
    }
    

    with timezones from the following table

提交回复
热议问题