Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


How can you sort dates?

Answers were Sorted based on User's Feedback



How can you sort dates?..

Answer / 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

How can you sort dates?..

Answer / ravinder

import java.util.ArrayList;
import java.util.StringTokenizer;

public class DateSort {
public DateSort() {}

void dateSort() {
ArrayList dateData = new ArrayList();
String months[] = {"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};

dateData.add(0,"29-AUG-2008");
dateData.add(1,"13-FEB-2007");
dateData.add(2,"29-JAN-2008");

System.out.print("\nSorted Dates =\t");

for(int i=0;i<dateData.size();i++){
for(int j=0;j<(dateData.size()-1-i);j++){

StringTokenizer date1=new StringTokenizer((String)dateData.get(j),"-");
StringTokenizer date2=new StringTokenizer((String)dateData.get(j+1),"-");
String date01[] = new String[3];
String date02[] = new String[3];
int k=0;
ArrayList temp=new ArrayList();

while (date1.hasMoreTokens()) {
date01[k]=date1.nextToken();
date02[k]=date2.nextToken();
k++;
}
if(Integer.parseInt(date02[2]) < Integer.parseInt(date01[2])){
int t=0;
temp.add(t,dateData.get(j));
dateData.add(j,dateData.get(j+1));
dateData.add(j+1,temp.get(t));
temp.remove(t);
}
}
}
System.out.println(dateData.size());
for(int i=0;i<dateData.size();i++)
System.out.println(dateData.get(i));


}

public static void main(String args[]) {
DateSort Dates = new DateSort();

Dates.dateSort();
}
}

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More Core Java Interview Questions

Explain the selection sort algorithm?

0 Answers   Hexaware,


What is token in java?

0 Answers  


What is an array in java?

0 Answers  


what is overloading in java?

13 Answers   Ascent, DNS, Infosys, SDA, VelTech,


Difference between stack and queue?

0 Answers   Flextronics,


Differentiate between a constructor and a method? Can we mark constructors final?

0 Answers  


What is lexicographically smallest string?

0 Answers  


What problems will occur when you don?t use layout managers?

1 Answers  


Explain class A{} class B{} class C{} all the three classes are saved in a single file name, what the name should i provide to the file ,in what file name should i run the program? Ple Explain

9 Answers   DNS, Infosys, TCS,


What does java edition mean?

0 Answers  


What is the superclass of exception?

3 Answers  


what is the final keyword denotes in java?

0 Answers   IBS,


Categories