简单日期格式化程序中的月份始终返回JANUARY [英] Month in simple date formatter always return JANUARY

查看:113
本文介绍了简单日期格式化程序中的月份始终返回JANUARY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用简单的日期格式将字符串转换为日期,但日期的月份总是返回JANUARY这里是我的代码:

I convert string to date by using simple date format but the month of the date is always return JANUARY here's my code:

  public static void main(String args[]){

    String date = "2017-12-29";
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
    SimpleDateFormat dateFormatter = new SimpleDateFormat("MMMM dd, YYYY");
    try {
        Date startEntryDate = formatter.parse(date);
        System.out.println(dateFormatter.format(startEntryDate));

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


推荐答案

As已经提到你必须使用 MM 而不是 mm ,但是?

为什么不使用 java.time library:

As mentioned already you have to use MM instead of mm, but?
Why not using java.time library :

String month = LocalDate.parse("2017-12-29").getMonth().name();

这篇关于简单日期格式化程序中的月份始终返回JANUARY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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