Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Date arithmetic...I'm just a bit stumped. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Date arithmetic...I Watch "Date arithmetic...I New topic
Author

Date arithmetic...I'm just a bit stumped.

J. van Scoyoc
Greenhorn

Joined: Oct 01, 2003
Posts: 4
I'm trying to write a report utility for PVCS Tracker. Tracker uses an Oracle database, but all dates, when an issue is logged or a note added, are stored as integers rather than Oracle dates. The vendor explained to me that these integers are the number of seconds elapsed since 1/1/1970, and sure enough, when I look at one of these numbers and divide by (3600*24), I get what looks like the appropriate number of days. In this case, I get 12152 days, which turns out to be 33.29 years, and that is just about right with respect to the date on the Tracker item.
So if I have a java.util.Date object Baseline, which is set to 1/1/1970, shouldn't I be able to have:
int DaysToAdd = 12152;
java.util.Date TrackerEntryDate = new java.util.Date();
TrackerEntryDate.setDate(Baseline.getDate()+DaysToAdd);
But when I try this, TrackerEntryDate turns out to be sometime in 2037, which sounds just about twice as many days in the future as it should be.
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
Hi
This is because, when you create a Date object that way (no args constructor), it defaults to the current date and time. If you use the overloaded constructor that takes a long as an argument, you can initialise it to the start of the epoch (January 1, 1970, 00:00:00 GMT). So,

should do the trick (0 milliseconds since the epoch). You can then add your days as required.
I would also advise you to have a good read of the API docs for this class, as well as the Calendar & GregorianCalendar classes - they have some behaviour that you may find counter-intuitive.
Hope this helps
Michael


"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
J. van Scoyoc
Greenhorn

Joined: Oct 01, 2003
Posts: 4
Thanks, that looks like just what I need.
I haven't had a chance to try it yet, though due to other things that I had to attend to.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date arithmetic...I'm just a bit stumped.
 
Similar Threads
What fields does java.util.Date.equals compare?
timestamp
Date/Calendar without Year requirement
calculating times (please help!!)
Date class