如何从android中的timepicker获取当前时间(以毫秒为单位)?

时间:2015-02-09 09:45:06

标签: android

我需要从时间戳中获取时间,以毫秒为单位,以便稍后我可以使用当前时间检查它,以便执行某些活动。 任何帮助。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:-1)

使用以下代码:

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

输出:

22-01-2015 10:20:56
Date - Time in milliseconds : 1421893256000
Calender - Time in milliseconds : 1421893256000