How can you sort dates?

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What is scope & storage allocation of global and extern variables? Explain with an example

581


What is difference between filereader and bufferedreader?

534


What is unmodifiable list in java?

536


Define Multiprogramming and Multiprocessing in java.

581


Is array dynamic in java?

483






Why is stringbuffer thread safe?

567


Can we catch more than one exception in single catch block?

603


List some important features of java 10 release?

517


What is the difference between break and continue statements?

549


What is javac in java?

562


How do you add spaces in java?

514


What is javac_g?

591


What is a class reference?

555


Why string is a class?

548


What is one third plus one third as a fraction?

489