2015-02-09 108 views
0

我需要從timepicker獲得以毫秒爲單位的時間,以便以後我可以用當前時間檢查它,以執行一些活動。 任何幫助。如何從android中的timepicker獲取以毫秒爲單位的當前時間?

+1

重複http://stackoverflow.com/questions/13223203/convert-date-and-time-to- milliseconds-in-android – 2015-02-09 09:49:25

+1

在時間選擇器中只有小時和分鐘字段。所選時間的剩餘部分(秒和毫秒)將爲零。不是嗎? – alijandro 2015-02-09 09:49:28

+0

不,我需要從一個正常的時間選擇器得到的毫秒數,@ShabbirDhangot – epiclapser 2015-02-09 09:49:39

回答

-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 
+2

我不認爲這是正確 – epiclapser 2015-02-09 10:25:08

+0

從你的選擇器採取日期時間並將其轉換爲毫秒如上所述。 – 2015-02-09 10:32:45

+0

@epiclapser你是對的 – 2015-02-09 10:37:47

-1
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); 
相關問題