I need to get the time in milliseconds from the timepicker, so that later I can check it with the current time, in order to carry out some activity. Any help.
Asked
Active
Viewed 6,008 times
1
-
1duplicate of http://stackoverflow.com/questions/13223203/convert-date-and-time-to-milliseconds-in-android – Shabbir Dhangot Feb 09 '15 at 09:49
-
1there are only hour and minute field in a time picker. the remained parts (second and millisecond) of the picked time will be zero. isn't it? – alijandro Feb 09 '15 at 09:49
-
No i need to get the time in milliseconds from a normal timepicker, @ShabbirDhangot – epiclapser Feb 09 '15 at 09:49
-
@alijandro Yes you are right – epiclapser Feb 09 '15 at 09:50
-
Is that not the answer you want? millisecond is zero, no needing to get again. – alijandro Feb 09 '15 at 09:51
-
No I want it in the format of getCurrentTimeMillis() function, @alijandro – epiclapser Feb 09 '15 at 09:52
-
This will help you to get seconds.https://github.com/IvanKovac/TimePickerWithSeconds – Shabbir Dhangot Feb 09 '15 at 09:56
-
@ShabbirDhangot I want to check the current time in a service with the picked time, any help? – epiclapser Feb 09 '15 at 10:00
-
I suggest you to skip milliseconds. because Time picker is not specially made for the milliseconds. – Shabbir Dhangot Feb 09 '15 at 10:10
-
ok ill try to avoid milliseconds – epiclapser Feb 09 '15 at 10:12
2 Answers
2
TimePickerDialog timePickerDialog = new TimePickerDialog(this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
final Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
long millis = calendar.getTimeInMillis();
}
}, 10, 20, true);

alijandro
- 11,627
- 2
- 58
- 74
-1
Use below code :
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
String dateInString = "22-01-2015 10:20:56";
Date date = sdf.parse(dateInString);
System.out.println(dateInString);
System.out.println("Date - Time in milliseconds : " + date.getTime());
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
System.out.println("Calender - Time in milliseconds : " + calendar.getTimeInMillis());
Output :
22-01-2015 10:20:56
Date - Time in milliseconds : 1421893256000
Calender - Time in milliseconds : 1421893256000

Karan Maru
- 991
- 6
- 17
-
take date-time from your picker and convert it into milliseconds as described above. – Karan Maru Feb 09 '15 at 10:32