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

What is assembly condition codes?

564


When should you use arraylist and when should you use linkedlist?

500


What is static synchronization?

581


Can we override private methods?

549


Is empty in java?

586






What is bool mean?

575


How many types of design patterns are there?

536


What is an interoperable application in java ?

590


What method is used to specify a container's layout in java programming?

550


What is data type modifier?

535


What is the final method?

606


How do you define a singleton class?

519


What are inner classes or non static nested classes in java?

711


What is the memory leak in java?

530


What is the set interface in java programming?

633