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 meant by Garbage collection?

716


What is the Concept of Encapsulation in OOPS

639


how to run ecllipse with jettyserver for windows environment using batch file

1587


What is methods and methodology?

630


What does the exclamation mark mean in java?

765






What are the 3 types of control structures?

606


What are the advantages of assembly language?

596


What are the different types of java?

647


How to perform quicksort in java?

659


What is a lock or purpose of locks in java?

678


What is nullpointerexception in java?

608


In case of inheritance what is the execution order of constructor and destructor?

730


What is mnemonic code?

658


How many bits is a float?

644


Which command from the jdk compiles a java program?

568