Results 1 to 11 of 11
Thread: Comparing Dates
- 04-19-2012, 10:10 PM #1
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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; }
- 04-19-2012, 10:36 PM #2
Re: Comparing Dates
What output are you seeing when you test it out, what is your question?
- 04-19-2012, 10:42 PM #3
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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?
- 04-19-2012, 10:50 PM #4
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?
- 04-19-2012, 10:55 PM #5
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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
- 04-20-2012, 01:28 AM #6
Re: Comparing Dates
So what does your bubble sorting routine look like?
- 04-20-2012, 01:32 AM #7
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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; }
- 04-20-2012, 09:46 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,450
- Rep Power
- 16
- 04-20-2012, 09:56 AM #9
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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
- 04-20-2012, 10:50 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,450
- Rep Power
- 16
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.
- 04-20-2012, 02:05 PM #11
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Similar Threads
-
Comparing dates in java
By Dominic in forum New To JavaReplies: 4Last Post: 11-15-2011, 03:04 PM -
comparing Graphs and Comparing Matrix
By jetnor in forum New To JavaReplies: 0Last Post: 03-27-2011, 01:40 AM -
comparing dates
By palls in forum New To JavaReplies: 3Last Post: 08-13-2010, 01:08 PM -
UTC Dates
By PedroCosta in forum Advanced JavaReplies: 3Last Post: 04-01-2010, 06:08 PM -
Comparing dates
By Java Tip in forum Java TipReplies: 0Last Post: 01-28-2008, 09:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks