Well, there is no direct way to get the offset of a timezone such as -08:00 from the DateTime object. The only solution that I found working was to format it by using the DateTimeFormat class.

Here is how to do it:

Frankly, this was a pain. The Calendar class’ getTime() method which returns a java.util.Date object is misleading. After hours and hours of figuring out why, here is what I noticed. The Calendar class’ getTime() method does not change the timezone even when you assign it with the setTimeZone() method.

I finally got to make a method out of it by using the DateFormat (or SimpleDateFormat whichever you like, or both using polymorphism) and formatting it as a String, then parsing that date string and converting it to a Date object.

That is the most accurate way of getting the new Date object with the new timezone. That said, I hope those who had a hard time trying to make it work can find this method that I made useful.

Related Posts Plugin for WordPress, Blogger...