3

I have the following code which gets current time in a certain format. This works perfectly fine locally when I test it out on my laptop.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS z");
ZonedDateTime date = ZonedDateTime.now();
String timeStamp = formatter.format(date);

This above works locally and the timestamp value is in following format: 2020-02-24 05:23:20.675 MST

But when I push it to production, the format changes to following: 2020-02-24 05:23:20.675 -07:00

I do not have access to the production settings and the team that handles it is in another timezone and will not be able to get them now. Believe it is some setting on their end but is there something I could do such that the format is always like: 2020-02-24 05:23:20.675 MST ?

Please advice, thanks.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
karvai
  • 2,417
  • 16
  • 31
  • `ZONEDDateTime` – Eugene Feb 24 '20 at 15:33
  • Q1: What is the difference between local and production (assuming code is identical)? Q2: Sure the time zone is not explicitly set on the production server (to that offset value)? – ernest_k Feb 24 '20 at 15:35
  • @Eugene What do you mean? I am using ZonedDateTime – karvai Feb 24 '20 at 15:35
  • exactly. did you happen to read the documentation if it? "A date-time **with a time-zone**" – Eugene Feb 24 '20 at 15:36
  • I think the problem in Locale of yours machine and production. Create formatter with specific Locale https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ofPattern-java.lang.String-java.util.Locale- – Maxim Andreev Feb 24 '20 at 15:37
  • @ernest_k A1: Code is identical. By local I mean I am running it on my IDE (Intellij) on my laptop. Production meaning deployed to cloud and live for customers. A2: I am unsure about that. – karvai Feb 24 '20 at 15:37
  • No, @MaximAndreev, locale is not relevant here (locale and time zone are orthogonal concepts). – Ole V.V. Feb 24 '20 at 18:50

1 Answers1

5

You have to specify your time zone, it seems in production you are using a different time zone than the one in local. Beside in your code you don't specify any Zone, for that it took the default Zone.

To solve this, you have to specify the zone :

ZoneId zoneId = ZoneId.of("America/Dawson_Creek"); // specify the zone you want to use
ZonedDateTime date = ZonedDateTime.now(zoneId);
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • Thanks for answering. Yes my local and prod is different. Local is GMT and prod is MST. I am looking for MST and based on docs it is -07:00. So if I go ZoneId zoneId = ZoneId.of("-07:00") that would resolve it? Giving it a go now to test. – karvai Feb 24 '20 at 15:42
  • @karvai don't use `-07:00` instead use the name of zone as I do in my answer – Youcef LAIDANI Feb 24 '20 at 15:44
  • In the docs there is no text for it. https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html Look under Field Detail – karvai Feb 24 '20 at 15:46
  • 1
    This may be related : https://stackoverflow.com/questions/45592878/java-doesnt-have-information-about-all-iana-time-zones – Arnaud Feb 24 '20 at 15:49
  • 1
    @karvai yes, `-07:00` can do the job – Youcef LAIDANI Feb 24 '20 at 15:52