I have a date inside a string, something like \"12-December-2012\". How can I convert this into milliseconds (long)?
you can use the simpleDateFormat to parse the string date.
Take a look to SimpleDateFormat
class that can parse a String
and return a Date
and the getTime
method of Date
class.
Easiest way is used the Date Using Date() and getTime()
Date dte=new Date();
long milliSeconds = dte.getTime();
String strLong = Long.toString(milliSeconds);
System.out.println(milliSeconds)
java.util.Date
using date formatterSimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
Date date = (Date)formatter.parse("12-December-2012");
long mills = date.getTime();
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();
}