--Parsing date and time strings with Java's SimpleDateFormat class to stringify Date objects
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*Sample to parse format with SimpleDateFormat.
*/
public class Sample {
public static void main(String[] args) {
String[] sourceList = {
"1-2-3 4:5:6",
"99-2-3 4:5:6",
"999-2-3 4:5:6",
"2000-12-31 12:34:56",
"9999-12-31 12:34:56.0001",
"9999-12-31 12:34:56.000001",
"10000-12-31 12:34:56",
"XXXX-YY-ZZ AA:BB:CC",
"9999X-12X-31X 12X:34X:56X",
"X9999-X12-X31 X12:X34:X56",
};
parseAndFormat(sourceList, "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss");
parseAndFormat(sourceList, "y-M-d H:m:s", "yyyy-MM-dd HH:mm:ss");
parseAndFormat(sourceList, "yyyy-MM-dd HH:mm:ss", "y-M-d H:m:s");
}
/**
*Parse and format.
* @param sourceList Array of strings to parse
* @param parsePattern Pattern string that parses the datetime string
* @param formatPattern Date A pattern string that formats an object
*/
private static void parseAndFormat(String[] sourceList, String parsePattern, String formatPattern) {
//Prepare the parser
SimpleDateFormat parser = new SimpleDateFormat(parsePattern);
//Prepare the formatter
SimpleDateFormat formatter = new SimpleDateFormat(formatPattern);
System.out.println("****************************************");
System.out.println("Parse: " + parsePattern);
System.out.println("Format: " + formatPattern);
for (String source : sourceList) {
try {
//Convert date and time string to Date object
Date date = parser.parse(source);
//Convert Date object to date and time string
String text = formatter.format(date);
System.out.println(source + " -> " + text);
} catch (ParseException e) {
System.out.println(source + " -> " + e.toString());
}
}
System.out.println();
}
}
Example executed on Java 8 (AdoptOpenJDK 1.8.0_272-b10) + macOS Catalina.
****************************************
Parse: yyyy-MM-dd HH:mm:ss
Format: yyyy-MM-dd HH:mm:ss
1-2-3 4:5:6 -> 0001-02-03 04:05:06
99-2-3 4:5:6 -> 0099-02-03 04:05:06
999-2-3 4:5:6 -> 0999-02-03 04:05:06
2000-12-31 12:34:56 -> 2000-12-31 12:34:56
9999-12-31 12:34:56.0001 -> 9999-12-31 12:34:56
9999-12-31 12:34:56.000001 -> 9999-12-31 12:34:56
10000-12-31 12:34:56 -> 10000-12-31 12:34:56
XXXX-YY-ZZ AA:BB:CC -> java.text.ParseException: Unparseable date: "XXXX-YY-ZZ AA:BB:CC"
9999X-12X-31X 12X:34X:56X -> java.text.ParseException: Unparseable date: "9999X-12X-31X 12X:34X:56X"
X9999-X12-X31 X12:X34:X56 -> java.text.ParseException: Unparseable date: "X9999-X12-X31 X12:X34:X56"
****************************************
Parse: y-M-d H:m:s
Format: yyyy-MM-dd HH:mm:ss
1-2-3 4:5:6 -> 0001-02-03 04:05:06
99-2-3 4:5:6 -> 1999-02-03 04:05:06
999-2-3 4:5:6 -> 0999-02-03 04:05:06
2000-12-31 12:34:56 -> 2000-12-31 12:34:56
9999-12-31 12:34:56.0001 -> 9999-12-31 12:34:56
9999-12-31 12:34:56.000001 -> 9999-12-31 12:34:56
10000-12-31 12:34:56 -> 10000-12-31 12:34:56
XXXX-YY-ZZ AA:BB:CC -> java.text.ParseException: Unparseable date: "XXXX-YY-ZZ AA:BB:CC"
9999X-12X-31X 12X:34X:56X -> java.text.ParseException: Unparseable date: "9999X-12X-31X 12X:34X:56X"
X9999-X12-X31 X12:X34:X56 -> java.text.ParseException: Unparseable date: "X9999-X12-X31 X12:X34:X56"
****************************************
Parse: yyyy-MM-dd HH:mm:ss
Format: y-M-d H:m:s
1-2-3 4:5:6 -> 1-2-3 4:5:6
99-2-3 4:5:6 -> 99-2-3 4:5:6
999-2-3 4:5:6 -> 999-2-3 4:5:6
2000-12-31 12:34:56 -> 2000-12-31 12:34:56
9999-12-31 12:34:56.0001 -> 9999-12-31 12:34:56
9999-12-31 12:34:56.000001 -> 9999-12-31 12:34:56
10000-12-31 12:34:56 -> 10000-12-31 12:34:56
XXXX-YY-ZZ AA:BB:CC -> java.text.ParseException: Unparseable date: "XXXX-YY-ZZ AA:BB:CC"
9999X-12X-31X 12X:34X:56X -> java.text.ParseException: Unparseable date: "9999X-12X-31X 12X:34X:56X"
X9999-X12-X31 X12:X34:X56 -> java.text.ParseException: Unparseable date: "X9999-X12-X31 X12:X34:X56"
Recommended Posts