Arbitrary date formats

Hello all,

I've been going through (all morning to be exact) posts, documentation and tutorials but have yet to find that nugget of wisdom so that I can implement a sticking problem I have...

I am using SimpleDateFormat to format a date (wow... what a leap there) I am using a SimpleDateFormat for my input date and my output date:

SimpleDateFormat inFormat =new SimpleDateFormat("yyyy-M-d h:m:s.S");

SimpleDateFormat outFormat =new SimpleDateFormat("M/d/yyyy");

Date d = inFormat.parse(str_Some_Date_String);

System.out.println ("Formatted date: " + outFormat.format(d));

Okay.... pretty simple... except if my input date string is not of the format I specified!

Is there a technique available to accept an arbitrary date string format? I've looked at using DateFormat.parse(str) but no matter what string I feed it... I get ParseExceptions saying that the format I gave it... wasn't a valid format.

I would *like* to be able to be able to format any date string... no matter the format (within reason).

Thanks much for anyone kind enough to respond!

./dave

[1284 byte] By [Minusone] at [2007-9-27 14:52:00]
# 1

You are basically asking if Java has some sort of artificial intellegence. I propose that no matter how 'smart' you or Java is you can't tell me what date 5/4/2002 represents if you don't know whether I am in Europe or the US. Moreover if I decide to put my date in like so, 01/02/02 is that Jan 2, 2002; Feb 1, 2000; or Feb 2, 2001? You are going to have to enforce so rules. You could allow a few date formats as long as they are different enough that you can tell them apart.

dubwai at 2007-7-5 22:52:03 > top of Java-index,Core,Core APIs...
# 2
Actually this isn't really AI. I suppose that there could be a class out there that compares the given date to a number of known formats but it would have to make 'assumptions' sbout the date.
dubwai at 2007-7-5 22:52:03 > top of Java-index,Core,Core APIs...
# 3

> Actually this isn't really AI. I suppose that there

> could be a class out there that compares the given

> date to a number of known formats but it would have to

> make 'assumptions' sbout the date.

which is exactly what I wanted to avoid... was hoping there was some PFM out there :)

thanks anyway

Minusone at 2007-7-5 22:52:03 > top of Java-index,Core,Core APIs...
# 4

I know this is a month old, but hopefully this can help someone else, I'm in the same boat and came up with a somewhat reasonable solution.

I have to convert an unknown date/time format to a GMT Date/Time string in a standard format. Here's what I came up with:

boolean processing = true;

//Any Date/Time String from any Locale

String fieldValue = "2002-11-5 07:48:43 AM EST";

System.out.println("Pre Conversion = " + fieldValue);

Locale[] locale = Locale.getAvailableLocales();

Calendar cal = null;

DateFormat format = null;

Date formatedDate = null;

int x = 0;

int y = 0;

int itr = 0;

//Iterate thru the avaliable Locale Formats

for (itr = 0; itr < locale.length; itr++)

{

if (!processing)

break;

ResourceBundle resource =

ResourceBundle.getBundle(

"java.text.resources.LocaleElements", locale[itr]);

String[] pattern = (String[])resource.getObject("DateTimePatterns");

//Try each format in combonation until we find one.

for (x = 0; x < pattern.length; x++)

{

if (!processing)

break;

for (y = 0; y < pattern.length; y++)

{

if (!processing)

break;

//Set the patten

format = new SimpleDateFormat(pattern[x] + " " +

pattern[y]);

//Try to parse and format the fieldValue

try

{

formatedDate = format.parse(fieldValue);

cal = Calendar.getInstance();

cal.setTime(formatedDate);

fieldName = cal.getTime().toString();

/*

//Convert it to GMT Time

GMTConversion gmt = new GMTConversion();

fieldValue = gmt.convertToGMT(cal);

*/

//Formatting was successful

processing = false;

/*

}

catch (ConversionException ce)

{

continue;

*/

}

catch (RuntimeException re)

{

continue;

}

catch (Error err)

{

continue;

}

catch (Exception e)

{

continue;

}

}

}

}

System.out.println("Post Conversion = " + fieldValue);

You will be left with your original date/time string (assuming its format cannot be determined from all avaliable locales) or a formated

Calendar, Date, String or long, This code outputs it as a string for demonstration purposes.

skitzo at 2007-7-5 22:52:03 > top of Java-index,Core,Core APIs...