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;
    }


  }
  }
}

}

Recommended Answers

All 5 Replies

The code you posted tries to parse "Thu Sep 28 20:29:30 JST 2056" with a format "dd/mm/yy", so of course that doesn't work. What exactly are you trying to do with the "dd/mm/yy" format?

I got the problem. My Bad.
But if i have a array which have strings in different format e.g. {"10/7/2013","10.08.2003"}, how do i parse it then?

"with difficulty"! I don't know of a generic "any format" date parser in the Java API (maube someone else does?). If they are in one of a small known set of formats then you could just try each in turn until the parsing works, or maybe there's some simple test you can perform on the input to determine which of the formats it is. What are the limits on the variety of possible input formats?

Ok. There are no limits as such. I want to run this on a file which has around 10000-20000 entries. So that's why I was asking if any siuch generic way exists.

This may be unsolvable... eg is "10/11/12" 10th Nov 2012 (UK), 11th Oct 2012 (US), 12th Nov 2010 (Swe)?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.