Convert date and time to milliseconds in Android Convert date and time to milliseconds in Android android android

Convert date and time to milliseconds in Android


You can create a Calendar object with the values from your DatePicker and TimePicker:

Calendar calendar = Calendar.getInstance();calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(),              timePicker.getCurrentHour(), timePicker.getCurrentMinute(), 0);long startTime = calendar.getTimeInMillis();


Merge the two strings together, and parse them using SimpleDateFormat.

Something like this:

String toParse = myDate + " " + myTime; // Results in "2-5-2012 20:43"SimpleDateFormat formatter = new SimpleDateFormat("d-M-yyyy hh:mm"); // I assume d-M, you may refer to M-d for month-day instead.Date date = formatter.parse(toParse); // You will need try/catch around thislong millis = date.getTime();

Sample on IDEOne: http://ideone.com/nOJYQ4


You can use this code

String string_date = "12-December-2012";SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy");Date d = f.parse(string_date);long milliseconds = d.getTime();