1

I have a bunch of timestamp fields in my java model bean which is sent the view. If a user in another country or timezone tries to view these fields, will they be shown in the user's timezone. If not, could you provide a code snippet to do this conversion to the users timezone.

[Update] Adeels reply below will work, and this following link shows how to set it when getting the bean from database itself.

Community
  • 1
  • 1
Abe
  • 8,623
  • 10
  • 50
  • 74

1 Answers1

0

Get the user's time zone, and use Calendar.setTimeZone() to set the time zone and retrieve the Timestamp according to the user's time zone. You can instantiate Calendar using that TimeZone, i.e.

Calendar cal = GregorianCalendar.getInstance(userTimeZone);

Or you can set it afterwards like,

Calendar cal = Calendar.getInstance();
cal.setTimeZone(userTimeZone);
Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
  • If I set Calendar.setTimeZone() will the default timezone of my whole application be changed? Meaning, will it affect only that session or everyone using the app – Abe Jun 09 '11 at 07:41
  • Get calendar instance like this, `Calender.getInstance(TimeZone...)`. Then only that instance will be having that timezone. Further, see my addendum to the post. – Adeel Ansari Jun 09 '11 at 08:13
  • This will work but only thing is I need to manually convert each field in my java bean using this new cal/simpledateformat. I have added a link to my post, which seems to be the right way to do it. They are picking up the values with the timezone format from database this way I dont need to set all fields. – Abe Jun 09 '11 at 11:09
  • @Abe: I didn't say that you should do it for every field. You presumed it yourself ;). Yes, indeed the link provided is having several ways to do it. You can see that in that thread the poster is also using this technique, as I suggested. But he went further and provided the convenient way of doing it. May be because OP specifically asked for it. I'm glad you find it. Happy coding. – Adeel Ansari Jun 09 '11 at 11:19