• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

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
0
expertsit
Asked:
expertsit
  • 2
  • 2
1 Solution
 
CEHJCommented:
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
 
Onyx23Commented:
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now