Java日期格式转换 - 错误月份 [英] Java date format conversion - getting wrong month

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

问题描述

 我在Java中转换日期时出现问题, String dateStr =2011-12-15; 
String fromFormat =yyyy-mm-dd;
String toFormat =dd MMMM yyyy;

try {
DateFormat fromFormatter = new SimpleDateFormat(fromFormat);
Date Date =(Date)fromFormatter.parse(dateStr);

DateFormat toformatter = new SimpleDateFormat(toFormat);
String result = toformatter.format(date);

} catch(ParseException e){
e.printStackTrace();
}

输入日期为2011-12-15,我期望结果为 2011年12月15日,但我认为2011年1月15日



我哪里错了?

解决方案

您的 fromFormat 使用应该使用数月的分钟。 p>

  String fromFormat =yyyy-MM-dd; 


I have a problem in converting the date in java, don't know where i am going wrong...

    String dateStr = "2011-12-15";
    String fromFormat = "yyyy-mm-dd";
    String toFormat = "dd MMMM yyyy";

    try {
        DateFormat fromFormatter = new SimpleDateFormat(fromFormat);
        Date date = (Date) fromFormatter.parse(dateStr);

        DateFormat toformatter = new SimpleDateFormat(toFormat);
        String result = toformatter.format(date);

    } catch (ParseException e) {
        e.printStackTrace();
    }

Input date is 2011-12-15 and I am expecting the result as "15 December 2011", but I get it as "15 January 2011"

where am I going wrong?

解决方案

Your fromFormat uses minutes where it should use months.

String fromFormat = "yyyy-MM-dd";

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

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