时间选择器格式

时间:2015-10-12 05:04:59

标签: android android-timepicker

我在Android项目中使用时间选择器。但它显示8.1而不是8.01。我正在使用代码:

public class TimePickerFragment extends DialogFragment implements
        TimePickerDialog.OnTimeSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        return new TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view, int hour, int minute) {
        timerFlag = true;
        minuteP = minute;
        hourP = hour;
        flagData.setTimePicker(timerFlag);
        setTime(timePicker, hour, minute);

    }
}

2 个答案:

答案 0 :(得分:2)

你必须检查分钟是否小于10.Code在下面给出......

    String minuteFormatter = "";
    if (minute < 10) {
        minuteFormatter = "0" + minute;
    } else {
        minuteFormatter = String.valueOf(minute);
    }
    StringBuilder Time = new StringBuilder().append(hour).append(" : ")
            .append(minuteFormatter);

答案 1 :(得分:1)

  

的String.format( “%02D:%02D”,小时,分钟)