0

I have a Java SE server application with a Saas website and registered users.

I have many events that occur on my server in different days.

Time is registered in localhost in a long number via SYSTEM.currentTimeMillis()/1000

Registered users can check these events time from their respective country and they need to see the correct time based on their timezone (not server timezone) through the website.

How do I show them the historical time of the events in their timezone? Any idea about how you would deal with this situation?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
GingerGirl
  • 301
  • 1
  • 16
  • First, in views, you must use **javascript** to display local dates. For this check this question [How do I display a date/time in the user's locale format and time offset?](http://stackoverflow.com/questions/85116/how-do-i-display-a-date-time-in-the-users-locale-format-and-time-offset) – Jordi Castilla Sep 21 '15 at 14:25
  • @JordiCastilla No, JavaScript is *not* required. Quite the opposite. JavaScript has virtually no support for date-time work while Java has industry-leading date-time frameworks in Joda-Time and [java.time](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html). Java should be used as much as possible for this. You may need JavaScript to detect a default time zone, but ultimately the most reliable way to decide on time zone is to ask the user. With a time zone in hand, use java.time to generate string representations of the date-time values. – Basil Bourque Sep 21 '15 at 17:30

3 Answers3

1

Easiest way is to use http://momentjs.com/timezone/. Idea is following - you send sth like this in html markup

<div class="raw-datetime">2014-12-01 12:00:00 UTC+03:00</div>

And after page loads - you run javascript that adjusts all raw datetime to browser timezone.

ZyXEL
  • 163
  • 1
  • 12
0

First of all, we will use UTC as a default and unique time zone in the system. Because, the time never go back or go to the future in UTC. There is no time shift for daylight saving. So, all applications (applications which we develop and has timezone support for their users) environment require a JVM parameter, which provides a UTC based environment.

Timezone JVM parameter usage

-Duser.timezone=UTC

For Views

For views, the date/time object should be rendered according to the specified time-zone. For Java world, this is handled by formatDate tld in jstl. Every project contains its own timezone holding logic itself.

User.timeZone : for admin panels

Some fmt:formatDate example

<fmt:formatDate value="${someDateTime}" timeZone="${user.timeZone}" pattern="MMM dd, yyyy - HH:mm:ss.S"/>

For further information about formatDate taglib please see check the link or google it.

For Forms (Getting the date / time data from the user)

When you getting date information via forms the you need to consider time zone and perform the time conversion. The conversion direction is User Time zone to UTC. The time in database should be in UTC time zone.

Motto is save it globally; show it locally :)

EDIT:

Hold timezone as a subfield of User object and set it to your formatter when you want to show the time, you can use it in JavaSE.

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm");
        formatter.setTimeZone(TimeZone.getTimeZone(user.getTimeZone()));
Sevle
  • 3,109
  • 2
  • 19
  • 31
erhun
  • 3,549
  • 2
  • 35
  • 44
0

java.time

First you need to determine the user’s time zone. Search on StackOverflow to learn that ultimately the best way to do that is to ask the user. You can try to use JavaScript on the browser to auto-detect a time zone, but there are issues.

You need to arrive at a proper time zone name, usually a continent/cityOrRegion such as America/Montreal or Asia/Kolkata. Never use those 3-4 letter codes like EST or IST as they are neither standardized nor unique.

To localize a date-time you need to know the user’s Locale, a human language and a set of cultural norms.

With a time zone in hand, use the new java.time framework built into Java 8 and later. Inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project.

long epochSeconds = … ;
Instant instant = Instant.ofEpochSeconds( epochSeconds );
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant, zoneId);
Locale locale = Locale.CANADA_FRENCH;
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( locale );
String output = zdt.format( formatter );
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154