Mahes
Mahes

Reputation: 4125

Format date time in gwt with timezone

I am need to format a date and display it to users based on users location.

I am trying to format the time using the following code

DateTimeFormat.getFormat("h:mm a z").format(new Date(timeInMillis))

This is the result i am getting "5:18 PM UTC-4" for new york users and "2:18 PM UTC-7" for seattle users. How do i generate string like "5:18 PM EST" for new york users and "2:18 PM PDT" for seattle users?

Note: the problem with using format(new Date(time), timezone) is that how to create a timezone object based on user locale? Timezone.createTimeZone(int) gives SimpleTimeZone implementation which will produce "UTC-4", in-order to generate "PDT", timezone has to be created with Timezone.createTimeZone(timezoneJson: string) but issue in this is that we have to pick the timezone at the compile time to create the input timezone json string.

Thanks in advance

Upvotes: 0

Views: 1562

Answers (1)

Andrei Volgin
Andrei Volgin

Reputation: 41089

You need to pass a TimeZone object to the formatter:

format.format(new Date(), timeZone));

Note that the best way to create TimeZone is from a JSON string that contains information on changes to time zone in the past. If you app does not deal with time in the past, then this may not be necessary.

Upvotes: 1

Related Questions