Search code examples
androiddatepickerdatetimepickermillisecondsdatetime-conversion

Conversion of DateTime into milliseconds


I want to convert Date+Time into milliseconds. i am using this code to convert them.

final TimePicker time = (TimePicker) findViewById(R.id.timePicker1);
final DatePicker date = (DatePicker) findViewById(R.id.datePicker1);

final Calendar calendar = Calendar.getInstance();

calendar.set(date.getYear(), date.getMonth(),
                            date.getDayOfMonth(), time.getCurrentHour(),
                            time.getCurrentMinute(), 0);
final long startTime = calendar.getTimeInMillis();

but it does't give me the correct value. for example if i want to convert this "Sep 23 2000 02:45 AM" it gives me this value "96965900504". but this value is not like that as in these sites

http://www.ruddwire.com/handy-code/date-to-millisecond-calculators/

http://www.timestampconvert.com/?go1=true&m=09&d=23&y=2000&hours=02&min=45&sec=0&Submit=++++++Convert+to+timestamp+++++&offset=-5

then i try to us time.getCurrentHour() + 5 because of my current location is GMT+5 but it does't gives me correct result. How can i get the correct value of date+time into milliseconds???


Solution

  • ok its done I just add TimeZone to this code

    TimeZone timezone = TimeZone.getTimeZone("GMT+05:00");
    calendar.setTimeZone(timezone);