12

Possible Duplicate:
how to convert java string to Date object

In my Java code I have input String like 05.10.2011 which I need to convert to milliseconds.

So:

String someDate = "05.10.2011"; 

I have to convert to match milliseconds format.

Community
  • 1
  • 1
user696847
  • 469
  • 2
  • 7
  • 16

3 Answers3

30
String someDate = "05.10.2011";
SimpleDateFormat sdf = new SimpleDateFormat("MM.dd.yyyy");
Date date = sdf.parse(someDate);
System.out.println(date.getTime());
dku.rajkumar
  • 18,414
  • 7
  • 41
  • 58
6

You can use Java's SimpleDateFormat to easily convert to a Date instance.

SimpleDateFormat formatter = new SimpleDateFormat("MM.dd.yyyy"); // Month.Day.Year

Date d = formatter.parse(inputString);
long timestamp = d.getTime();
Jason Nichols
  • 11,603
  • 5
  • 34
  • 53
4

use the simpleDateFormat which takes a string and converts it to Date then call the getTime() to get the date in milliseconds format

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89