This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

creating java.util.Calendar from time in seconds  RSS feed

 
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
 
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
 
Sheriff
Posts: 23640
48
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:
 
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
Posts: 23640
48
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!