Results 1 to 18 of 18
Like Tree1Likes
  • 1 Post By rwhitney

Thread: Difference in days between dates

  1. #1
    peter_62au is offline Member
    Join Date
    Dec 2013
    Posts
    18
    Rep Power
    0

    Default Difference in days between dates

    Hi,
    I know this has been covered before but none of the answers made sense to me. I'm hoping there is an easy way to do this. I have 2 user inputted strings that I have converted to dates and I just want the difference in days.

    My code :

    import java.util.Date;
    import java.text.SimpleDateFormat;
    import java.util.Scanner;
    import java.text.ParseException;


    public class DateTest {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) throws ParseException {

    Scanner myScanner = new Scanner(System.in);

    String lstStart, thisStart; // Dates, user input

    long DaysBetween;


    System.out.println("Date of last start");
    lstStart = myScanner.nextLine();

    System.out.println("Date of This start");
    thisStart = myScanner.nextLine();




    Date oldDate = new SimpleDateFormat("dd/MM/yy").parse(lstStart);

    Date newDate = new SimpleDateFormat("dd/MM/yy").parse(thisStart);

    DaysBetween = newDate - oldDate;


    Apparently I can't just subtract the dates like I would in VB.

    Thanks in advance.

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,000
    Rep Power
    6

    Default Re: Difference in days between dates

    Quote Originally Posted by peter_62au View Post
    Hi,
    I'm hoping there is an easy way to do this.
    Huh? So you want to assume that until now any answer you have found on the net was purposely made difficult?

    Seems to me like it is simple math. You can get the number of milliseconds between the two dates (I'm not going to spoonfeed that, I am going to assume you know how to find and read the API documentation). I'm sure you can translate that to seconds, hours and then days.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    peter_62au is offline Member
    Join Date
    Dec 2013
    Posts
    18
    Rep Power
    0

    Default Re: Difference in days between dates

    No I didn't assume any answer I found was purposley made difficult.
    I just assumed, wrongly it seems, that there would be a simple way just to get the days without having to go down to milliseconds and then having to multiply that back up to days.

    Thanks for your time.

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,000
    Rep Power
    6

    Default Re: Difference in days between dates

    It is VERY simple. Its a you-problem.

    Of course there is such a thing as the Jodatime API which has all kinds of methods to do calculations on dates, but that would require you to learn how to add third party APIs to your application.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    peter_62au is offline Member
    Join Date
    Dec 2013
    Posts
    18
    Rep Power
    0

    Default Re: Difference in days between dates

    Fine, it's a me-problem I'll keep reading.

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,000
    Rep Power
    6

    Default Re: Difference in days between dates

    Try thinking about it too, you have a brain and the solution can come from yourself too, it doesn't necessarily have to be provided to you by someone else (in fact: I hope the former becomes true).

    Say you have two sets of seconds. How would you on paper (or in your head) calculate how many days of difference there are between those two sets of seconds? The Java code you have to write will be very similar to the simple multiplication calculations you'd do.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    peter_62au is offline Member
    Join Date
    Dec 2013
    Posts
    18
    Rep Power
    0

    Default Re: Difference in days between dates

    Well for anyone else who's interested here is how I finally managed to do it, don't know if it's the best way but it works.

    Calendar calendar1 = Calendar.getInstance();
    Calendar calendar2 = Calendar.getInstance();
    calendar1.setTime(oldDate);
    calendar2.setTime(newDate);
    long milliseconds1 = calendar1.getTimeInMillis();
    long milliseconds2 = calendar2.getTimeInMillis();
    long diff = milliseconds2 - milliseconds1;
    long diffDays = diff / (24 * 60 * 60 * 1000);

    However I hardly think that this is an easy way given that it requires 8 statements to perform what is essentilly a simple subtraction.

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Difference in days between dates

    Java Code:
    long milliseconds1 = oldDate.getTime();
    Just a hint.
    Indeed you can completely skip the intermediate 'millsecondsx' variables.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    peter_62au is offline Member
    Join Date
    Dec 2013
    Posts
    18
    Rep Power
    0

    Default Re: Difference in days between dates

    Thank you so much.
    This is what I've got now.

    DaysBetween = (newDate.getTime() - oldDate.getTime())/(24 * 60 * 60 * 1000);

    I thought there must be a way to do it in 1 statement I obviously wasn't looking in the right place.

    Thanks again.

  10. #10
    paul59539 is offline Member
    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Default Re: Difference in days between dates

    Finally, i found the answers that i have been searching... Thanks.

  11. #11
    rwhitney is offline Member
    Join Date
    Jun 2014
    Posts
    4
    Rep Power
    0

    Default Re: Difference in days between dates

    How and where exactly to you enter this into the text field properties? Or do they go somewhere else? I am obviously really new to this... I am hoping to avoid the abuse you received in your earlier postings ;)

  12. #12
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Difference in days between dates

    Enter what into what text fields?
    The OP's code reads a date in from the console in dd/MM/yy format.
    The result is presumably written to the console, though that part isn't shown.

    And believe me that wasn't abuse in any shape or form. Gimbal got the OP to sit and think about the problem and solve it themselves. That last post (#10) is a eureka moment that wouldn't have happened otherwise.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,000
    Rep Power
    6

    Default Re: Difference in days between dates

    Quote Originally Posted by rwhitney View Post
    I am hoping to avoid the abuse you received in your earlier postings ;)
    There has been no abuse, but what you say here is pretty abusive in itself.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    rwhitney is offline Member
    Join Date
    Jun 2014
    Posts
    4
    Rep Power
    0

    Default Re: Difference in days between dates

    Quote Originally Posted by gimbal2 View Post
    There has been no abuse, but what you say here is pretty abusive in itself.
    I guess that is just how I took it at first, but Tolls made me see it from a different perspective. You wanted them to think it through, but what about people don't know anything about how to use the program? Do you push them the same way? I totally got the basic math thing, and in Excel I would have had it figured out in two seconds. I don't know anything about Java, but I work a lot with VBA. I was kind of hoping they were similar, but I can't figure it out AT ALL...
    gimbal2 likes this.

  15. #15
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,000
    Rep Power
    6

    Default Re: Difference in days between dates

    Quote Originally Posted by rwhitney View Post
    I guess that is just how I took it at first, but Tolls made me see it from a different perspective. You wanted them to think it through,
    Thank you!

    but what about people don't know anything about how to use the program? Do you push them the same way?
    Worse: I push them even more. Because people who don't know anything and still blunder ahead need a reality check; first learn how to walk, then try to run.

    Let me illustrate that a little:

    - you thought that knowing VB.NET could make you rush head-first into Java programming. Wrong.
    - the only way to learn Java is to properly learn Java - from start to finish. The start is a good book and an open mind.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  16. #16
    rwhitney is offline Member
    Join Date
    Jun 2014
    Posts
    4
    Rep Power
    0

    Default Re: Difference in days between dates

    Point taken! I'll be back once I educate myself a little more. Until then... manual calculation it is :) Nothing better than a bunch of construction guys calculating dates and percentages in the field...

  17. #17
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,000
    Rep Power
    6

    Default Re: Difference in days between dates

    I'm confused though, you're continuing this thread as if you started it.

    I did mention the JodaTime API way up there which can do it for you; but that requires knowing how to work with external dependencies and JodaTime is unconveniently not compatible with the standard Java Date class. So difficulty upon difficulty, I can't recommend that to people who are new to this.

    But since the start of this thread Java 8 has also been released which contains an expanded Date API which is an alternative to JodaTime.

    Java SE 8 Date and Time

    But again: its not exactly a trivial API to use.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  18. #18
    rwhitney is offline Member
    Join Date
    Jun 2014
    Posts
    4
    Rep Power
    0

    Default Re: Difference in days between dates

    There you go again... maybe I'm just a little sensative. I did not feel a need to start a new thread since my question was related to the one asked. Obviously mine was an uneducated question, but you helped me to see that. The code itself seems relatively easy to figure out (basically common sense like VBA)... I just need to learn how and where to enter it.

    I'll stop replying to this thread and create my own when I am ready again. Thank you for your time, and please stop being snappy... my feelings get hurt easily. Just a girl ;)

Similar Threads

  1. Replies: 14
    Last Post: 11-06-2012, 09:56 AM
  2. Replies: 1
    Last Post: 06-02-2012, 04:57 AM
  3. difference between two dates..
    By Shahnawaz in forum New To Java
    Replies: 2
    Last Post: 09-07-2011, 07:35 AM
  4. Best way to find number of days between 2 dates??
    By alinaqvi90 in forum Advanced Java
    Replies: 18
    Last Post: 11-22-2010, 08:49 AM
  5. No fo days between two dates
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-28-2008, 09:06 AM

Posting Permissions

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