views:

115

answers:

5

According to the java API, the constructor Date(year, month, day) is depreciated. I know that I can replace it with the following code:

Calendar myCal = Calendar.getInstance();
myCal.set(Calendar.YEAR, theYear);
myCal.set(Calendar.MONTH, theMonth);
myCal.set(Calendar.DAY_OF_MONTH, theDay);
Date theDate = myCal.getTime();

However, I would like something shorter to replace it with (ideally one-two lines).

Any suggestions?

+8  A: 

You could use new GregorianCalendar(theYear, theMonth, theDay).getTime():

public GregorianCalendar(int year, int month, int dayOfMonth)

Constructs a GregorianCalendar with the given date set in the default time zone with the default locale.

aioobe
I believe this is the "official Java correct answer" of how you are supposed to do it. Personally I rately have year, month and day in three fields like that, I usually have some form of string, so I use SimpleDateFormat.
Jay
Remember that January is 0, not 1. Or use Calendar constants: Calendar.JANUARY, etc.
Alexander Pogrebnyak
+8  A: 

This is yet another reason to use Joda Time

new DateMidnight(2010, 3, 5)
Sean Owen
Introducing a dependency of a third party library for such a simple thing seems like an overkill.
aioobe
+1 Or ANYTHING but Sun's time classes. Those are horrible!
Vuntic
@aioobe it might seem like overkill, but if there wasn't a use for it, it would have never been created. Joda Time is not only easier to use (IMO), it is also much more efficient if you are creating many calendars or datetime objects.
NickLarsen
This constructor does not exist for DateTime. Please add 0 - fields for hour, minute, second, millisecond.
Alexander Pogrebnyak
Ah of course, I meant DateMidnight
Sean Owen
+3  A: 

You could use

new SimpleDateFormat( "yyyyMMdd" ).parse( "20100520" )
tangens
But he has `theYear`, `theMonth`, `theDay`. Do you suggest he converts those to strings, and then concatenates them?
aioobe
It should be mentioned that SimpleDateFormat isn't thread safe
stacker
@stacker: What is not thread-safe in this answer?
doublep
@doublep SimpleDateFormat itself, if called by multiple threads the results are unpredictable - something I spend hours to fix it.
stacker
@stacker Are you sure? I thought it was only shared instances of SimpleDateFormat - not the class itself. You're saying the class has static state?
CPerkins
To the best of my knowledge, SimpleDateFormat is not thread safe in that you cannot create a single instance, use it from two different threads that may execute simultaneously, and be confident that it will work. But I don't think there's any risk in creating an instance within a function and using it, as that instance would then only be used by the one thread. If I'm missing something here, feel free to correct me, but I've never observed a problem with such usage.
Jay
But tangens is creating a new SimpleDateFormat each time, so it's not being shared by different threads.
Steve Kuo
@stacker: An instance of `SimpleDataTime` is not thread safe, but the class certainly is. Instantiating a new copy on a thread and using that (as in the example) will never be a problem even with multiple threads executing it concurrently - each thread will have their own copy. This may not be the most efficient or correct answer for the question, however, but for other reasons.
Kevin Brock
Sorry for the confusion, next time I first think.
stacker
+3  A: 

Calendar has a set() method that can set the year, month, and day-of-month in one call:

myCal.set( theYear, theMonth, theDay );
martin clayton
+1  A: 

Why don't you just write a simple utility method:

public final class DateUtils {
    private DateUtils() {
    }

    public static Calendar calendarFor(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, day);
        return cal;
    }

    // ... maybe other utility methods
}

And then call that everywhere in the rest of your code:

Calendar cal = DateUtils.calendarFor(2010, Calendar.MAY, 21);
Jesper
I do like the simplicity of this answer. Very readable and reusable.
hibernate