conversion of date whith other timezone
i have for example the date:
09 JUL 2006 15:36:15 in MET-1METDST TimeZone
which can be convert to a String 20060709153615.
i want to have a method to convert this date to an other
TimeZone GMT for example in other to get a String result like:
20060709133615 which correspond to 09 JUL 2006 13:36:15 in GMT TimeZone.
we should take into account day summer time.
thanks.
[417 byte] By [
kimo100a] at [2007-10-3 1:25:58]

java.util.Date objects always hold the date and time as an "epoch time", which is GMT baseed. Converting between this format an a text format is done with java.text.DateFormat objects and conversion for timezone is done at that point. Two SimpleDateFormat objects with different time zone settings will do the job for you.
A Date object represents a moment in time. You don't have to convert the date to display it in another time zone. If you're in GMT and I'm in PDT, we can stil talk on the phone; there isn't a 7 hour delay in our conversation.
What you do is use a DateFormat that's been configured for the appropriate time zone. So, perhaps, you could create two SimpleDateFormat objects, configure one for GMT and one for PDT. Then, you could display the same Date object using both formatters, for example.
Getting a different milliseconds since epoch value for different timezones would violate the meaning of the value and thus lead to bugs.I'd suggest that you not do that.
Ah, ****, too slow again.
Message was edited by:
paulcw
String dateAsString = "09 JUL 2006 15:36:15";
SimpleDateFormat parser = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
parser.setTimeZone(TimeZone.getTimeZone("MET-1METDST"));
Date date = parser.parse(dateAsString);
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("EST")); // Or GMT or other
System.out.println(dateAsString + " -> " + formatter.format(date));
i use this code:
try
{
String dateAsString = "09 JUL 2006 15:36:15";
SimpleDateFormat parser = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
parser.setTimeZone(TimeZone.getDefault());
Date date1 = parser.parse(dateAsString);
trace_.lowTrace (" date1 -> " + parser.format(date1));
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("GMT")); // Or GMT or other
Date date2 = formatter.parse(dateAsString);
trace_.lowTrace (" date2 -> " + formatter.format(date2));
}
catch(Exception e)
{
}
but i get always the same result:
date1 -> 09 Jul 2006 15:36:15
date2 -> 09 Jul 2006 15:36:15
i have MET-1METDST timezone.
Well, of course. You have two different parsers parsing and then re-displaying the same String. Internally they think they're looking at different times but they're printing the same thing externally.
Look: if you were in London, and at 3:00pm London time you asked someone what time it is, they'd say 3:00pm. If you were in New York and at 3:00pm New York time asked someone what time it is, they'd say 3:00pm. But they're different 3:00pm.
But if you were in London, and at 3:00pm you CALLED NEW YORK and asked what time it is, they'd say 10:00am (or whatever).
Create a string that gives a time. Parse it with a SimpleDateFormat configured with one time zone. Get the Date object from the parser. Now, use a DIFFERENT SimpleDateFormat, configured with a DIFFERENT time zone, and format the Date object (do not re-parse the same string).
it works well.thanks a lot.
> i have MET-1METDST timezone.You chose to ingore this in my example. Why?
TimeZone.getDefault() = MET-1METDST timezone for me.the problem was that i should format the same date.thanks .
Hi folks,I am using code snippet added by sabre150.But I am not able to add day 'daylight savings' to the time.Does any body know how to add it.Thanks.