Simulate different timezone with Java

0

I developed a web application (with java) and in it I have a business rule which needs to save the user's subscription time. However 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 on 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.

    
asked by anonymous 10.03.2016 / 13:00

1 answer

1

If you are using java 7 down I recommend using the Joda Time library. 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"));

Ideally, all DateTime should be in UTC in the database and that TimeZone should only be 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 to the server you can use this implementation that I put up.

    
10.03.2016 / 15:04