Aug 03

All the constructors and methods of the Date class that allow you to set the date value are deprecated so how do we set a Date instance to the date we need it to be.

Answer is to use the Calendar class and use the various set() methods to set the date required. Once you have that set you can use the getTime() method to get your required Date.

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

An alternative is to use the GregorianCalendar class directly

Calendar cal = new GregorianCalendar(year, month, day);
Date date = cal.getTime();

written by objects \\ tags: , ,


Leave a Reply

You must be logged in to post a comment.