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

cygmwpex  于 2023-05-15  发布在  Android
关注(0)|答案(2)|浏览(111)
int hours = time.getHours();
int minutes = time.getMinutes();
int seconds = time.getSeconds();

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

qvtsj1bj

qvtsj1bj1#

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保持如下值:十点二十一分

e5nqia27

e5nqia272#

您可以使用下面提到的方法从时间选择器中获取小时,然后可以按以下方式对其进行格式化:

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

相关问题