Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8224053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:55:24+00:00 2026-06-07T14:55:24+00:00

I am attempting to sort a collection of Dates in LinkedList myQueue by emptying

  • 0

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;    
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:55:25+00:00Added an answer on June 7, 2026 at 2:55 pm

    You’re prof has provided a custom class called Date which is totally unrelated to java.util.Date. So you simply can’t cast. If you really need to convert between those two different Date types, then you’ll have to write the converters.

    Best would be to remove the imports of java.util.Date from your code and use only the Date type 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 Date class instead of java.util.Date.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to SORT an ArrayList by (Type Date) MyObject.getDate() My format is formatted
I am attempting to sort a Python list of int s and then use
I am attempting to sort an ArrayList of TokenDoubleCounters (my custom object). In TokenDoubleCounter,
I currently have a deployed app (fortworth.herokuapp.com) that I am attempting to sort movies
I'm attempting to do a simple bubble sort code to get familiar with list/string
I have sort of a tricky problem I'm attempting to solve. First of all,
Attempting to use XStream's JavaBeanConverter and running into an issue. Most likely I'm missng
I am attempting sort this array by each stdClass Object's [title] in an Ubercart
I'm attempting to sort a ListView using C#, but whenever I click the sort
So I am pulling a list of links and I am attempting to sort

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.