Deprecated Date class

S

stixwix

Hi,

Is there a way to do the following without using the deprecated Date
constructor Date(String)?

Calendar calendar = new GregorianCalendar();
Date d = new Date("Friday, 24 March 2006");
calendar.setTime(d);

thanks
Andy
 
N

Nils Bandener

Hi!
Is there a way to do the following without using the deprecated Date
constructor Date(String)?

Calendar calendar = new GregorianCalendar();
Date d = new Date("Friday, 24 March 2006");
calendar.setTime(d);

Yep, using java.text.SimpleDateFormat:

Calendar calendar = new GregorianCalendar();
SimpleDateFormat format = new SimpleDateFormat("E, d M y");
Date d = format.parse("Friday, 24 March 2006");
calendar.setTime(d);

(You'll have to handle some exceptions, see the API doc for these)

Bye

Nils
 
J

John O'Conner

stixwix said:
Hi,

Is there a way to do the following without using the deprecated Date
constructor Date(String)?

Calendar calendar = new GregorianCalendar();
Date d = new Date("Friday, 24 March 2006");
calendar.setTime(d);


Calendar calendar = Calendar.getInstance();
calendar.set(2006, Calendar.MARCH, 24);
Date d = calendar.getTime();

How's that?

Regards,
John O'Conner
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top