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

Using dates in Java..........

Options
  • 28-08-2007 12:17pm
    #1
    Closed Accounts Posts: 27,857 ✭✭✭✭


    Hey folks,

    I'm working on a program here, and dates are turning out to be a major pain in the ass!

    I'm making a program to store info about members of a club. One bit of info I want to store is the date they joined on.

    Then I want to compare that date with the current date, and if there's a year or more in the difference, return true.

    I've read umpteen articles about it but it's still not registering! :(

    Here's a little thing I did to try and learn a bit, but it keeps saying FALSE, even though the dates are the same (so it should be saying TRUE). Is the problem that if you create a new GregorianCalendar without the arguments (year, month, date), that it uses different arguments?
    import java.util.*;
    import java.text.*;
    
    public class DateTest
    {
    	public static void main(String[] args)
    	{
    		GregorianCalendar firstFlight = new GregorianCalendar(2007, Calendar.AUGUST, 28);
    
    		GregorianCalendar thisday = new GregorianCalendar();
    
    		if (thisday.equals(firstFlight)) {
    			System.out.println("TRUE");
    		} else {
    			System.out.println("FALSE");
    		}
    
    	}
    }
    

    Cheers, and thanks for any help!


Comments

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


    if (thisday.equals(firstFlight))
    

    Isn't that comparing the reference? This will always return false as the thisday and firstFlight are seperate objects. You need to do a date comparison. Google Java Date Compare and you'll get a solution.

    I'd also say you need to initial thisDay with the current date - at least you would in C#. <edit>It seems GregorianCalendar initialises with the current date by default</edit>


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    Evil Phil wrote:
    if (thisday.equals(firstFlight))
    

    Isn't that comparing the reference?
    It shouldn't be.

    == compares references.

    .equals says whether two objects are "meaningfully equal".

    My guess (from a quick look at the javadocs) is that the empty constructor works with current date, down to the second, whereas the year/month/day constructor defaults to midnight of the specified date.

    .equals is then comparing to the second, and finding that the two values are unequal....cause they are.

    For reference, I'm basing this on the fact that a constructor exists which takes year/month/day/hour/minute/second.

    jc


  • Closed Accounts Posts: 362 ✭✭information


    You are comparing dates including time so it will never be the same
    a couple of solutions below
    public static void main(String[] args) {
            
            Calendar ff = Calendar.getInstance();
            ff.set(2007,7,28);
            Calendar td = Calendar.getInstance();
            if(td.equals(ff))
                System.out.println("TRUE");
            else
                System.out.println("FALSE");
            
            GregorianCalendar firstFlight = new GregorianCalendar(2007, 7, 28);
            GregorianCalendar thisday = new GregorianCalendar();        
            int year = thisday.get(GregorianCalendar.YEAR);
            int month = thisday.get(GregorianCalendar.MONTH);
            int day = thisday.get(GregorianCalendar.DAY_OF_MONTH);
            GregorianCalendar check = new GregorianCalendar(year,month,day);        
            if (check.equals(firstFlight))
                System.out.println("TRUE");
            else
                System.out.println("FALSE");
            
        }
    


Advertisement