Getting a date object from a string

I have a string object.

String s = "2006-05-11 17:04:28.000"

How can I create a date object from that string?

I tried Date date = new Date(s);

But that looks like a deprecated method. What is the best way ?

thanks in advance

Anu
expertsitAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
Use a DateFormat



DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date d = df.parse("2006-05-11 17:04:28.000");
0
 
expertsitAuthor Commented:
Thanks that worked.
0
 
CEHJCommented:
:-)
0
 
Curtis ShullFounderCommented:
You can try:


strFormat = "2006-05-11 17:04:28.000";
DateFormat myDateFormat = new SimpleDateFormat(strFormat);
Date myDate = null;
try {
     myDate = myDateFormat.parse(strDate);
} catch (ParseException e) {
     System.out.println("Invalid Date Parser Exception ");
     e.printStackTrace();
}
System.out.println("Finished Date Function " + myDate.getDay());
0
 
expertsitAuthor Commented:
Thanks for your response.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.