aspose file tools*
The moose likes Java in General and the fly likes creating java.util.Calendar from time in seconds Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "creating java.util.Calendar from time in seconds" Watch "creating java.util.Calendar from time in seconds" New topic
Author

creating java.util.Calendar from time in seconds

shankar alagiri
Greenhorn

Joined: Dec 01, 2006
Posts: 4
Hello,

I want to find the date and time using java.util.calendar api.
The parameter that is know to me is Time in seconds.

thanks,
Shankar.A
Ådne Brunborg
Ranch Hand

Joined: Aug 05, 2005
Posts: 208
Basically, you use the fact that there is always 1000 milliseconds in a second:



Or, if you just want the Date object, you don't need to bother with Calendar:


[ December 01, 2006: Message edited by: �dne Brunborg ]

Entia non sunt multiplicanda praeter necessitatem
shankar alagiri
Greenhorn

Joined: Dec 01, 2006
Posts: 4
Hi Brunborg,

Thank You for your response,

long timeInMillis = 1995181298*1000;
Calendar myCalendar = Calendar.getInstance();
myCalendar.setTimeInMillis(timeInMillis);
Date theDate = myCalendar.getTime();

This is the exact piece of code i'm using to get the data & time for a given time in seconds.

The following are input data and corresponding output i'm getting,

3159720303 Sat Feb 15 13:03:03 GMT-07:00 2070

3159720205 Sat Feb 15 13:03:25 GMT-07:00 2070

It seems to me the output is not right.

Please help

thanks,
Shankar.A
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

Try using long literals in your calculation so you don't drop the most significant bits when the compiler decides you wanted ints:
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Shankar]: The parameter that is know to me is Time in seconds.

Time in seconds... since what? What would a value of 0 mean here? Where do these numbers come from?

1995181298 seconds is approximately 63 years. And 3159720303 seconds is approximately 100 years. What dates are these values supposed to represent? From that you can tell (hopefully) what date was 63 or 100 years earlier, and then we'll be able to do some math to correct the output of your program. Or possibly, if this isn't making sense, maybe these aren't really times in seconds at all?


"I'm not back." - Bill Harding, Twister
shankar alagiri
Greenhorn

Joined: Dec 01, 2006
Posts: 4
Hi,

Thanks All,

long timeInMillis = 1995181298L*1000L;


I tried using the above expression and there is no change in the output.

I'm coding a Network managemant Application which needs to calculate the date & time of an event occured in the Network Element.

The network time in seconds represents when that event occured in the network element.

Thanks.
Shankar.A
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

Then Jim Yingst's response is the one you have to answer before any progress can be made.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: creating java.util.Calendar from time in seconds