12小时格式timepicker android

时间:2017-08-24 17:46:22

标签: android datetimepicker timepicker

我有一个时间戳工作正常。但它是24小时格式。我希望它是12小时格式。我无法找到可能的解决方案。我目前的代码段是:

Calendar mcurrentTime = Calendar.getInstance();
            final int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
            int minute = mcurrentTime.get(Calendar.MINUTE);
            TimePickerDialog mTimePicker;
            mTimePicker = new TimePickerDialog(AddJob.this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                    ////
                }
            }, hour, minute, true);
            mTimePicker.setTitle("Select Time");
            mTimePicker.show();

提前致谢。

编辑: 我可以在选择日期后检查子午线。但我希望用户选择子午线。

更新:如何将android问题与javascript问题重复?有人对此投反对票。!

3 个答案:

答案 0 :(得分:3)

更改TimePickerDialog来电:

您的代码是:

mTimePicker = new TimePickerDialog(AddJob.this,
          new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker,
                       int selectedHour, int selectedMinute) {
                    ////
                }
            }, hour, minute, true);

将您的上一个参数从 true 更改为 false

因为TimePickerDialog构造函数语法是:

TimePickerDialog(Context context,
        TimePickerDialog.OnTimeSetListener listener,
        int hourOfDay,
        int minute,
        boolean is24HourView)

注意:最后一个参数是 boolean is24HourView 因此,将其更改为false,您的问题将得到解决

有关详细信息,请查看TimePickerDialog Documentation

希望它有所帮助!!!

答案 1 :(得分:1)

private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // TODO Auto-generated method stub
        hour = hourOfDay;
        minutes = minute;
        String timeSet = "";
        if (hour > 12) {
          hour -= 12;
          timeSet = "PM";
        } else if (hour == 0) {
          hour += 12;
          timeSet = "AM";
        } else if (hour == 12){
          timeSet = "PM";
        }else{
          timeSet = "AM";
        }

        String min = "";
        if (minutes < 10)
         min = "0" + minutes ;
        else
         min = String.valueOf(minutes);

        String mTime = new StringBuilder().append(hour).append(':')
        .append(min ).append(" ").append(timeSet).toString();

    }
};

答案 2 :(得分:0)

你必须这样做,试试这个。

private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // TODO Auto-generated method stub
        hour = hourOfDay;
        minutes = minute;
        String timeSet = "";
        if (hour > 12) {
          hour -= 12;
          timeSet = "PM";
        } else if (hour == 0) {
          hour += 12;
          timeSet = "AM";
        } else if (hour == 12){
          timeSet = "PM";
        }else{
          timeSet = "AM";
        }

        String min = "";
        if (minutes < 10)
         min = "0" + minutes ;
        else
         min = String.valueOf(minutes);

        String aTime = new StringBuilder().append(hour).append(':')
        .append(min ).append(" ").append(timeSet).toString();
        et1.setText(aTime);
    }
};