Results 1 to 8 of 8
Thread: Confused about date objects
- 05-09-2010, 10:32 PM #1
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
Confused about date objects
I have the following method of an invoice object:
I mean to add 30 days to the invoice date and return that as the due date. However, the code above subtracts 30 days from the invoice date instead of adding it. Upon experimenting further I found that adding anything up to 24 days worth of milliseconds will add to the invoice date, whereas 25 days or over will be subtracted. Being new to Java I have absolutely no idea why this should be so or where I'm going wrong. Can anyone help?Java Code:public Date getDueDate() { long current = invoiceDate.getTime(); Date date = new Date(current + (30 * 24 * 60 * 60 * 1000)); return date; }
- 05-09-2010, 11:34 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
All of those numbers in the parens "(30 * 24 * 60 * 60 * 1000)" are integers, making the result an integer, which means a max value of 2,147,483,647 and that formula produces 2,592,000,000, so the value "overflows" and 2,147,483,648 is actually -2,147,483,648 making your result -444,516,352. Do that operation with longs, rather than integers, and you'll be okay. i.e.
Java Code:(30L * 24L * 60L * 60L * 1000L)
- 05-09-2010, 11:43 PM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
cross-posted
so I just wasted my time being nice and helpful enough to craft a detailed explanation and solution for you seeing as how you weren't mannered enough to alert the users here of your parallel thread.
- 05-09-2010, 11:48 PM #4
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
Hey thanks for the answer! It all makes sense now. I'm sorry if the fact that I asked the question elsewhere affected you so emotionally, I hope one day you'll be able to get over it ;)
- 05-09-2010, 11:51 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
I hope you'll understand that cross-posting is rude. Until then I think you can forget about any further help.
- 05-10-2010, 12:09 AM #6
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
A simple "oh and by the way, cross posting is considered rude, please don't do it again" would have sufficed. But I guess overreacting is more fun, so whatever. Thanks for the answer though, I had been mistaken in thinking that there was some type conversion going on behind the scenes.
- 05-10-2010, 07:05 AM #7
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
- 05-10-2010, 07:24 AM #8
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
I explained exactly what happened - and why I'm not such an inconsiderate a-hole after all - on the other forum Masijade linked to, so I'm not going to bother going through all that rigmarole again. I still think he overreacted in a manner that was terse and unfriendly, but hey, that's pretty much par for the course in most programming forums. Newbies get snapped at for making some minor forum faux-pas, yada yada. Life goes on.
Similar Threads
-
Flickr Api - trouble return Date objects
By red_beardo in forum New To JavaReplies: 5Last Post: 04-27-2010, 04:06 PM -
julian date to full date format
By judy318 in forum New To JavaReplies: 7Last Post: 11-02-2009, 12:17 PM -
Compare date input to database with current date
By hungleon88 in forum Advanced JavaReplies: 2Last Post: 11-25-2008, 08:10 AM -
Creating a Gregorian Calendar using a Date object gives date - 1
By prachi_goliwadekar in forum New To JavaReplies: 1Last Post: 05-08-2008, 08:32 PM -
Difference between current date and anothe date
By vijay balusamy in forum New To JavaReplies: 1Last Post: 04-16-2008, 04:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks