繁体   English   中英

日期和simpledateformat,输出错误

[英]Date and simpledateformat, wrong output

因此,如果我打印2013年1月18日,我得到2014年6月1日,或者我打印2013年8月18日,我得到2014年6月8日,我应该怎么做才能获得正确的英文格式日期输出?

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String s = reader.readLine();
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        Date givenDate = formatter.parse(s);
        SimpleDateFormat formatterprint = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH);
        System.out.println(formatterprint.format(givenDate).toUpperCase());

用于解析日期的模式是dd,MM,yyyy ,这意味着天后跟一个逗号,然后是月,然后是逗号,然后是年。

您输入08/18/2013 08是有效的一天,但18不是有效的月份。 并且您应该输入逗号,而不是斜杠。

由于您输入18/2013作为月份,因此将其解释为2013年的第18个月,与2014年的第6个月相对应(由于年份,只有12个月):6月。

暂无
暂无

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

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