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.*;

  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]); 
    try {        
    }catch(Exception e){
  }catch(Exception e){

//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++){



//Printing the array in converted format
System.out.println("The array in converted form is: ");
for (m=0;m<target1.length;m++){



//Printing the sorted date array
System.out.println("The sorted array is: ");
for (m=0;m<target2.length;m++){

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



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, learning, and sharing knowledge.