为什么是“new Date(int year,int month,int day)”已弃用? [英] Why was "new Date(int year, int month, int day)" deprecated?

查看:1064
本文介绍了为什么是“new Date(int year,int month,int day)”已弃用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近继承的应用程序是关于构造函数的完全不推荐的警告:

 日期d = new Date一年,一个月,一天)

有人知道还是可以指出一个简单的原因因为这被这样的替换:

 日期d = null; 
日历cal = GregorianCalendar.getInstance();
cal.set(1900 +年,月,日);
d = cal.getTime();

现在,显然不推荐的警告本身不是问题,但是你可以想象到数百万的LOC如果这个构造函数被删除,那么这个构造函数会不会很痛苦?



在我在基准测试中的简短的小游戏中,后者需要大约50%的时间来执行。 >

解决方案

最初, Date 旨在包含有关日期的所有逻辑,但API设计人员终于意识到,他们迄今为止的API非常不足,无法彻底扩展,以便正确处理诸如时区,地点,不同日历,夏令时等问题。



所以他们创建了 Calendar 以处理所有这些复杂性,并将 Date 降级到一个简单的时间戳,不赞成它的所有功能涉及格式化,解析和个别日期字段。



BTW,内部这些方法,如 D ate(int,int,int)构造函数现在调用 Calendar ,所以如果你看到速度有所不同,那么你做错了什么调用日历



底线:不是Java的日历 API过于复杂,这是人类对日期的概念,而 Calendar 的唯一问题在于,它提供的功能并不是最常用的快捷方式。


My application I've recently inherited is FULL of deprecation warnings about the constructor:

Date d = new Date(int year, int month, int day)

Does anyone know or can point to a reason why something as simple as this was "replaced" with something like this:

Date d = null;
Calendar cal = GregorianCalendar.getInstance();
cal.set(1900 + year, month, day);
d = cal.getTime();

Now, obviously deprecation warnings are not a problem in themselves, but can you imagine the millions of LOC that would be crying out in agony if this constructor was ever removed?

In my brief little play at benchmarking, the latter takes about 50% more time to execute.

解决方案

Originally, Date was intended to contain all logic concerning dates, but the API designers eventually realized that the API they had so far was woefully inadequate and could not be cleanly extended to deal correctly with issues such as timezones, locales, different calendars, daylight savings times, etc.

So they created Calendar to deal with all that complexity, and relegated Date to a simple timestamp, deprecating all its functionality that dealt with formatting, parsing and individual date fields.

BTW, internally these methods such as Date(int, int, int) constructor now call Calendar, so if you see a difference in speed, then you're doing something wrong when calling Calendar.

The bottomline: It's not Java's Calendar API that is overly complex, it's the human concept of dates, and the only problem with Calendar is that it offers not much in the way of shortcuts to the most common usages.

这篇关于为什么是“new Date(int year,int month,int day)”已弃用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆