偏移量为0的无法解释的日期

时间:2015-03-12 10:23:55

标签: android

这是我的代码

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中正确声明了吗?那么为什么还是一个错误?

1 个答案:

答案 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);