Results 1 to 8 of 8
  1. #1
    Sharke is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default Confused about date objects

    I have the following method of an invoice object:

    Java Code:
    public Date getDueDate()
    	{
    		long current = invoiceDate.getTime();
    		Date date =  new Date(current + (30 * 24 * 60 * 60 * 1000));
    		return date;
    	}
    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?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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)

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  4. #4
    Sharke is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    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 ;)

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    I hope you'll understand that cross-posting is rude. Until then I think you can forget about any further help.

  6. #6
    Sharke is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    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.

  7. #7
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Quote Originally Posted by Sharke View Post
    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.
    No, a simple "I posted this question on another forum" would have sufficed. Masijade was kind enough to voluntarily spend his time answering your question, when the question was already answered.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  8. #8
    Sharke is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by m00nchile View Post
    No, a simple "I posted this question on another forum" would have sufficed. Masijade was kind enough to voluntarily spend his time answering your question, when the question was already answered.
    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

  1. Flickr Api - trouble return Date objects
    By red_beardo in forum New To Java
    Replies: 5
    Last Post: 04-27-2010, 05:06 PM
  2. julian date to full date format
    By judy318 in forum New To Java
    Replies: 7
    Last Post: 11-02-2009, 01:17 PM
  3. Compare date input to database with current date
    By hungleon88 in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 09:10 AM
  4. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 09:32 PM
  5. Difference between current date and anothe date
    By vijay balusamy in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 05:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •