File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Working with Date and Time

 
Help Me
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Help Me
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just converting from milliseconds to years. Well..I got the point now. Thanks a lot!

Regards,
Babu Rajendran
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Babu Rajendran:
I'm just converting from milliseconds to years.

A year is not 12 * 30 = 360 days.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic