我现在拿到一个字符串形式的日期,类似于"10-Nov-2011"这样的,要把他转换成Date类型的,有没有和这对应的SimpleDateFormat?
10-Nov-2011
对应
"dd-MMM-yyyy"
没有
SimpleDateFormat format = new SimpleDateFormat();
Date dd = null;
try {
dd = format.parse("10-Nov-2011");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
console info:
null
java.text.ParseException: Unparseable date: "10-Nov-2011"
at java.text.DateFormat.parse(DateFormat.java:337)
at com.hudepin.test.SimpleDateTest.main(SimpleDateTest.java:17)
[code="java"]
String s = "10-Nov-2011";
DateFormat fmt = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.UK);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(fmt.parse(s)));
[/code]
呵呵,楼上兄弟还要努力啊~~~
我晕死,你是没用过SimpleDateFormat么,
改成如下:
[code="java"]
......
SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
......
[/code]
呵呵,你应该不是做java的吧。我把代码补全。
[code="java"]
String s = "10-Nov-2011";
DateFormat fmt = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.UK);
SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
System.out.println(sdf.format(fmt.parse(s)));
[/code]
如果是需要返回Date,但写一个转换方法
[code="java"]
public Date String2Date(String s) {
// TODO 判断非空
Date date = null;
DateFormat fmt = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.UK);
try {
date = fmt.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
[/code]
这个转换和SimpleDateFormat没有任何关系。
无所谓,解决问题就好,不过你改恶补一下java的知识~~~。