1

The saved time in the application is in UTC. The users of the application are in different timezones, such as PST, IST etc.

My issue is very similar to the one in this post. I tried implementing a solution provided there, but it does not seem to be working for me:

Calendar calendar = Calendar.getInstance();
calendar.setTime(history.getCreatedTimestamp());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

//Here you say to java the initial timezone. This is the secret
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
//Will print in UTC
System.out.println(sdf.format(calendar.getTime()));

//Here you set to your timezone
sdf.setTimeZone(TimeZone.getDefault());
//Will print on your default Timezone
System.out.println(sdf.format(calendar.getTime()));
Archana
  • 53
  • 1
  • 9
  • 1
    Why are you converting an existing value into a string and then parsing it again? That's almost always a bad idea. Please provide a [mcve] that demonstrates the problem - there's a lot that's missing here. – Jon Skeet Feb 20 '18 at 11:18
  • 2
    Additionally, it's not clear what you expect `ZoneOffset.ofHours(Calendar.ZONE_OFFSET)` to do, but that's basically wrong. `Calendar.ZONE_OFFSET` is a constant - surely you want to use the user's actual time zone, which is unlikely to be a `ZoneOffset` anyway. "PST" isn't a time zone - it's an ambiguous abbreviation for *part* of when a time zone is observed. You'll need to know the user's actual time zone ID, e.g. America/New_York - or use `ZoneId.systemDefault()` if your code is running on the user's machine - it's unclear from your question. – Jon Skeet Feb 20 '18 at 11:20
  • no, the code will run in a server located in PST. – Archana Feb 20 '18 at 11:31
  • https://stackoverflow.com/a/14314481/5624464 refer this answer – Vishal Kawade Feb 20 '18 at 11:33
  • Well, probably located in Pacific Time... but anyway, that suggests you need to get the user's time zone from somewhere. Do you have that? (Additionally, please take more care when presenting code. Use the preview to see what we'll see when it's posted, and ask yourself whether that's the best presentation in terms of indexing etc.) – Jon Skeet Feb 20 '18 at 11:33
  • Yes looks like there is no way to do it wihout getting the timeZone. Currently i do not have it at server context. Thanks will be diligent enough in future. – Archana Feb 20 '18 at 11:38
  • Depending on your architecture there may be [an interesting answer here](https://stackoverflow.com/a/34602679/5772882) about getting a browser’s time zone — in case you are doing a web app. – Ole V.V. Feb 20 '18 at 12:30

1 Answers1

3

I suggest you to use Java 8 classes (SimpleDateFormat and Calendar are trouble, avoid them if possible):

long timestamp = history.getCreatedTimestamp();
// create Instant from timestamp value
Instant instant = Instant.ofEpochMilli(timestamp);

// formatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

// convert to UTC
System.out.println(formatter.format(instant.atZone(ZoneOffset.UTC)));

// convert to another timezone
System.out.println(formatter.format(instant.atZone(ZoneId.of("America/Los_Angeles"))));

// convert to JVM default timezone
System.out.println(formatter.format(instant.atZone(ZoneId.systemDefault())));

Unfortunately, short zone names like PST and IST are ambiguous (check this list), so you need to translate them to IANA's names (which are the standard used by Java, such as America/Los_Angeles or Europe/London).

More about timezones: https://codeofmatt.com/2015/02/07/what-is-a-time-zone