Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

creating java.util.Calendar from time in seconds

 
shankar alagiri
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
shankar alagiri
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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?
 
shankar alagiri
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then Jim Yingst's response is the one you have to answer before any progress can be made.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic