Convert Date/Time for given Timezone - java

前端 未结 16 2277
孤城傲影
孤城傲影 2020-11-22 12:36

I want to convert this GMT time stamp to GMT+13:

2011-10-06 03:35:05

I have tried about 100 different combinations of DateFormat, TimeZone,

16条回答
  •  粉色の甜心
    2020-11-22 12:59

    As always, I recommend reading this article about date and time in Java so that you understand it.

    The basic idea is that 'under the hood' everything is done in UTC milliseconds since the epoch. This means it is easiest if you operate without using time zones at all, with the exception of String formatting for the user.

    Therefore I would skip most of the steps you have suggested.

    1. Set the time on an object (Date, Calendar etc).
    2. Set the time zone on a formatter object.
    3. Return a String from the formatter.

    Alternatively, you can use Joda time. I have heard it is a much more intuitive datetime API.

提交回复
热议问题