JDK 6 中的陷阱

发布于 2024-08-08 06:15:40 字数 891 浏览 3 评论 0原文

JDK 6 中是否存在早期版本中不存在的问题?我有兴趣发现 Timestamp.valueOf() 工作方式中的一些令人惊讶的变化,例如以下变化。

Timestamp.valueOf(),当提供包含单个数字的日期或月份的时间戳时。例如。 2009-9-20、2009-9-3、2009-12-4 等在 JDK 6 中的行为有所不同 - 它会抛出 IllegalArgumentException ,指出时间戳格式不正确。而 JDK 5(及更早版本)可以很好地提供正确的值,并在这些单位数字前面加上“0”前缀。

JDK 6 只是更加严格,因为该方法确实希望它的参数是 JDBC 时间戳转义格式的字符串。然而,这个 BREAKS 代码是用 JDK 5 编写的。

代码如下:

String s = "2009-9-1 00:00:00";
Timestamp t = Timestamp.valueOf(s);

然而,JDK 6 可以将小时、分钟、秒设为单位数字。我通过查看 JDK 6 中 Timestamp 类的源代码找出了问题所在。我发现了一个数组 intDate[] ,它被初始化为 {4,2,2} ,并且日期中每个项目的长度都与此进行检查大批。

为什么时间部分即使只有个位数也能正常工作?因为根据等效数组 intTime[] 检查长度的代码在源代码中被注释掉了。

JDK 5 中的 Timestamp 类没有任何这些检查,并且可以很好地处理此类输入。

我在官方网站上没有发现任何地方提到过这种奇怪的现象。不过我发现另一个人也有同样的问题。这个问题很容易解决,我有兴趣找出 JDK 6 中发生的其他此类奇怪的变化。

Are there any gotchas in JDK 6 which did not exist in earlier versions? I am interested in finding out some surprising changes like the following one in the way Timestamp.valueOf() works.

Timestamp.valueOf(), when provided with a timestamp which contains a date or a month with a single digit. eg. 2009-9-20, 2009-9-3, 2009-12-4 etc., behaves differently in JDK 6 - it throws an IllegalArgumentException saying that the timestamp is not properly formatted. Whereas JDK 5 (and earlier versions) works just fine providing the proper values with '0' prefixed to those single digit numbers.

JDK 6 is just being more strict because the method does expect it's argument to be a String in JDBC timestamp escape format. However, this BREAKS code written in JDK 5.

Code like:

String s = "2009-9-1 00:00:00";
Timestamp t = Timestamp.valueOf(s);

However JDK 6 is fine with hours, minutes, seconds being single digits. I have figured out what's wrong by looking at the source code of Timestamp class in JDK 6. I found an array intDate[] which is initialized to {4,2,2} and the length of each item in the date is checked against this array.

Now why did the time part work fine even when there are single digits in them? Because the code that checks the length against an equivalent array intTime[] is commented out in source.

The Timestamp class in JDK 5 did not have any of these checks and work just fine with such inputs.

I do not find such oddities mentioned anywhere in the official site. Though I found one other person having the same problem. This problem is easily fixable and I am interested in finding out other such odd changes that had happened in JDK 6.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小傻瓜 2024-08-15 06:15:40

正式地,

编辑

另外你可以看看Sun的bug数据库。

这个 链接 显示 Java 中类型 bug 的项目,状态已接受,关键字“1.6 1.5”

我检查了其中的一些,它看起来像您所需要的。

Officially, this

EDIT

Additionally you can take a look at Sun's bug database.

This link shows items in Java of type bug with status accepted and the keywords "1.6 1.5"

I check a few of them and it looks like what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文