2017-09-23 57 views

回答

0
  1. 首先,你可以使用事件偵聽器在毫秒檢索當前時間:

然後,以後,您檢索所需格式的時間。說,小時:分鐘( 「HH:MM」):

Long newTimeMsecValue = Calendar.getInstance().getTime().getTime(); 
    timePicker.setOnTimeChangedListener(new 
    TimePicker.OnTimeChangedListener() { 
      @Override 
      public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { 
     //NOTE: hourOfDay is always in 24h mode, not dependent of AM/PM mode! 
      newTimeMsecValue = new Long (((hourOfDay * 3600) + (minute * 60)) * 1000); // 
    }}); 


    Calendar myCalend = Calendar.getInstance(); 
     myCalend.setTimeInMillis(newTimeMsecValue);// value from the code above 
     Date calDate = myCalend.getTime(); 
     DateFormat df = new SimpleDateFormat("HH:mm"); 
     String humanDate = df.format(calDate); 

現在:humanDate值會保持這樣的:10:21

0

您可以使用方法:getHour()和getMinutes()來獲得小時,然後你可以用這種方式格式化:

String date = String.format(Locale.getDefault(),「%02d:%02d」,timePicker.getHour(),timePicker.getMinute ));

相關問題