I'm working on an app in which I'm getting date and time from date and time picker dialog. I need to convert it into milliseconds in long value and upload to the server. How to achieve this?
Asked
Active
Viewed 4,313 times
3 Answers
2
first u create calender with values from date and time picker
Calendar calendar = Calendar.getInstance();
calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(),
timePicker.getCurrentHour(), timePicker.getCurrentMinute(), 0);
long startTime = calendar.getTimeInMillis();

RaghuPrasanth V
- 194
- 16
1
As already answered here: how to get milliseconds from a date picker
Just create a calendar and get time in millis
DatePicker datePicker = new DatePicker(this);
TimePicker timePicker = new TimePicker(this);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
cal.set(Calendar.MONTH, datePicker.getMonth());
cal.set(Calendar.YEAR, datePicker.getYear());
cal.set(Calendar.HOUR, timePicker.getCurrentHour());
cal.set(Calendar.MINUTE, timePicker.getCurrentMinute());
long millis = cal.getTimeInMillis();

Dennis
- 372
- 1
- 2
- 17
-
Thanks I'll try this – parag pawar May 29 '18 at 07:10
-
check the edit, I fix the picker – Dennis May 29 '18 at 07:12
-
1Using Joda or (if available) Java 8 time is nearly always a cleaner solution. – chrylis -cautiouslyoptimistic- May 29 '18 at 07:19
-
`java.time` was introduced in Java 8 alright, but *is* available for lower API level Android through [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP), the Android edition of the backport. – Ole V.V. May 29 '18 at 07:22
0
Override TimePickerDialog.onTimeChanged(). It will be called once the selected time changes. You can then cinvert the result to millseconds by doing some simple maths.

Hauke Sommerfeld
- 63
- 4