简体   繁体   中英

Java SimpleDateFormat returning wrong value in Date object

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;
    }
}

代表日期的字符串

SimpleDateFormat对象返回的Date对象

Case-sensitive

Uppercase Y represents week-based year .

Try using lowercase y instead

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM