17

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?

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

13
  • 1
    Any particular reason for not using a third party library? Joda Time is a much better API than the built-in ones. Unless you have a really, really good reason not to use it, I strongly recommend that you jump to it. Commented Mar 4, 2010 at 14:11
  • 1
    Yup. I know 9 out of 10 will suggest me using Joda Time. That's why I put a remark on no 3rd party library :) Commented Mar 4, 2010 at 14:12
  • 1
    While Calendar.getInstance() may be expensive, that is only a factor if it is affecting the performance of your application. If it is not, I wouldn't worry about it. Commented Mar 4, 2010 at 14:15
  • 1
    @jarnbjo: Avoid standard Java API if you don't actually need functionality not offered by the Joda. Compared to the Joda, standard Java API is rather buggy, poorly documented and has often ambiguous, misleading method names ( and constants ). Commented Mar 4, 2010 at 14:47
  • 1
    Calendars are rather cheap to create, but if you do it in a loop, you can cache the calendar instance and reuse it. You need to make sure it's used in one thread at a time, so I wouldn't recommend caching in an instance variable. Commented Mar 4, 2010 at 14:47

4 Answers 4

20

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.

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.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for constructing directly, but be careful of using a specific Calendar.
Yep. I understand that using day, month, year implies a knowledge about the calendar being used (but if it's generic handling of DMY... there's no method for creating the calendar... :)
The point of using Calendar.getInstance() is to allow for locale-specific differences. Creating a GregorianCalendar directly throws away that benefit to save... one line of code. It'd be better to simply put the creation in a static factory method.
8

"Rather expensive" is somewhat vague. Have you actually tried using the code you've supplied, measured it and found it to be too expensive? 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 Date to 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?

(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.)

3 Comments

Joda Time is probably overkill. "Expensive" in relation to Calendars is something that was true in the JDK 1.4 days that has persisted. I was under the same misconception as the OP til I tested it and could easily instantiate hundreds of thousands of these a second no problem.
@cletus: Most of my reason for recommending Joda Time isn't to speed this up - it's to use a better API. I'm assuming that if he's creating a Date, he's going to use it as well. Joda Time is simply a better way of handling dates and times, period.
One has to wonder if Joda Time has become to Java what jQuery has become to Javascript: something always suggested. :)
2

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.

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

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.