Hello,
I want some code which will give me the number of milliseconds since
the Java epoch which is January 1, 1970. I try the following code, but,
alas, every time I run it I get a different value
instead of a constant. Any ideas of what
might be going wrong would be
sincerely appreciated.
import java.util.Calendar;
class T {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 28);
calendar.set(Calendar.MONTH, 2);
calendar.set(Calendar.YEAR, 2006);
long millis = calendar.getTime().getTime();
System.err.println(millis);
}
}
Daniel Dyer - 19 Feb 2006 23:12 GMT
> Hello,
>
[quoted text clipped - 4 lines]
> might be going wrong would be
> sincerely appreciated.
Calendar.getInstance() returns a Calendar with its fields set to the
current date and time. You are only re-setting the date fields. The
other fields (hours, minutes, seconds, etc.) remain unchanged from the
current date/time, which is different every time you run the code.
Dan.

Signature
Daniel Dyer
http://www.dandyer.co.uk
David N. Welton - 20 Feb 2006 07:35 GMT
> Hello,
>
[quoted text clipped - 4 lines]
> might be going wrong would be
> sincerely appreciated.
Time waits for no man, nor for J2ME:-)

Signature
David N. Welton
- http://www.dedasys.com/davidw/
Linux, Open Source Consulting
- http://www.dedasys.com/
Darryl L. Pierce - 21 Feb 2006 14:36 GMT
> I want some code which will give me the number of milliseconds since
> the Java epoch which is January 1, 1970.
System.currentTimeMillis()
> I try the following code, but,
> alas, every time I run it I get a different value
> instead of a constant. Any ideas of what
> might be going wrong would be
> sincerely appreciated.
It's not a constant since time changes. You're setting a data but not a
time, so the time value is whatever the moment is of the current day.

Signature
Darryl L. Pierce <mcpierce@gmail.com>
Homepage: http://mcpierce.multiply.com/
"McVeigh's lawyer got him the death penalty, which, quite frankly,
I could have done." - Jon Stewart
Roedy Green - 24 Feb 2006 20:56 GMT
>. I try the following code, but,
>alas, every time I run it I get a different value
>instead of a constant. Any ideas of what
>might be going wrong would be
>sincerely appreciated.
that is because you did not specify the time. I take it the fool
thing assumed current time rather than 0:00 or 12:00 .

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.