给出错误月份的简单日期格式

simple date format giving wrong month

我试图将日期从 yyyy-MM-dd 格式转换为 yyyy-MM 格式,当我 运行 下面的代码输入“2012-10-22”时,它给了我一个输出2012-JAN 而不是 2012-OCT。想知道我哪里做错了吗?

  public static String dateFormatter(String presentDate)
     {
     String formattedDate = "";
     SimpleDateFormat tempFormat = new SimpleDateFormat("YYYY-MM-DD");
     SimpleDateFormat finalFormat = new SimpleDateFormat("YYYY-MMM");

     try {
        Date currentFormat = tempFormat.parse(presentDate);
        formattedDate = finalFormat.format(currentFormat);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



    return formattedDate;
}

将第一种格式更改为

SimpleDateFormat tempFormat = new SimpleDateFormat("yyyy-MM-dd");

因为 DD 是一年中的第几天。 22 肯定是一月份

使用这个

SimpleDateFormat tempFormat = new SimpleDateFormat("yyyy-MM-dd");

而不是

SimpleDateFormat tempFormat = new SimpleDateFormat("YYYY-MM-DD");