How to convert a string Date to long millseconds

Dawood Ahmed picture Dawood Ahmed · Sep 18, 2012 · Viewed 228.2k times · Source

I have a date inside a string, something like "12-December-2012". How can I convert this into milliseconds (long)?

Answer

Jon Lin picture Jon Lin · Sep 18, 2012

Using SimpleDateFormat

String string_date = "12-December-2012";

SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy");
try {
    Date d = f.parse(string_date);
    long milliseconds = d.getTime();
} catch (ParseException e) {
    e.printStackTrace();
}