Ich versuche, ein String-Array von Daten mit SimpleDateFormat zu formatieren. Der erste Eintrag wird korrekt formatiert, aber der zweite Eintrag ist nicht übertragbar, auch wenn sie identisch sind. Ich kann nicht herausfinden, was ich hier falsch mache, irgendwelche Tipps? HierVerwenden von SimpleDateFormat zum Formatieren eines Zeichenfolgenarrays
String dates = "";
String test = "MM/dd/yyyy";
String end = "MMMMMMMMM d, yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(test);
//Match the pattern of 1 or more digits, a backslash, one or more digits,
//a backslash, and then four digits
Pattern pattern = Pattern.compile("\\d+/\\d+/\\d\\d\\d\\d");
Matcher matcher = pattern.matcher(input);
//While there are matches, concatenate the match to the string dates,
//with a semicolon separating each match
while (matcher.find())
{
String a = matcher.group(0).toString();
dates += ";" + a;
}
//Split the string on semicolons
String[] inputString = dates.split(";");
for (int i=1; i<inputString.length; i++)
{
System.out.println(inputString[i]);
}
//Change all dates of format MM/dd/yyyy into Month date, year and
//force them into uppercase
for (int i=1; i<inputString.length; i++)
{
String old = inputString[i];
Date a = sdf.parse(old);
sdf.applyPattern(end);
String notOld = sdf.format(a);
inputString[i] = notOld;
inputString[i] = inputString[i].toUpperCase();
System.out.println(inputString[i]);
}
ist die Ausgabe:
12/6/1852
12/6/1954
DECEMBER 6, 1852
Exception in thread "main" java.text.ParseException: Unparseable date: "12/6/1954"
at java.text.DateFormat.parse(Unknown Source)
at Info.fixer(Info.java:132)
at Info.main(Info.java:35)
'was ist die variable Eingabe im Beispiel halten? –
Für dieses Snippet die Textdatei einfach: "12/6/1852 12/6/1954" auf zwei Zeilen. Die Zeichenfolge enthält viel mehr, da es sich um eine lange Textdatei handelt, in die Daten extrahiert werden. Diese werden jedoch ganz oben in der Datei getestet, sodass sie nie über sie hinausgeht. –
Wenn Sie 'applyPattern (end)' aufrufen, ändern Sie 'sdf' in das Format' MMMMMMMM d, yyyy'. Bei der nächsten Iteration versuchen Sie, '12/6/1954' mit diesem Format zu parsen, und Hoppla! Das funktioniert nicht. – Andreas