How can you sort dates?

Answer Posted / nitesh

Right now two ways come to my mind.

First:
Create a TreeSet, add all the dates to it. They get
sorted by default since java.util.Date implements
comparable. Here is an example -->
Set<Date> sortedDates = new TreeSet<Date>();
sortedDates.add(new Date());
Calendar cal = Calendar.getInstance();
cal.set(2009, 2, 12);
sortedDates.add(cal.getTime());

Second:
We can use the compare method inside Date class
directly and write our own sorting implementation.

Is This Answer Correct ?    11 Yes 5 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How many types of memory areas are allocated by jvm?

564


What is core java called?

526


What is flush () in java?

558


Does .length start 0 java?

569


What is super constructor?

551






What is the multi-catch block in java?

537


What is the difference between the prefix and postfix forms of the ++ operator?

556


Explain scope or life time of local variables in java?

560


What is ‘has a’’ relationship in java?

726


What is the use of isempty in java?

539


How do you clear an arraylist in java?

500


What is the super void?

461


What is jpa specification?

539


For class CFoo { }; what default methods will the compiler generate for you>?

621


Difference between arraylist and vector.

594