Problems while retrieving the UNIX timestamp from the Calendar class

// Time according to the local timezone

Calendar c1 = Calendar.getInstance();

out.println("Local time: ");

out.println("Epoch time: " + c1.getTimeInMillis());

out.println("Hour: " + c1.get(c1.HOUR_OF_DAY));

out.println("Minute: " + c1.get(c1.MINUTE));

out.println("Second: " + c1.get(c1.SECOND));

// Time according to the Indian timezone

Calendar c2 = Calendar.getInstance(TimeZone.getTimeZone("Asia/Calcutta"));

out.println("India time: ");

out.println("Epoch time: " + c2.getTimeInMillis());

out.println("Hour: " + c2.get(c1.HOUR_OF_DAY));

out.println("Minute: " + c2.get(c1.MINUTE));

out.println("Second: " + c2.get(c1.SECOND));

Please read the code snippet given above.

First I get the Calendar object (variable c1 in the snippet) according to the local timezone. Then I get the Calendar object and set it to the Indian timezone (variable c2 in the snippet). The hours, minutes and seconds displayed by the Calendar object represented by c2 is correct according to the Indian timezone. However, getTimeInMillis() continues to display the "epoch time" according to the local timezone and doesn't show the it according to the Indian timezone.

Is this a bug or known issue with the implementation of the Calendar class ?

Any solutions or workarounds ?

Please HALP!

[1718 byte] By [ParampreetDhatta] at [2007-11-26 18:55:07]
# 1

> However, getTimeInMillis() continues to display the

> "epoch time" according to the local timezone and

> doesn't show the it according to the Indian

> timezone.

Correct.

> Is this a bug or known issue with the implementation

> of the Calendar class ?

How about reading the API which clearly says that the method returns UTC?

CeciNEstPasUnProgrammeura at 2007-7-9 20:33:01 > top of Java-index,Java Essentials,Java Programming...
# 2
No BUG! The time in both Data end Calendar is ALWAYS stored as the number of milliseconds since the the epoch (1st Jan 1970 UTC).
sabre150a at 2007-7-9 20:33:01 > top of Java-index,Java Essentials,Java Programming...
# 3
Ooops! My bad.However , is there any way to obtain the epoch time according to a particular timezone ?
ParampreetDhatta at 2007-7-9 20:33:01 > top of Java-index,Java Essentials,Java Programming...
# 4
> However , is there any way to obtain the epoch time> according to a particular timezone ?I bet some minimal math would do.
CeciNEstPasUnProgrammeura at 2007-7-9 20:33:01 > top of Java-index,Java Essentials,Java Programming...
# 5
Oh...Thanks for the help guys ! :)
ParampreetDhatta at 2007-7-9 20:33:01 > top of Java-index,Java Essentials,Java Programming...
# 6
Hint: you need to find the [url= http://java.sun.com/j2se/1.5.0/docs/api/java/util/TimeZone.html#getOffset(long)]offset[/url] of a given time zone from UTC.
DrLaszloJamfa at 2007-7-9 20:33:01 > top of Java-index,Java Essentials,Java Programming...