Search code examples
androidandroid-alarmsandroid-timepicker

android-Timepicker methods getcurrenthour() is deprecated, how to use getHour() in android 4.5 ??


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

These functions are not usable for Android 4.5, how do I use them in that version.


Solution

    1. First, you use event listener to retrieve current time in mSecs:

    Then, later, you retrieve the time in required format. Say, hours:minutes ("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);
    

    Now : humanDate keeps value like: 10:21