Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

comparing dates in Java

  • 17-12-2001 04:47PM
    #1
    Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭


    Folks,

    I want to compare 2 dates, in java, and get the number of days in between them. I've never worked with the calendar class and am kinda stuck. Any advice on where I should start?


Comments

  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Not sure about the Calendar class for this but you could just use the Date class and call getTime() for each one, then do the math yourself.


  • Registered Users, Registered Users 2 Posts: 932 ✭✭✭yossarin


    yeah - i was looking at the calender class + its not as straightforward as i thought:

    a quick
    Calender ans = new Calender();
    ans.setTimeInMillis(Date1.getTime()-Date2.getTime());

    will give you the long between those dates as a calender instance

    int year = ans.get(Calender.YEAR);
    int dayOfYear = ans.get(Calender.DAY_OF_YEAR);

    this would work for dates in the same year, but then you'd have to mess where the dates were in different years (+ leap years :()

    also don't forget to check for AD or BC :)

    thats a bit complicated though ? theres probably a simpler way
    might be a start...
    yoss


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Ok what I did was use Dates. I set the Date objects with the dates in question, did a getTime() on both of them. Subtract one from the other then figure out how many days are in the difference.

    A lot simplier than the calander class.


Advertisement