不使用格式将字符串转换为日期对象
Convert string to date object without using the format
是否可以在不知道字符串值格式的情况下将字符串转换为 java sql/util 日期对象。
您需要了解一些格式化信息。如果你有一些想法,你可以迭代它们。
private static String[] formats = new String[] {/* Your list of possible formats */};
public static Date parse(String date) throws ParseException {
for (String format : formats) {
DateFormat df = new SimpleDateFormat(format);
try {
return df.parse(date);
} catch (ParseException e) {}
}
throw new ParseException(
"This date does not conform to any known format", 0);
}
当然,这只是在同一日期不能满足多种格式的情况下(例如,评论中提到的 01/01/01)
是否可以在不知道字符串值格式的情况下将字符串转换为 java sql/util 日期对象。
您需要了解一些格式化信息。如果你有一些想法,你可以迭代它们。
private static String[] formats = new String[] {/* Your list of possible formats */};
public static Date parse(String date) throws ParseException {
for (String format : formats) {
DateFormat df = new SimpleDateFormat(format);
try {
return df.parse(date);
} catch (ParseException e) {}
}
throw new ParseException(
"This date does not conform to any known format", 0);
}
当然,这只是在同一日期不能满足多种格式的情况下(例如,评论中提到的 01/01/01)