Converting To Another Timezone In Java

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...

4 comments

Leave a Reply

Your email address will not be published. Required fields are marked *