一个是:2013.05.08 13:30:15一个是:2013 05 14 14:17:25因为都是String,所以我想先转换为Date,这样不对啊?
DateFormat format = new SimpleDateFormat("yyyy MM dd hh:mm:ss");
format.setLenient(false);
String s = "2013.05.08 13:30:15";
Timestamp ts = new Timestamp(format.parse(s).getTime());
System.out.println(ts.toString());会报转为异常错误。
DateFormat format = new SimpleDateFormat("yyyy MM dd hh:mm:ss");
format.setLenient(false);
String s = "2013.05.08 13:30:15";
Timestamp ts = new Timestamp(format.parse(s).getTime());
System.out.println(ts.toString());会报转为异常错误。
format.setLenient(false);
String s = "2013.05.08 13:30:15";
Timestamp ts = new Timestamp(format.parse(s).getTime());
System.out.println(ts.toString());
2013.05.08 13:30:15
yyyy MM dd hh:mm:ss
你看上下格式一样么?String s = "2013.05.08 13:30:15";
DateFormat format = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss");
Date date = null;
try {
date = format.parse(s);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(date.toString());
Exception in thread "main" java.text.ParseException: Unparseable date: "2013.05.08 13:30:15"
at java.text.DateFormat.parse(DateFormat.java:357)
at program.Test.main(Test.java:15)在Java API中,DateFormat的setLenient(false)方法确保严格的日期格式,可以看到代码中,"2013.05.08 13:30:15"和指定的日期格式"yyyy MM dd hh:mm:ss"并不匹配,应当更改其中任意一项。
更改代码如下:
(1)更改指定日期格式,另外表示24小时制使用大写的H
DateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
format.setLenient(false);
String s = "2013.05.08 13:30:15";
Timestamp ts = new Timestamp(format.parse(s).getTime());
System.out.println(ts.toString());
(2)更改指定字符串,12小时制仍可以使用小写的h
DateFormat format = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss");
format.setLenient(false);
String s = "2013.05.08 01:30:15";
Timestamp ts = new Timestamp(format.parse(s).getTime());
System.out.println(ts.toString());