How to convert a datetime value to UTC (or GMT)
I have this problem.
The user should provide the following inputs:
- A date time value - i.e: 10 september 2006, 10:40 AM
- The world location to which that value refers - i.e.: Rome/Italy
I need as output, the UTC date time value:
- i.e: 10 september 2006, 8:40 AM
in this case it is two hours before because during summer time, the Italian timezone is GMT+2
And if the user enters:
- A date time value - i.e: 25 december 2006, 10:40 AM
- The world location to which that value refers - i.e.: Rome/Italy
The output should be: (UTC time)
- i.e: 25 december 2006, 9:40 AM
It only only one hour before because during winter, in italy the timezone is GMT+1
In few words I need that Java can do by itself all the calculations about the timezone and daylight saving around different places in the world, so that I can store in my DB the universal UTC time.
Can you suggest me some code or some good URLs?
Thanks in advance
You can do a lot with Calendar and its time zones - it knows offsets and DST rules. How to map from "Rome" to "CET" I don't know, though.
Leon,
Is this a webapp or a fatclient?
either way you're talking...
In a webapp, extract locale from request headers... Just not allways set, and not all browsers support it.
To calculate:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Locale.html
http://java.sun.com/j2se/1.4.2/docs/api/java/util/TimeZone.html
The user will manually choose its city.The locale is not enough because in the same locale there are more timezone of the change dates for daylight saving time can change.
> You can do a lot with Calendar and its time zones - it knows offsets and DST rules.
Bingo! http://javaalmanac.com/egs/java.util/GetTimeOtherZone2.html
> How to map from "Rome" to "CET" I don't know, though.
My guess is getTimeZone("CET")
Bingo! http://forum.java.sun.com/thread.jspa?threadID=705128&tstart=210
So I guess you need to maintain a GMT+ table? and lookup the city and then +/-GMT offset to give you UTC...... or ... which database are you using ... some have GMT time functions built-in.
> > How to map from "Rome" to "CET" I don't know,> though.> My guess is getTimeZone("CET")My point was rather:getTimeZone("Rome"); // ?The time zone is not directly provided, according to the OP.
Leon,
> The user will manually choose its city.
> The locale is not enough because in the same locale
> there are more timezone of the change dates for
> daylight saving time can change.
To me that says manually maintaining a list of current GMT offsets in database, and forcing the user to tell you where in the world they are... and a lot people will bullshit you on principle. I would.
Surely that's not the prefered option... there's just got to be a better way... unless it's a requirement for other reasons?
For a GUI app maybe just get Z (timezone) fromformatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
s = formatter.format(date);
// Tue, 09 Jan 2002 22:14:02 [b]-0500[/b]
For a webapp :
... look at page source of http://www.time.gov/timezone.cgi?UTC/s/0/java
... it's all done in javascript.
Keith.
I wrote this code, it works but I have still some issues.
GregorianCalendar abroad = new GregorianCalendar(TimeZone.getTimeZone("Asia/Tokyo"));
abroad.set(Calendar.YEAR, 2006);
abroad.set(Calendar.MONTH, 11);
abroad.set(Calendar.DATE, 25);
abroad.set(Calendar.HOUR_OF_DAY, 1);
abroad.set(Calendar.MINUTE, 0);
abroad.set(Calendar.SECOND, 0);
System.out.println(abroad.getTime());
DateFormat df1 = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
df1.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));
System.out.println(df1.format(abroad.getTime()));
DateFormat df2 = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
df2.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println(df2.format(abroad.getTime()));
DateFormat df3 = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
df3.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
System.out.println(df3.format(abroad.getTime()));
It outputs:
Sun Dec 24 17:00:00 CET 2006
luned?25 dicembre 2006 1.00.00 JST
domenica 24 dicembre 2006 11.00.00 EST
domenica 24 dicembre 2006 16.00.00 UTC
In few words this code is able to compute any date from a specific timezone, to other timezone.
My questions are:
- Why the first output is in english ? and the other are in italian? (my OS is italian) but I didn't changed the locale in the code, so I don't understand why it outputs english text.
- When I get the date value in other timezones, I just get a "string" computed by the "format(...)" method of the DateFormat class.
But I really need to get something "numeric" for every specific value: minutes, hours, day of the month, month, year. Because this class will be used outside java and I don't need a string but something that the user will have formatted in his choosen locale setting for its website. (i think that I was not good to explain this point, i hope that someone can understand)
However thank you all :)
