在我的代码中,我必须将 String value(Input) 转换为java.sql.Date format
. 我面临的问题是,输入日期格式未定义,它可以是任何格式。例如,输入字符串可能是"Jan 10,2014"
or"01-10-2014"
或"2014/Jan/10"
。所以现在我需要将此输入值转换为java.sql.Date(DD/MMMM/YYYY)
. 有没有办法进行这种转换?
问问题
177 次
2 回答
4
这是不可能的。
您无法区分dd/MM/yyyy
和MM/dd/yyyy
。
您确实需要知道格式,否则您的程序可能不会按照您想要的方式运行。
于 2014-03-25T08:38:04.297 回答
0
尝试List
使用 SimpledateFormat 使用上面提到的所有模式。
像这样的东西:
SimpleDateFormat format1 = new SimpleDateFormat("MMM dd,yyyy");
SimpleDateFormat format2 = new SimpleDateFormat("MM-dd-yyyy");
SimpleDateFormat format3 = new SimpleDateFormat("yyyy/MMM/dd");
// Note: be sure about the format, or else you may end up assigning incorrect values
List<DateFormat> list = new ArrayList<DateFormat>();
list.add(format1);
list.add(format2);
list.add(format3);
for (DateFormat format : list) {
try {
System.out.println(format.parse("Jan 10,2014"));
// Match found. Take action
} catch (ParseException exception) {
// Ignore. Try other pattern
}
}
于 2014-03-25T08:39:45.443 回答