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]

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.