Simulate different Timezone with Java

Asked

Viewed 140 times

0

I developed a web application (with java) and there I have a business rule that needs to save the user’s subscription time. But I need to save 3 times: user’s local Timezone, global Timezone, and server Timezone.

My problem is in testing the user’s local Timezone. Does anyone know how I can simulate a different Timezone for the browser, so I can simulate this test?

Note: I thought about creating a virtual machine, and hosting my application in it, and doing the test, because I would only have to change the Timezone of the virtual machine. So this process will take a long time, I want to know if there is any simple way.

1 answer

1


If you are using java 7 down recommend using the library Joda Time . Here’s an example of how to find time with this library.

//cria datetime com UTC
DateTime dt = new DateTime("2014-09-15T21:20:14", DateTimeZone.UTC);
DateTime dtLondon = dt.withZone(DateTimeZone.forID("Europe/London"));

The ideal is that all Datetime are in UTC in the database and that the Timezone is only converted at the time of showing to the user based on the location information of his browser. If for some reason you need to convert on the server you can use this implementation I put on top.

  • Well done, vlw. One way I managed to do the server simulation was to set in Tomcat the argument: -Duser.Timezone=GMT+13 (I wanted Timezone +13), and gave everything ok.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.