Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

comparing dates in Java

  • 17-12-2001 3: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