Search code examples
androiddatepickerdatetimepickertimepicker

Convert date and time to milliseconds in Android


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


Solution

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