android-Timepicker方法getcurrenthour()已弃用,如何在android 4.5中使用getHour()?

时间:2017-09-23 18:19:53

标签: android android-alarms android-timepicker

int hours = time.getHours();
int minutes = time.getMinutes();
int seconds = time.getSeconds();

这些功能不适用于Android 4.5,如何在该版本中使用它们。

2 个答案:

答案 0 :(得分:0)

  1. 首先,使用事件侦听器以mSecs:
  2. 检索当前时间

    然后,稍后,您将以所需格式检索时间。比如说,小时:分钟(“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

答案 1 :(得分:0)

你可以使用方法:getHour()和getMinutes()从timePicker获取小时,然后你可以用这种方式格式化它:

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