Java SimpleDateFormat returning wrong value in Date object

主宰稳场 提交于 2019-12-14 03:16:49

问题


I'm trying to parse a string to get a Date object, but it's always returning Sun. December 30, 2012 for the date. Does anyone have any ideas on what I'm doing wrong?

I was using the same code using strings in YYYY-MM-dd format and it worked just fine, so I'm not sure why switching to this format is causing issues.

 public static Date getDateObjFromStr(String dateStr)
{
    DateFormat formatter = new SimpleDateFormat("MM/dd/YYYY");
    Date dateObj;
    try {
        dateObj = formatter.parse(dateStr);
        return dateObj;
    } catch(Exception e) {
        return null;
    }
}


回答1:


Case-sensitive

Uppercase Y represents week-based year.

Try using lowercase y instead

DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");


来源:https://stackoverflow.com/questions/19849087/java-simpledateformat-returning-wrong-value-in-date-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!