Link to home
Create AccountLog in
Avatar of pmsguy
pmsguy

asked on

How do I set the Timezone for a Java Application dynamically like PHP date_default_timezone_set?

PHP allows you to set the timezone for the session using date_default_timezone_set.

I want to do the same for my Java application.  That is, dynamically change the timezone for the session.  The timezone needs to dynamically change multiple times during the Java session.

I do NOT want to set the timezone for every instance of a date/time related object.
Also, I do not want to use a command line setting like "java -Duser.timezone=UTC"


Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Your app should be set up to store and use the timezone of the client
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Generally speaking, you will ask the user to set their timezone as a preference and then save it always in a cookie. otoh, you could do it for the session only
Avatar of pmsguy
pmsguy

ASKER

My java process is a daemon process and I need to dynamically change the timezone for the given database I am traversing.  Therefore, I want to change the timezone in Java so any date/time command will be relative to this timezone.  Again, I am asking is there a command in Java to set the default timezone for the session?  Similar to PHP; see http://php.net/manual/en/function.date-default-timezone-set.php
>>Again, I am asking is there a command in Java to set the default timezone for the session?  

No - you need to do that yourself
Avatar of pmsguy

ASKER

Please exlain - "how to do that yourself?"
That would depend on how you want this custom timezone to manifest itself
>  Again, I am asking is there a command in Java to set the default timezone for the session?

I already mentioned above that there isn't


session.setAttribute("my.timezone", timezone);
Avatar of pmsguy

ASKER

So for every data/time command I instantiate in Java I have to set the timezone?

That stinks if that is the case.

Again, php allows you to define the timezone for the application, see http://php.net/manual/en/function.date-default-timezone-set.php.

I can't see how Java does not allow you to do this.
>>So for every data/time command I instantiate in Java I have to set the timezone?

No - you just get and use the TimeZone you saved in the session
Why not make all of it UTC?  Then the only time you would need to be concerned about the local time zone is when you are ready to produce client output.
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account