J2ME milliseconds from epoch to given date

J

John Goche

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);

}

}
 
D

Daniel Dyer

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.

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.
 
D

David N. Welton

John said:
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.

Time waits for no man, nor for J2ME:)

--
David N. Welton
- http://www.dedasys.com/davidw/

Linux, Open Source Consulting
- http://www.dedasys.com/
 
D

Darryl L. Pierce

John said:
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.

--
Darryl L. Pierce <[email protected]>
Homepage: http://mcpierce.multiply.com/
"McVeigh's lawyer got him the death penalty, which, quite frankly,
I could have done." - Jon Stewart
*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
 
R

Roedy Green

. 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 .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top