Convert date and time to milliseconds in Android

Ahmad Abbasi picture Ahmad Abbasi · Nov 4, 2012 · Viewed 44.3k times · Source

I have Date and Time from DatePicker and TimePicker. Now i want to change the selected date and time into milliseconds. How can I do this???

For Example I have Date selected 2-5-2012 and Time is 20:43

Now I have to convert this Date Time into milliseconds something like

DateTimeInMilliseconds = 1234567890

Answer

Sam picture Sam · Nov 4, 2012

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();