我在尝试将字符串转换为日期时收到此错误。无法解释的数据下面是我的代码:-
String str = "hello"
在您的解析 String 中缺少第二个str
。因此,要解析它,您不应该在SimpleDateFormat
pattern
. 同时更正日期和月份格式。看声明df
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");//Remove :ss
要了解模式的详细信息,请阅读此文档。
String date2 = sdformatter.format(date1);// format method return String.
//Should declare with String
String str = "25-Nov-2013 06:00 AM";
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");//Remove :ss
SimpleDateFormat sdformatter = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss a");
Date date1=null;
try {
date1 = df.parse(str);
} catch (ParseException ex) {
ex.printStackTrace();
}
String date2 = sdformatter.format(date1);
System.out.println(date2);
根据 str 格式,您应该编写 SimpleDateFormat,
(25-Nov-2013 06:00 AM ---> dd-MMM-yyyy hh:mm a) and for
(25-Nov-2013 06:00:30 AM-----> dd-MMM-yyyy hh:mm:ss a)
将工作
尝试这个
long newerdate = new Date().parse("25-Nov-2013 06:30 AM");
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("dd-MM-yyyy hh:mm a");
String data = df.format(newerdate);
System.out.println(data);