java 构造日期 - 一种有效的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2379835/
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-10-29 20:52:28  来源:igfitidea点击:

Construct Date - An efficient way

java

提问by Cheok Yan Cheng

May I know what is the most efficient way to construct a date object using a specific day, month, and year.

我可以知道使用特定的日、月和年构造日期对象的最有效方法是什么吗?

Date(int year, int month, int day) 

This construct is depreciated. Hence, what I usually do is:

此构造已折旧。因此,我通常做的是:

Calendar calendar = Calendar.getInstance();
Date date = calendar.set(year, month, date).getTime();

However, my understanding is that Calendar.getInstance() is rather expensive. What is the most efficient way to construct a Date object? Or should I just use Date(int year, int month, int day) quietly without telling the rest?

但是,我的理解是 Calendar.getInstance() 相当昂贵。构造 Date 对象的最有效方法是什么?或者我应该安静地使用 Date(int year, int month, int day) 而不告诉其他人?

Please don't suggest using any third-party library.

请不要建议使用任何第三方库。

回答by helios

With this you can avoid the innecesary "now time" instance creation.

有了这个,您可以避免不必要的“现在时间”实例创建。

Date coolDate = new GregorianCalendar(year, month, day).getTime();

You can check GregorianCalendar javadoc for other constructors. You have date+time, and timezone.

您可以检查 GregorianCalendar javadoc 以获取其他构造函数。您有日期+时间和时区。

Anyway I agree with Jon Skeet that it's not so expensive. I agree with you that code doesn't need a default "now" initialization.

无论如何,我同意 Jon Skeet 的观点,它并不那么贵。我同意你的看法,代码不需要默认的“现在”初始化。

回答by Jon Skeet

"Rather expensive" is somewhat vague. Have you actually tried using the code you've supplied, measured it and found it to be tooexpensive? Do you have a concrete idea of how cheap you need this operation to be?

“比较贵”有点含糊。你有没有使用您提供的代码实际上尝试,测量了一下,发现它是昂贵?你对你需要这个操作的成本有多低有一个具体的想法吗?

Also, you haven't specified which time zone you want the value in the Dateto represent. UTC? The default time zone? What time of day do you want it to be - midnight, or the current time of day? Your current code will keep the existing time of day - is that really what you want?

此外,您还没有指定您希望 中的值Date代表哪个时区。世界标准时间?默认时区?您希望它是一天中的什么时间 - 午夜,还是一天中的当前时间?您当前的代码将保留现有时间 - 这真的是您想要的吗?

(As I mentioned in a comment, I would strongly suggest you move to Joda Time - but even if you don't, you should still check whether or not you've actually got a problem with your existing code before looking for a solution.)

(正如我在评论中提到的,我强烈建议您转向 Joda Time - 但即使您不这样做,在寻找解决方案之前,您仍应检查现有代码是否确实存在问题。 )

回答by duffymo

I would simply do this:

我会简单地这样做:

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date thisDate = formatter.parse("2010-03-04");

It's pretty efficient from a lines of code standpoint; I can't speak to its runtime efficiency vis a vis Calendar.

从代码行的角度来看,它非常有效;与 Calendar 相比,我无法谈论它的运行时效率。

Ten years later in 2020: the only right answer is to use classes in java.util.timepackage.

十年后的 2020 年:唯一正确的答案是使用java.util.time包中的类。

LocalDate localDate = LocalDate.now();
LocalDate.of(2020, 3, 7);
LocalDate.parse("2020-03-07");

回答by Michael Borgwardt

Whatever you use, as long as it's in the Java Standard API, it will involve the use of Calendar(both the Dateconstructor and SimpleDateFormatuse it internally), so there's no point fretting about that class's supposed inefficiency.

无论您使用什么,只要它在 Java 标准 API 中,就会涉及使用CalendarDate构造函数和SimpleDateFormat内部使用),因此不必担心该类的所谓低效率。