Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

J2ME milliseconds from epoch to given date

Thread view: 
John Goche - 19 Feb 2006 23:06 GMT
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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.