0

I have an RHEL6 Linux server in Mexico running my application. The server timezone is CDT. So when I issue the "date" command on the Linux command line I get the correct date and time which is an hour behind me. (I'm in EDT).

However, when I use the java.util.date object and display the current server date in a JSP, it is displaying the proper date and time for my timezone.

For instance I use:

<% ...
Date local = new Date();
out.write("'" + local + "'");
... %>

This would show, say,

Tue Aug 22 14:39:13 EDT 2017

, the date and time I am writing this. But I need to display

Tue Aug 22 13:39:13 CDT 2017

which is the actual date and time of the server.

I'm somewhat new to Java, JSP. So I'm not sure exactly how to get the output the way I need it.

Thank You

Paul Stoner
  • 1,359
  • 21
  • 44
  • [Displaying server time to jsp](https://stackoverflow.com/questions/19515138/displaying-server-time-on-jsp-page) – Berkley Lamb Aug 22 '17 at 18:50
  • 1
    @BerkleyLamb I looked at the SO question/answer you cited prior to asking my question. From what I saw, it does not address my specific need of ensuring the date/time is properly displayed in the JSP. and I'm currently not using javascript to constantly poll the server for the date time. Thank you, though, for your input – Paul Stoner Aug 22 '17 at 18:59
  • @NimrodArgov, I need to look at your suggested SO post a little deeper. It may actually lead to some insight. Thank you – Paul Stoner Aug 22 '17 at 19:00

1 Answers1

2

Your Question is a duplicate of many others, so briefly…

Do not use troublesome legacy date.time classes, now supplanted by java.time classes.

Date is always in UTC yet its toString method dynamically applies the JVM’s current default time zone. Confusing. One of many reasons to avoid legacy classes.

Always specify your desired/expected time zone. Never rely implicitly on the JVM’s current time zone. So as a programmer you should never care what zone the deployed server may be set to. That default can change at any moment during runtime so it is unreliable.

Never use the 3-4 letter pseudo time zones like CDT or EDT. They are not actual zones, they are not standardized, and they are not even unique! Real time zones named continent/region.

ZonedDateTime.now(
    ZoneId.of( "America/Mexico_City" ) 
).toString()

For other formats see DateTimeFormatter. Notice the ofLocalized… methods.

How to get the user’s desired time zone? You can try to get a default offset via the web browser. As I recall browsers return only a current offset, not a full time zone. But ultimately the only way to be sure is to ask the user explicitly.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • thank you for your input. Unfortunately, this is legacy code I am working with and may not be able to update it accordingly – Paul Stoner Aug 22 '17 at 19:45
  • @PaulStoner (A) If you are capturing the current moment, and generating a string, then how would you be tied into old code? (B) Furthermore, FYI, you can convert to/from java.time via new methods added to the old classes. So you can always interact with old code while sticking with only java.time in your new code. The old date-time classes really are an awful mess and should be avoided. – Basil Bourque Aug 22 '17 at 21:17