简单的日期格式给出错误的月份 [英] simple date format giving wrong month

查看:78
本文介绍了简单的日期格式给出错误的月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将日期从yyyy-MM-dd格式转换为yyyy-MM格式,当我使用输入"2012-10-22"运行以下代码时,它给了我2012-JAN的输出,而不是2012年10月.对我在哪里做错了任何想法吗?

I was trying to convert date from yyyy-MM-dd format to yyyy-MM format, when I run the below code with the input "2012-10-22" it is giving me an output of 2012-JAN instead of 2012-OCT. any thoughts on where I am doing wrong?

  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肯定是在一月

as DD is the day in the year. 22 is definitely in January

这篇关于简单的日期格式给出错误的月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆