time picker showing time like 4:7 instead of 04:07

前端 未结 8 1851
小蘑菇
小蘑菇 2021-02-05 05:41

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 l

相关标签:
8条回答
  • 2021-02-05 06:20

    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));
    
    0 讨论(0)
  • 2021-02-05 06:23
    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();
    
    0 讨论(0)
  • 2021-02-05 06:25

    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;
        }
    
    });
    
    0 讨论(0)
  • 2021-02-05 06:36

    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);
        }
    
    0 讨论(0)
  • 2021-02-05 06:37

    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);
    }
    
    0 讨论(0)
  • 2021-02-05 06:40

    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"));
    
    0 讨论(0)
提交回复
热议问题