| Author |
Working with Date and Time
|
Help Me
Greenhorn
Joined: Feb 28, 2006
Posts: 7
|
|
Hi, I've written the following code to calculate the elapsed time since birth. But on running it, the month and year are not getting displayed correctly. I'm getting a negative value for the month and also the elapsed year is wrong. Please help me... Thanks, Babu Rajendran
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
Can you explain what this calculation does? :
System.out.println("\t" + elapsedMS / (12 * 30 * 24 * 60 * 60 * 1000) + " years");
Specifically, what are you trying to achieve with the 30? Also, remember that any literal integers in your program will be treated as int values, and that ints have a maximum size. Try these two lines to watch what happens when you exceed this maximum: (the l in the above is a lowecase letter 'L') [ April 23, 2007: Message edited by: Paul Sturrock ]
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Help Me
Greenhorn
Joined: Feb 28, 2006
Posts: 7
|
|
I'm just converting from milliseconds to years. Well..I got the point now. Thanks a lot! Regards, Babu Rajendran
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
Originally posted by Babu Rajendran: I'm just converting from milliseconds to years. Well..I got the point now. Thanks a lot! Regards, Babu Rajendran
Are you sure your logic is correct?
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12911
|
|
Originally posted by Babu Rajendran: I'm just converting from milliseconds to years.
A year is not 12 * 30 = 360 days.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
Computing duration with months in it from milliseconds is suspect, I think. Start from January 31 of any year, what answers would you want from each of the following? jan31 + oneMonth = ?? jan31 + oneMonth + oneMonth = ?? jan31 + twoMonths = ?? jan31 + oneMonth - oneMonth = ?? Years might bother some folks, too. What's feb29 + oneYear? feb29 + oneYear - oneYear? I think you can get the answers humans expect for the difference between dates by breaking up the year, month, day parts of the date and working from there. What do you think?
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
 |
|
|
subject: Working with Date and Time
|
|
|