简体   繁体   English

Java 1.7中的SimpleDateFormat中的行为更改?

[英]Behavior change in SimpleDateFormat in java 1.7?

Consider following snippet 考虑以下片段

public class Test1 {
    public static void main(String[] args) throws ParseException, java.text.ParseException {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        f.setLenient(false);
        String dateStr = "2008-01-23 00:00:00.0";
        Date date=f.parse(dateStr);
        System.out.println(date);

    }
}

this works fine in java 1.6 and gives output Wed Jan 23 00:00:00 IST 2008 在Java 1.6中可以正常工作,并提供输出Wed Jan 23 00:00:00 IST 2008

In case of java 1.7 it gives Exception 如果是Java 1.7,则会产生异常

Exception in thread "main" java.text.ParseException: Unparseable date: "2008-01-23 00:00:00.0"
    at java.text.DateFormat.parse(DateFormat.java:357)

I think JAVA 1.7 behavior is correct because 00:00:00.0 doesn't come under hh:mm:ss format (1-12 hr format) 我认为JAVA 1.7行为是正确的,因为00:00:00.0不在hh:mm:ss格式(1-12 hr格式)下

Is my assumption is correct that 1.6 has bug and is corrected in 1.7? 我的假设是正确的,即1.6存在错误并在1.7中得到了纠正?

Yes, this behaviour has changed with Java 7. The bugfix documentation can be found on Oracle website . 是的,此行为在Java 7中已更改。可以在Oracle网站上找到错误修复文档。 The bug-id is JDK-4396385. 错误ID是JDK-4396385。

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

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