android – time picker showing time like 4:7 instead of 04:07-ThrowExceptions

Exception or error:

I have a time picker function which sets time in an EditText . But the format it shows is not suitable. for example for 04:07pm is shown as 4:7. whenever the digit in time is less than 10 it removes the 0 automatically.
please help me out.
My code is

        if (v == btnTimePicker1)
               {

                // Process to get Current Time
                final Calendar c = Calendar.getInstance();
                mHour = c.get(Calendar.HOUR_OF_DAY);
                mMinute = c.get(Calendar.MINUTE);

                // Launch Time Picker Dialog
                TimePickerDialog tpd = new TimePickerDialog(this,
                        new TimePickerDialog.OnTimeSetListener() {

                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay,
                                    int minute) {
                                // Display Selected time in textbox
                                txtTime1.setText(hourOfDay + ":" + minute);
                            }
                        }, mHour, mMinute, false);
                tpd.show();




            }
How to solve:

Just change the line:

txtTime1.setText(hourOfDay + ":" + minute);

to:

txtTime1.setText(String.format("%02d:%02d", hourOfDay, minute));

and all will be well.

If you want a 12-hour clock instead of a 24-hour one, then replace that line with these instead:

int hour = hourOfDay % 12;
if (hour == 0)
    hour = 12;
txtTime1.setText(String.format("%02d:%02d %s", hour, minute, 
                               hourOfDay < 12 ? "am" : "pm"));

or you could do it in just 2 lines with:

int hour = hourOfDay % 12;    
txtTime1.setText(String.format("%02d:%02d %s", hour == 0 ? 12 : hour,
                               minute, hourOfDay < 12 ? "am" : "pm"));

###

The logic is simple, i have just trimmed the answers above

just replace the line where we set time in editText with

txtTime.setText(pad(hourOfDay) + ":" + pad(minute));

then add a function for it i.e

       public String pad(int input) 
         {

            String str = "";

            if (input > 10) {

                str = Integer.toString(input);
            } else {
                str = "0" + Integer.toString(input);

            }
            return str;
        }

###

You can check if the hours and minutes are less then ten.
If so, you just add a “0” infront of that specific string.
Just modify your code like this:

@Override
    public void onTimeSet(TimePicker view, int hourOfDay,
    int minute) {
        // Display Selected time in textbox

        String hourString;
        if (hourOfDay < 10)
            hourString = "0" + hourOfDay;
        else
            hourString = "" +hourOfDay;

        String minuteSting;
        if (minute < 10)
            minuteSting = "0" + minute;
        else
            minuteSting = "" +minute;

        txtTime1.setText(hourString + ":" + minuteSting);
    }

###

First need to create one function that check your input and convert it in String as per condition.

public String pad(int input) {
    if (input >= 10) {
        return String.valueOf(input);
    } else {
        return "0" + String.valueOf(input);
    }
}

Then you can call like this

 txtTime1.setText(pad(hourOfDay) + ":" + pad(minute));

###

You can use the following code –

  public void onTimeSet(TimePicker view, int hourOfDay, int minuteOfDay) {
   int hour;
   String minute, amOrPm;
   if (hourOfDay > 12) {
    hour = hourOfDay - 12;
    amOrPm= "PM";
   } else {
    hour = hourOfDay;
    amOrPm = "AM";
   }
   if(minuteOfDay < 10) {
      minute = "0"+minuteOfDay;
   } else {
      minute = "" + minuteOfDay;
   }
   txtTime1.setText(hour + " : " + minute + " " + amOrPm);
  }

###

TimePickerDialog tpd = new TimePickerDialog(YourActivity.this, new 
TimePickerDialog.OnTimeSetListener() {

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        if (hourOfDay == 0) {
            hourOfDay = 12;
        }
        mEndTime = (String.format("%02d:%02d", hourOfDay, minute));
        //mEndTime = (hourOfDay + ":" + minute);

        mEditTextEnd.setText(mEndDate + " " + mEndTime + ":00");
    }
}, mHourEnd, mMinuteEnd, true);
tpd.show();

###

Try this

simpleTimePicker1 = (TimePicker) findViewById(R.id.timedawn);
simpleTimePicker1.setIs24HourView(true);
simpleTimePicker1.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
    @Override
    public void onTimeChanged(TimePicker view, int hourOfDay1, int minute1 ) {

        String shour1 = String.valueOf(hourOfDay1);
        String sminute1 = String.valueOf(minute1);


        if ((shour1.length() == 1 ) && (sminute1.length() == 1)) {
            sHour1= "0" + shour1;
            sMinute1= "0" + sminute1;
        }
        else if ((shour1.length() == 2 ) && (sminute1.length() == 1)) {
            sHour1= shour1;
            sMinute1= "0" + sminute1;
        }
        else if ((shour1.length() == 1 ) && (sminute1.length() == 2)) {
            sHour1= "0" + shour1;
            sMinute1= sminute1;
        }
        else{
            sHour1= shour1;
            sMinute1= sminute1;
        }
        String timew = sHour1+":" + sMinute1;
        time3=timew;
    }

});

###

i might be little late to the party but this will surely help to someone else.

 private String getTime(int hr, int min) {
    Time tme = new Time(hr, min, 0);//seconds by default set to zero
    Format formatter;
    formatter = new SimpleDateFormat("h:mm a");
    return formatter.format(tme);
}

Leave a Reply

Your email address will not be published. Required fields are marked *