Results 1 to 11 of 11

Thread: Comparing Dates

  1. #1
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Comparing Dates

    Hi,

    I am trying to write a method to compare date of births to one another. The code below is a method called compareTo(Date d) which takes in a Date object and tries to compare the date of birth of the another Date object?
    Thanks

    Java Code:
    public int compareTo(Date d)
    	{
    		if(year > d.year || month > d.month || day > d.day)
    			return 1;
    		else if(year < d.year || month < d.month || day < d.day)
    			return -1;
    		else
    		return 0;
    	}

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Comparing Dates

    What output are you seeing when you test it out, what is your question?

  3. #3
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Comparing Dates

    I am using this method in a sorting algorithm to try and sort the dates from youngest to oldest? And my outputs are not sorted which is below.
    Thanks


    1988/3/3
    18/9/1988
    17/9/1988
    21/9/1976
    2/3/1985
    6/4/2
    Bubble Sorted Dates Below
    6/4/2
    2/3/1985
    21/9/1976
    17/9/1988
    18/9/1988
    1988/3/3

    I wasnt too sure what code tags I could place the output on?

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Comparing Dates

    For one thing, it doesn't look like your dates are all formatted the same. What date is 6/4/2.
    I would first look at your Date class, and make sure that it is in order first.

    Where are you getting these dates from, is it a text file?

  5. #5
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Comparing Dates

    Well Im not concerned on the format of the dates, Il worry about that when I can get them sorting correctly, like I mean dates that are not correctly formatted will not effect the output of the result (They are just integers). I know that my compareTo(Date d) method is not correctly comparing the dates correctly, thats what I'd like to figure out?
    No I am not using a text file but scanning them in from the keyboard when the program is in runtime.

    Thanks

  6. #6
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Comparing Dates

    So what does your bubble sorting routine look like?

  7. #7
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Comparing Dates

    The bubble sort method looks as follows below:
    Thanks again

    Java Code:
    public static ArrayList<Date> bubbleSort(ArrayList<Date> dateList)
    	{
    		Date temp;
    		int size = dateList.size();
    	
    		for(int x=0; x<size; x++)
    		{
    			for(int i=1; i<size-x; i++)
    			{
    				if(dateList.get(i-1).compareTo(dateList.get(i)) > 0)
    				{
    					temp = dateList.get(i);
    					dateList.set(i, dateList.get(i-1));
    					dateList.set(i-1, temp);
    				}
    			}
    		}
    		return dateList;
    	}

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Comparing Dates

    Quote Originally Posted by dougie1809 View Post
    Hi,

    I am trying to write a method to compare date of births to one another. The code below is a method called compareTo(Date d) which takes in a Date object and tries to compare the date of birth of the another Date object?
    Thanks

    Java Code:
    public int compareTo(Date d)
    	{
    		if(year > d.year || month > d.month || day > d.day)
    			return 1;
    		else if(year < d.year || month < d.month || day < d.day)
    			return -1;
    		else
    		return 0;
    	}
    That first if will consider a Date greater than another Date if either the year, month or day are greater.
    So 15/07/2000 is after 12/12/2020 according to that logic.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Comparing Dates

    Yes, thats my problem, a date could be ordered after another date if the day is higher, but does not take in the year. How could I get around that, so it will be looking at the date, and then the month, and then the day to determine who is younger to place in before an older person?
    Thanks

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Comparing Dates

    You need to expand your comparison.
    If year1 > year 2 return 1.
    else year1 == year 2 and month1 > month2 return 1...
    etc.
    There'll be better ways of organising it, but you need to get the base cases down.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Comparing Dates

    Cheer that worked perfectly. I couldn't think of the logic of the method.
    Thanks again

Similar Threads

  1. Comparing dates in java
    By Dominic in forum New To Java
    Replies: 4
    Last Post: 11-15-2011, 04:04 PM
  2. comparing Graphs and Comparing Matrix
    By jetnor in forum New To Java
    Replies: 0
    Last Post: 03-27-2011, 02:40 AM
  3. comparing dates
    By palls in forum New To Java
    Replies: 3
    Last Post: 08-13-2010, 02:08 PM
  4. UTC Dates
    By PedroCosta in forum Advanced Java
    Replies: 3
    Last Post: 04-01-2010, 07:08 PM
  5. Comparing dates
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-28-2008, 10:02 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
  •