?
Solved

Using Calendar and Date classes to sort info in a List

Posted on 2003-03-26
27
Medium Priority
?
188 Views
Last Modified: 2010-03-31
how can i use Calendar and Date classes to sort out information by date/time in a list?
The data is in the form
MM/DD:HH:MM:

thanks for any assistance
0
Comment
Question by:welsh_boy
  • 11
  • 8
  • 4
  • +2
27 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 8209349
There are many ways. You can make GregorianCalendar or Date objects and use their compareTo (), after (), before () methods.... or simply the getTime () method of Date which returns a long value (milliseconds since Jan 1, 1970) and then compare 2 such values to check which one came first.

Mayank.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8209426
1. Create SimpleDateFormat sdf = new SimpleDateFormat("MM/dd:HH:mm");
2. Create a list of Dates using sdf.parse
3. Sort it
0
 
LVL 35

Expert Comment

by:girionis
ID: 8209455
 Assuming that your List is called "l" then you could do something like this:

Colelctions.sort(l, new Comparator()
                                        {
                                             public int compare(Object o1, Object o2)
                                             {
                                                  long date1 = (((java.util.Date) o1).getTime())l;
                                                  long date2 = (((java.util.Date) o2).getTime())l;
                                                 
                                                  return date1 - date2;
                                            }
                                        });

  Hope it helps.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 35

Expert Comment

by:girionis
ID: 8209460
I also assumed that your list holds objects of type "java.util.Date".
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8209481
Collections.sort(l) if list contains Dates
0
 

Author Comment

by:welsh_boy
ID: 8213135
I have formatted the data in my list to be of the form Date which is a comparable class with a CompareTo method

compareTo
public int compareTo(Date anotherDate)Compares two Dates for ordering.
Parameters:
anotherDate - the Date to be compared.
Returns:
the value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value greater than 0 if this Date is after the Date argument.

How can I implement this to some record that I want to add to the list and compare it with the existing records in the list!!?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8213168
If you're *not* simply comparing Dates, then use a comparator as girionis has shown (unnecessary for Dates) for the class, or field of the class, in question
0
 
LVL 35

Expert Comment

by:girionis
ID: 8213173
 If you already have a list that holds Date objects try CEHJ's suggestion first. If this does not work try mine. If this still does not work then post again with (any) error messages or description of the problem.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8215466
>> How can I implement this to some record that I want to add to the list and compare it with the existing records in the list!!?

result = object1.compareTo ( object2 ) ;

if ( result > 0 )
  // after
else if ( result < 0 )
  // before
else
  // equal

Mayank.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8216913
>>How can I implement this to some record that I want to add to the list

Please give the definition of your 'record' (which of course can only be a class in Java)
0
 

Author Comment

by:welsh_boy
ID: 8223864
   checkDate = new Date(year, month1, day, startHr, startMin);  
    String check = new String (checkDate(day,month,year,startHr,startMin,stopHr,stopMin));

    toRecord.add(adds my record);

Will I need to implement a bubble sort or something like that to compare the date I want to add with each of the dates already in the list using the following code?
if ( result > 0 )
 // after
else if ( result < 0 )
 // before
else
 // equal
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8223890
>>Will I need to implement a bubble sort...

No. There are sorting routines built in to Java.

Your first line of code should not be used as that contructor is deprecated. Use SimpleDateFormat to parse the Date.

Not sure what's happening in the second line, nor your third.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8223898
>>  checkDate = new Date(year, month1, day, startHr, startMin);  
>>   String check = new String (checkDate(day,month,year,startHr,startMin,stopHr,stopMin));

Won't compile.

0
 

Author Comment

by:welsh_boy
ID: 8224218
sorry that code was stupid the
Sting check was just used to print out what I was doing in the command window to see what i was dealing with.


This is the actual code I am working on, (mostly psuedo) to compare the dates to be added to the dates alreading in the list


for (int i = 3; i<toRecord.getItemCount();i++)
prevdate = toRecord.getSelectedIndex(i);  
if ( checkDate.compareTo(prevDate) > 0 )
    move to next list entry
    compareTo
else if (checkDate.compareTo(prevDate)  < 0 )
    insert where prevdate was
    move prevdate down the list
    move all items down the list
else
    if equal
    insert where prevdate was
    move prevdate down the list
    move all items down the list

Thanks for the help so far
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8224230
Well, that pseudocode seems to be sorting. As i said, Java will do this for you.
0
 

Author Comment

by:welsh_boy
ID: 8230056
so you cant see anything I have left out ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8230186
I think you're missing the point. The only code you need, if it contains Dates, is the following, which i've already posted:

>>Collections.sort(l) if list contains Dates

If it's some other class (what you call a 'record') then you need a Comparator. You *don't* need to implement sorting yourself, as your above pseudo-code implies you're doing.
0
 

Author Comment

by:welsh_boy
ID: 8230362
>>Colelctions.sort(l, new Comparator()

My list doesnt only contain dates, I can create dates for each entry I want to add to the list, then I add the date along with other information to the list.

Is this a problem?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8230377
Your list should contain a wrapper object in that case. It should wrap a Date and any other information you want to use. You would then sort it with a Comparator.
0
 

Author Comment

by:welsh_boy
ID: 8230447
I am not familiar with wrappers
what are their purpose?

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8230452
It's just a class that contains other classes, such as Dates etc.
0
 

Author Comment

by:welsh_boy
ID: 8230606
ok clear somethings up

I can access the following data from a listing

    int day = progData.getDay();
    int month = progData.getMonth();
    int startHr = progData.getStartHour();  
    int startMin = progData.getStartMin();
    int stopHr = progData.getStopHour();
    int stopMin = progData.getStopMin();    
    String channel = progData.getChannel();  
    String title = progData.getTitle();  

Then I can creat a Date using
    Date checkDate = new Date(year, month, day, startHr, startMin);  

And this is what I want to compare to a record in my list
java.awt.List toRecord;

And if the date is before the date of the record in the list, add it before that record and move all the other records down.
If it is after, check next record until it is before and place there, then move all the other records down.
If its the same just place after.

Hope this has cleared it up a bit!!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8230673
You keep talking about 'records'. Is a record a Date in this case?
0
 

Author Comment

by:welsh_boy
ID: 8233551
by record I mean an item in my list, these consist of
Date, Time, Channel and Title

i use
    toRecord.add(" " +propDay +"        "
    +propMonth +"           " +startTime +"              " +channel + "   " +title);

to add items to my list
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 255 total points
ID: 8233566
Then you should have something like:

class Record {
  Date date;
  //..
  String title;
  // get/set methods required
}

Then you can add a 'record' to the list of records.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8233663
Welsh_boy,

I think it'll be better if you also post your class definition. That'll be very helpful in clearing the confusions which we're all having. And try putting your class definition in the same form as CEHJ has provided if its not so..

Pls keep this page updated.

Mayank.
0
 

Expert Comment

by:CleanupPing
ID: 9058481
welsh_boy:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question