Format date time in gwt with timezone - gwt

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

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.

Related

Does the creation of a date object accept a parameter for a timezone?

To my understanding let date = Date() would create a date object with my current timezone. I've seen that there are certain functions where I would use a date formatter to then convert it into UTC time and add the offset to that time and then convert back into a date object but I'm wondering if there is a more elegant solution? And if not is there any other way to get the current time in another timezone in a date object without using the data formatter, as I require the calendar.component(.weekday, from: date) to find the date of the week.
If you look at the very first line on the documentation on Date, you can read the following:
A specific point in time, independent of any calendar or time zone.
So your two options when you want to deal with a local date are the DateTimeFormatter and Calendar

How to fix dates lagging one day behind in calendar

I'm developing a Clio integration with access to the calendar, but there's been an issue with dates. While the documentation says they expect an ISO-8601 timestamp date, it seems like there's something adding offset to the timezone value in dates being sent to the system.
For example, if I send a date 2018-05-17T23:59:59.999999-04:00 on both start_at and end_at properties when creating a calendar entry for an all day event, the value returned when fetching this entry through the API is 2018-05-17T17:00:00-07:00, which is clearly wrong. Am I missing something here?
The expected result should be something like either 2018-05-17T23:59:59-04:00 or 2018-05-18T03:59:59Z if milliseconds are ignored.
All dates are based on UTC timezone. Could it be that your site/server/script is set to a local timezone and so the dates are off for part of the day?
Try setting your scripting environment to UTC time before making any date/time-based queries.

Turning a localized Date to a Timestamp in Firebase

I'm new to Firestore and since it doesn't seem like it has a native createdAt/updatedAt as part of a document, I'm creating them when I create the new document. Using a straight up Date() as the value for my initial dictionary that I save to Firestore obviously gives me a localized date/time – UTC -500, for example.
Firestore stores this as November 19, 2018 at 5:14:54 PM UTC-5
Is there a specific date format that Firestore likes in order to save something as a Timestamp and so that I will be able to sort on it later?
I tried using "yyyy-MM-dd HH:mm:ss Z" as the dateFormat from just printing out the value of Date() in a Playground. Would love some help here. Thanks!
A Timestamp object doesn't have a date format. It's just a measurement of a number of seconds since Unix epoch, plus some number of nanoseconds. It does not accept a formatted time. If you have a formatted time, you will have to parse it to get a Timestamp object in return.`
The date format you're seeing in the console is just the way the console is choosing to format it. If you want to format a Timestamp for display, you'll need to do that yourself.

ASP.NET MVC Handle Timezones Entity Framework

I like to save the current DateTime.Now for the current user as UTC. For any user the time is than relative to each other ? I used this code snippet to realize UTC for the Entity Framework:
Entity Framework DateTime and UTC
After a look into the database the column for the date has not changed.
09.05.2014 20:21:24
I need to show on the client side the date relative to each user. So when the user created his account in America 14:00 I like to see in Europe 20:00.
First of all you need to use DateTime.UtcNow instead of DateTime.Now. EF will save date as it is, it will not do anything.
For database, it is just a date, whether it is UTC or specific time zone, database has no idea. It is only when we retrieve date from database, before displaying it to local user, we should call ToLocalTime or similar method in client UI library to convert UTC into localtime. ToLocalTime assumes that input is actually UTC.
If you are saving date from client side, for example with AJAX post in JavaScript, JavaScript dates should be serialized as UTC.
Remember that server has no idea from where you are sending date, server cannot convert any date to UTC, client on other hand has complete idea of which country and which timezone it is set at and it can easily convert local time into UTC.
Calling .ToLocalTime() on a DateTime object will convert it to the local time. You should always Save the date as UTC then convert upon display.

Date and timezone using j2me

I have a date string as input from an rss like:
Wed, 23 Dec 2009 13:30:14 GMT
I want to fetch only the time-part, but it should be correct according to the my timezone. Ie, in Sweden the output should be:
14:30:14
What is the best way? I want it to work with other RSS date formats as well if possible. Im using regexp right now but that is not very general.
Im having a hard time finding any library or support for dates and timezones in j2me?
/Br Johannes
In normal Java, I'd use a SimpleDateFormat object (that you create to match the pattern of the date you're getting in the RSS) to parse the String value and give you a Date object. Then, using a Calendar (which would be localized to your time zone), you'd be able to get the hour, minute, and second information.
I don't know j2me, but a google search suggests that these classes are available in j2me.