I am attempting to sort a collection of Dates in LinkedList myQueue by emptying it into ArrayList myArrayList, sorting it via Comparator, then iterating over the linked list to place each Date back into the myQueue.
Everything works fine until I send the Date Objects into My Comparator for some reasin Casting the dates I created into Dates in the Comparator does not work.
Here is the Format the Dates are in:
Thu Jul 30 00:00:00 JST 1908
Here is the Error:
Exception in thread "main" java.lang.ClassCastException: java.util.Date cannot be cast to Date
at DateComparator.compare(DateComparator.java:14)
at java.util.TimSort.countRunAndMakeAscending(Unknown Source)
at java.util.TimSort.sort(Unknown Source)
at java.util.TimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at QueueTest.main(QueueTest.java:76)
This is Where I place all Objects in myQueue into myArrayList:
if (!myQueue.isEmpty()) {
//Collections.sort(myQueue, new DateComparator());
Object nextDate;
ArrayList myArrayList = new ArrayList();
while(!myQueue.isEmpty()){
nextDate = myQueue.removeFirst();
myArrayList.add(nextDate);
}
Collections.sort(myArrayList, new DateComparator());
Iterator it=myArrayList.iterator();
while(it.hasNext()){
myQueue.addLast(it.next());
}
And this is where the issue is in My Comparator:
Date d1 = (Date) a;
Date d2 = (Date) b;
I just started going to school for Computer Science in Okinawa, got nobody to really talk to about this asides from my dog so any help is appreciated.
//EDIT:
This is the Date() SuperClass that My professor requires we use in order to create out Date Objects:
public class Date {
private static int numberDates=0;
public static final int VALID_START_YEAR=1584;
private int year;
private int month;
private int day;
public Date() {
month = 1;
day = 1;
year = 1970;
numberDates++;
}
public Date(int newMonth, int newDay, int newYear) {
month = newMonth;
day = newDay;
year = newYear;
numberDates++;
}
public Date( Date other) {
month = other.month;
day = other.day;
year = other.year;
numberDates++;
}
public Date( String date) {
java.util.StringTokenizer st = new java.util.StringTokenizer(date,"/");
month = Integer.parseInt(st.nextToken()); //extract next token
day = Integer.parseInt(st.nextToken());
year = Integer.parseInt(st.nextToken());
numberDates++;
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public String toString() {
return(month + "/" + day + "/" + year);
}
public static int getNumberDates() {
return numberDates;
}
public void increment() {
day++; //stub for the actual method to be written later...
}
public static void clearNumberDates() {
numberDates = 0;
}
public static boolean isValidDay(int month, int day, int year) {
return true;
}
public boolean equals( Object other ) {
if ((other instanceof Date) &&
((((Date)other).year==year) &&
(((Date)other).month==month) &&
(((Date)other).day==day)))
return true;
else
return false;
}
public int hashCode() {
return (year + month + day) % 101;
}
public int compareTo( Date other ) {
if (equals(other))
return 0;
else if (year < other.year)
return -1;
else if (year > other.year)
return 1;
else if (month < other.month)
return -1;
else if (month > other.month)
return 1;
else if (day < other.day)
return -1;
else if (day > other.day)
return 1;
else
return 0;
}
You’re prof has provided a custom class called
Datewhich is totally unrelated tojava.util.Date. So you simply can’t cast. If you really need to convert between those two differentDatetypes, then you’ll have to write the converters.Best would be to remove the imports of
java.util.Datefrom your code and use only theDatetype that has been provided for this assignment.Sorting those dates is possible the way you started. You’ll have to reimplement your DateComparator to use the custom
Dateclass instead ofjava.util.Date.