java.util.Date 的替代品

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18958549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:54:15  来源:igfitidea点击:

Alternatives for java.util.Date

javajava.util.date

提问by Sangram Anand

Currently I am using the deprecated setMethods of java.util.Date. As I want to migrate away from it, what are the alternatives and what advantages do they have?

目前我使用的是过时set的方法java.util.Date。当我想远离它时,有哪些替代方案以及它们有哪些优势?

I need to have a Datethat is set to today, midnight for a HQLquery that selects everything that happened today.

我需要有一个Date设置为今天,午夜的HQL查询,选择今天发生的一切。

Currently I use:

目前我使用:

Date startingDate = new Date();
startingDate.setHours(0);
startingDate.setMinutes(0);
startingDate.setSeconds(0);

采纳答案by Angelo Fuchs

Note: This answer is most likely no longer accurate for Java 8 and beyond, there is a better date/calendar APInow.

注意:对于 Java 8 及更高版本,此答案很可能不再准确,现在有更好的日期/日历 API

The standard alternate is using the CalendarObject.

标准的替代方法是使用CalendarObject。

Calendar cal = Calendar.getInstance(); // that is NOW for the timezone configured on the computer.
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTES, 0);
cal.set(Calendar.SECONDS, 0);
Date startingDate = cal.getTime();

Calendarhas the advantage to come without additional libraries and is widely understood. It is also the documented alternative from the Javadocof Date

Calendar具有无需额外库的优势,并被广泛理解。这也是从所记录替代的JavadocDate

The documentation of Calendarcan be found here: Javadoc

的文档Calendar可以在这里找到:Javadoc

Calendarhas one dangerous point (for the unwary) and that is the after/ beforemethods. They take an Objectbut will only handle CalendarObjects correctly. Be sure to read the Javadoc for these methods closely before using them.

Calendar有一个危险点(对于粗心的人),那就是after/before方法。他们接受Object但只会Calendar正确处理对象。在使用这些方法之前,请务必仔细阅读 Javadoc。

You can transform CalendarObjects in quite some way like add a day (cal.add(Calendar.DAY_OF_YEAR, 1);) or "scroll" through the week (cal.roll(Calendar.DAY_OF_WEEK, 1);) and such. Have a read of the class description in the Javadoc to get the full picture.

您可以通过Calendar某种方式转换对象,例如添加一天 ( cal.add(Calendar.DAY_OF_YEAR, 1);) 或“滚动”一周 ( cal.roll(Calendar.DAY_OF_WEEK, 1);) 等。阅读 Javadoc 中的类描述以了解全貌。

回答by Jean Logeart

The best alternative is to use the Joda Time API:

最好的选择是使用Joda Time API

Date date = new DateMidnight().toDate();     // today at 00:00

To avoid the to-be deprecated DateMidnight:

为避免被弃用DateMidnight

Date date = new DateTime().withMillisOfDay(0).toDate();

回答by Lokesh

You may use joda dates library , you will get lots of flexibility with that. Otherwise you can use java.util.Calendar for creating custom date.

您可以使用 joda 日期库,您将获得很大的灵活性。否则,您可以使用 java.util.Calendar 创建自定义日期。

回答by sunysen

1、The alternate is using the java.util.CalendarObject;

1、备用正在使用java.util.Calendar对象;

2、Detailed usage, please refer to the link below;

2、详细使用方法请参考以下链接;

http://www.leepoint.net/notes-java/other/10time/30calendar.html

http://www.leepoint.net/notes-java/other/10time/30calendar.html

回答by Joel

Date does not handle internationalization properly, that's why it was deprecated.

Date 没有正确处理国际化,这就是它被弃用的原因。

Prior to JDK 1.1, the class Date had two additional functions. It allowed the interpretation of dates as year, month, day, hour, minute, and second values. It also allowed the formatting and parsing of date strings. Unfortunately, the API for these functions was not amenable to internationalization. As of JDK 1.1, the Calendar class should be used to convert between dates and time fields and the DateFormat class should be used to format and parse date strings. The corresponding methods in Date are deprecated.

在 JDK 1.1 之前,类 Date 有两个附加功能。它允许将日期解释为年、月、日、小时、分钟和秒值。它还允许格式化和解析日期字符串。不幸的是,这些函数的 API 不适合国际化。从 JDK 1.1 开始,应该使用 Calendar 类在日期和时间字段之间进行转换,并且应该使用 DateFormat 类来格式化和解析日期字符串。不推荐使用 Date 中的相应方法。

The simplest alternative is to use java.util.Calendarinstead:

最简单的替代方法是使用java.util.Calendar

Calendar calendar = Calendar.getInstance(); // get a calendar instance (current)

and the call calendar.set(...) methods.

并调用 calendar.set(...) 方法。