In the code below, I am trying to convert the target[] strings to a given format in target1[] and then sorting them using bubble sort and store it in target2[] which is a date object array.. However, when the desirable format is "EEE MMM dd kk:mm:ss z yyyy", it is working. When the desirable format is "dd-mmm-yy", it is not working.. Can you please elaborate on what all patters of strings can be coverted to date objects and the problem in the below code.
import java.text.*;
import java.util.*;
import java.io.*;
public static void main(String arg[]){
int l,m;
String s;
String target[] = {"Thu Sep 28 20:29:30 JST 2056","Thu Sep 24 20:29:30 JST 2000", "Thu Jan 24 20:29:30 JST 2015", "Thu Sep 28 20:09:30 JST 2000"};
String[] target1= new String[target.length];
Date[] target2= new Date[target.length];
// target is the original array of date strings
// target1 is the array of date strings in the new format defined above
//target2 is the date array corresponding to strings of target1
DateFormat df = new SimpleDateFormat("dd/mm/yy");// Defining the format of the new Date
//EEE MMM dd kk:mm:ss z yyyy
// Parsing the date strings into date objects
for (l=0; l<target.length; l++){
try {
Date result = (Date) df.parse(target[l]);
target2[l]=result;
try {
target1[l]=result.toString();
}catch(Exception e){
System.out.println("kya");
}
}catch(Exception e){
System.out.println("yup");
}
}
//Sorting the Date array from lowest to highest
bubble_srt(target2, target2.length);
//Printing the original array
System.out.println("The original array was: ");
for (m=0;m<target.length;m++){
System.out.println(target[m]);
}
//Printing the array in converted format
System.out.println();
System.out.println("The array in converted form is: ");
for (m=0;m<target1.length;m++){
System.out.println(target1[m]);
}
//Printing the sorted date array
System.out.println();
System.out.println("The sorted array is: ");
for (m=0;m<target2.length;m++){
try{
s=target2[m].toString();
System.out.println(s);
}catch(Exception e){}
}
}
// Bubble sorting algorithm
public static void bubble_srt(Date a[], int n){
int i, j;
Date t;
for(i = 0; i < n; i++){
for(j = 1; j < (n-i); j++){
int r= a[j-1].compareTo(a[j]);
if (r==1){
t = a[j-1];
a[j-1]=a[j];
a[j]=t;
}
}
}
}
}