Calendar to Date conversion using correct TimeZone
I get object of Date type from external storage, it's in UTC.
What I need is to convert it to TimeZone of Amsterdam. But I run this code in Ukraine (it's in different TimeZone than Amsterdam)
Date dateInUTC = getDateFromExternalStorage();
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("Europe/Amsterdam"));
calendar.setTime(dateInUTC); //after this I have got CORRECT date in the Calendar
Date dateNew = calendar.getTime();
My problem is that in dateNew I get not datetime in Amsterdam Timezone, but datetime in the TimeZone where the Java process is running (it's Ukraine for me).
Thank you for reading this post. I'm waiting for smart people's suggestion about the issue
[772 byte] By [
leokoma] at [2007-11-27 6:19:36]

> My problem is that in dateNew I get not datetime in
> Amsterdam Timezone, but datetime in the TimeZone
> where the Java process is running (it's Ukraine for
> me).
Date objects don't have a TimeZone.
http://java.sun.com/javase/6/docs/api/java/util/Date.html
Example:
import java.text.*;
import java.util.*;
class Foo {
public static void main(String[] args) throws Exception {
Date dateInUTC = getDateFromExternalStorage();
Calendar calendar = Calendar.getInstance();
TimeZone tz = TimeZone.getTimeZone("Europe/Amsterdam");
calendar.setTimeZone(tz);
calendar.setTime(dateInUTC); //after this I have got CORRECT date in the Calendar
Date dateNew = calendar.getTime();
DateFormat df = DateFormat.getInstance();
df.setTimeZone(tz);
System.out.println(df.format(dateNew));
}
static Date getDateFromExternalStorage() { return new Date(); }
}
~
> I need only converting to Date which has all fields inited with values suitable for target TimeZone.
The only relevant (e.g., non-deprecated), visible "field" of date is "time", a long value which represents the number of milliseconds since January 1, 1970, 00:00:00 GMT. Its value is suitable for any TimeZone. See reply #1.
~