I\'m trying to do something really simple, but starting to realize that dates in Java are a bit of minefield. All I want is to get passed groups of three ints ( a year, a m
LG's answer is valid but in general you should use Calendar
for all your date operations and leave Date
out of it unless your API requires it explicitly. In that case you can just convert it when passing it to the API.
Well, you can use the deprecated constructor. It has been deprecated for over 13 years and still works - it isn't going anywhere. If you don't want to do that and don't want to use a third party library, you have to use Calendar.
In Java 7, hopefully there will be a new time API, and you won't have the need for a third party API anymore.
The idea is to use the Calendar class, like so:
Calendar cal = Calendar.getInstance();
cal.set(year, month, date);
Date date = cal.getTime();
Indeed, if you check the Javadoc of the constructor you are mentioning, it is exactly what is suggested:
Date(int year, int month, int date)
Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date) or GregorianCalendar(year + 1900, month, date).
Or ... use JodaTime :-).