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"

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Your app should be set up to store and use the timezone of the client
Mick BarryJava DeveloperCommented:
Java does not have a method for that, so you'll need to handle it yourself by adding Timezone to the session and use that timezone throughout your application

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

pmsguyAuthor Commented:
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
>>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
pmsguyAuthor Commented:
Please exlain - "how to do that yourself?"
That would depend on how you want this custom timezone to manifest itself
Mick BarryJava DeveloperCommented:
>  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
Mick BarryJava DeveloperCommented:

session.setAttribute("my.timezone", timezone);
pmsguyAuthor Commented:
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

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
Ray PaseurCommented:
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.
Mick BarryJava DeveloperCommented:
> So for every data/time command I instantiate in Java I have to set the timezone?

basically yes

> Again, php allows you to define the timezone for the application, see

you can set the default timezone for the application, but that would affect all sessions
How about this custom timezone class where you can set/get the timezone and date/time manipulation?
class platformTime{
    public static final String     timeZoneValue = "America/New_York";
    public static final String     date_epoch_format = "dd/MM/yyyy HH:mm:ss";
    public static int platform_SECOND;
    public static int platform_MINUTE;
    public static int platform_HOUR;
    public static int platform_DAY;
    public static int platform_MONTH;
    public static int platform_YEAR;
    public static int platform_DAY_OF_WEEK;

    public void updatetime(){
//get Calendar instance
Calendar now = Calendar.getInstance();

TimeZone timeZone = now.getTimeZone();

//get current TimeZone using getTimeZone method of Calendar class

Calendar nyTime = new GregorianCalendar(TimeZone.getTimeZone(timeZoneVal));


platform_SECOND = nyTime.get(Calendar.SECOND);
platform_MINUTE = nyTime.get(Calendar.MINUTE);
platform_HOUR = nyTime.get(Calendar.HOUR_OF_DAY);

platform_DAY = nyTime.get(Calendar.DATE);
platform_MONTH = nyTime.get(Calendar.MONTH);
platform_YEAR = nyTime.get(Calendar.YEAR);
platform_DAY_OF_WEEK = nyTime.get(Calendar.DAY_OF_WEEK);

    public int getSecond(){
        return platform_SECOND;
      public int getMinute(){
        return platform_MINUTE;
    public int getHour(){
        return platform_HOUR;
    public int getDay(){
        return platform_DAY;
    public int getMonth(){
        return platform_MONTH;
    public int getYear(){
        return platform_YEAR;
    public int getDayOfWeek(){
        return platform_DAY_OF_WEEK;
    public String getDateTime(){
        String datetime = platform_DAY+"/"+platform_MONTH+"/"+ platform_YEAR + " "+  platform_HOUR+":"+platform_MINUTE+":"+platform_SECOND;
        return datetime;

    public long getEpoch(){
        java.util.Date epoch;
        long longDate;
            epoch = new java.text.SimpleDateFormat(date_epoch_format).parse(getDateTime());
            longDate = (epoch.getTime()/1000);
            return longDate;
        } catch (Exception e) {
            return 0;

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.