这是我的代码
private Time today;
private String currentDateTime;
private static String dateTimeFormatting = "%y-%m-%d %H:%M:%S";
today = new Time(Time.getCurrentTimezone());
today.setToNow();
currentDateTime = today.format(dateTimeFormatting);
SimpleDateFormat sdf = new SimpleDateFormat(dateTimeFormatting);
currentDateTimeFormatted = sdf.parse(currentDateTime);
它将返回此错误:
Error: java.text.ParseException: Unparseable date: "15-03-12 18:21:06" (at offset 0)
从我看到的,我的格式在dateTimeFormatting中正确声明了吗?那么为什么还是一个错误?
答案 0 :(得分:2)
问题是SimpleDateFormat
接受的语法与Time
的语法不同,在处理时间和日期以提供适当的Locale
引用时也是一种很好的做法。
private Time today;
private String currentDateTime;
private static String dateTimeFormatting = "%y-%m-%d %H:%M:%S";
private static String simpleDateFormatting = "yy-MM-DD HH:mm:SS"
today = new Time(Time.getCurrentTimezone());
today.setToNow();
currentDateTime = today.format(dateTimeFormatting);
SimpleDateFormat sdf = new SimpleDateFormat(simpleDateFormatting, Locale.getDefault());
currentDateTimeFormatted = sdf.parse(currentDateTime);