1

Given a timezone like 'EDT' in java, is there a way to set an arbitrary datetime in java to be in that timezone and print with the correct label, (either EDT or EST)?

Some context:

I am developing a weekly report generation feature that needs to timestamp reports in the timezone of the user who first configured the report. I would like to label the reports using the 3 letter timezone labels like 'EST' or 'CST,' so that users would see a message like "This report was generated at 03-28-2018 12:00:00 pm EDT." (I realize these can be ambiguous.)

Reports are generated as pdfs on the server so all information must come from the report configuration saved in the database.

Right now when the client posts a new report configuration, it includes the three letter timezone in the configuration along with its offset (eg 'GMT-0400' for EDT), and use that to create a LocalDateTime with the saved offset, then print it with saved label. Obviously this won't account for daylight savings, so I need a better solution.

For reference I am using AngularJS on the client.

gabeR
  • 11
  • 2
  • 2
    Fundamentally, instead of your configuration including an ambiguous not-actually-a-time-zone abbreviation, you'd be *much* better off if you could capture an IANA time zone ID, e.g. "America/New_York". From there, you can get to the right abbreviation for whatever instant in time you want. We don't have much context about how the reports are created, but there are plenty of options for detecting time zones in all kinds of environments. – Jon Skeet Mar 28 '18 at 19:20

1 Answers1

0

It turns out my problem needed a client side solution, based on comments and this question, I used moment.js moment.tz.guess(); to get an IANA timezone which is included when configuring a new report.

Once the server knows the IANA timezone creating the zoned datetime is simple:

String timezone = "America/New_York";
ZonedDateTime timestamp = ZonedDateTime.now().withZoneSameInstant(ZoneId.of(timezone));
gabeR
  • 11
  • 2