0

In my backend I am using Springboot.

I want to be able to set the server time to be EST so that when I do something like:

Date() that time is in EST. Is that possible?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
J_Strauton
  • 2,270
  • 3
  • 28
  • 70
  • 1
    (1) I recommend you don’t use `Date`. That class is poorly designed and long outdated Instead use `ZonedDateTime` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/).(2) A `Date` hasn’t got, as in cannot have a time zone. So no, as the question is asked, that is not possible. – Ole V.V. Jul 07 '20 at 16:42

2 Answers2

2

TL;DR: No, that is not possible.

Messages:

  1. While it is possible to mess with the JVM’s default time zone, it is not recommended since it will affect not only all parts of your program but also all other programs running in the same JVM. Conversely every part of your program and every program running in the same JVM may set the default time zone to something that you didn’t want, so relying on it is fragile. Instead give explicit time zone to your date and time operations. You may configure a time zone setting in your program (for example through a properties file) and use the time zone that you have configured.
  2. When asking about EST, if thereby you mean Eastern Standard Time, in most places you obviously only can obtain that during the standard time of the year, not during DST/summer time.
  3. When asking about Date, that class cannot hold a time zone, which obviously prevents what you are asking.

Use ZonedDateTime for a date and time with a time zone

    ZoneId eastern = ZoneId.of("Australia/Victoria");
    ZonedDateTime now = ZonedDateTime.now(eastern);
    
    System.out.println(now);

Output when running just now:

2020-07-08T02:54:50.528310+10:00[Australia/Victoria]

This time is in Australian Eastern Standard Time, abbreviated either AEST or just EST. However, if you run the code after October 4, you will get Australian Eastern Daylight Time instead (AEDT, EDT or EDST).

If you intended North American Eastern Standard Time, you may try America/Toronto or America/New_York. They will give you EDT now and EST after 1st November.

There are time zones that will give you Eastern Standard Time all year, though, for example Australia/Brisbane in Australia and America/Cancun in North America.

ZonedDateTime is part of java.time, the modern Java date and time API. I recommend that you always use java.time, never the poorly designed and long outdated Date class.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

You can't change server machine's timezone from Java. If you want to do that on a server level, then you might want to consult server settings; however, if you're seeking to solve this at the application level, you can register a bean of the formatted Date object in the Spring Context, and then inject it everywhere you wish.

Note, that if it's java.util.Date, then you should use the DateFormat instance and set the desired timezone in it. Then you use that object in your Date instance. Date class, on its own, does not contain any timezone information so you can't set the timezone on a Date object.

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
  • The terrible `Date` class was supplanted years ago by the *java.time* classes defined in JSR 310. Specifically replaced by `java.time.Instant`. For modern solution, see the [Answer by Ole V.V.](https://stackoverflow.com/a/62780151/642706). – Basil Bourque Jul 08 '20 at 04:11