简体   繁体   English

SimpleDateFormat解析返回年末日期

[英]SimpleDateFormat parse return End of Year date

I have this simple example: 我有一个简单的例子:

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/YY");
        System.out.println( sdf.format(new Date()));
        try {
            System.out.println(  sdf.parse("10/18/14")  );
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

In the console, I'm getting 10/18/14 Sun Dec 29 00:00:00 EST 2013 在控制台中,我将获得10/18/14 Sun Dec 29 00:00:00 EST 2013

Any explanation why sdf.parse return dec 29th 2013 in stead of Oct 18th 2014 为何sdf.parse返回2013年12月29日而不是2014年10月18日的任何解释

Your format String is YY and should be yy 您的格式StringYY ,应为yy

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");

Per the SimpleDateFormat Javadoc (which says in part), 根据SimpleDateFormat Javadoc(部分说明),

 Letter Date or Time Component Presentation Examples G Era designator Text AD y Year Year 1996; 96 Y Week year Year 2009; 09 

Currently you're getting the Week year . 目前,您将获得“ 周年”

When I make the above change I get (the expected) 当我进行上述更改时,我得到了(预期)

10/18/14
Sat Oct 18 00:00:00 EDT 2014

这里发生了同样的事情: SimpleDateFormat似乎给出了不正确的结果尝试先看看是否有人遇到相同的问题,这样可以更轻松,更快速地找到问题的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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