我需要将 GregorianCalendar
转换为 String
并再次将此字符串重新转换为 GregorianCalendar
我用它来转换GregorianCalendar
中的String
DateFormat format = new SimpleDateFormat();
Date date = null;
try {
date = format.parse(dateS);
} catch (ParseException e) {
e.printStackTrace();
}
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
不幸的是,当我调用第二种方法时,我收到此错误
java.text.ParseException: Unparseable date: "11 apr 2014 21:48:02" (at offset 2)
在这一行
date = format.parse(dateS);
最佳答案
您需要使用用于解析日期字符串的模式来实例化您的 SimpleDateFormat
对象。对于您的情况:
DateFormat format = new SimpleDateFormat("d MMM yyyy HH:mm:ss");
可以在 docs for SimpleDateFormat
中找到可接受的模式符号表。
关于java - 字符串 from\to GregorianCalendar 导致 java.text.ParseException : Unparseable date,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23025265/