24小时格式到12小时格式

时间:2013-12-17 08:29:58

标签: android time-format

我将24小时格式转换为12小时格式时遇到问题。这是我的代码。

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

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                // TODO Auto-generated method stub
                ViewGroup vg=(ViewGroup) view.getChildAt(0);
                hour = hourOfDay;
                minutes = minute;
                String am_pm = (hourOfDay < 12) ? "AM" : "PM";
                SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a",Locale.US);
                String tme = hour +":"+ minutes+am_pm;
                et1.setText(tme);
            }
        };

我从警告对话框切换到timepicker对话框,因为我不知道如何将24小时更改为12小时格式

et1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new TimePickerDialog(MainActivity.this, mTimeSetListener, hour, minutes, false).show();
            }
        });

    }  

这证明我已经将布尔印象变为假。

8 个答案:

答案 0 :(得分:14)

试试这个 -

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);

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

答案 1 :(得分:5)

我知道我太迟了但只有两行才能让它变得完美。

private TimePickerDialog.OnTimeSetListener timeSetListener = new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            boolean isPM = (hourOfDay >= 12);
            tvTime.setText(String.format("%02d:%02d %s", (hourOfDay == 12 || hourOfDay == 0) ? 12 : hourOfDay % 12, minute, isPM ? "PM" : "AM"));
        }
    };

答案 2 :(得分:3)

有一个构造函数参数

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

docs。格式化使用android.text.format.DateFormat

答案 3 :(得分:2)

这就是我这样做的方式。 Formats SimpleDateFormat需要https://developer.android.com/reference/java/text/SimpleDateFormat.html#format

TimePickerDialog timePickerDialog=  new TimePickerDialog(NotificationActivity.this,
    new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker timePicker, int hour, int minute) {

            Time time = new Time(hour, minute, 0);

            //little h uses 12 hour format and big H uses 24 hour format
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("h:mma");

            //format takes in a Date, and Time is a sublcass of Date
            String s = simpleDateFormat.format(time);
            toTime.setText(s);
        }
    }, hour, minute, false);

答案 4 :(得分:1)

为什么不使用

timePicker.setIs24HourView(false);

答案 5 :(得分:1)

请尝试使用此代码

  int Hour=0;
int Minute=0;
int new_hour=0;
 String new_minute=null;
TimePickerDialog.OnTimeSetListener time = new TimePickerDialog.OnTimeSetListener() {



public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
         Hour = hourOfDay;
         Minute = minute;

        String time_form = hourOfDay + ":" + minute + ":00";
        int r = minute / 10;
        if (r == 0) {
            new_minute = "0" + minute;
        } else {
            new_minute = minute + "";
        }
        if (Hour > 12) {
            new_hour = (hourOfDay - 12);
            edit_text.setText(new StringBuilder().append(new_hour)
                    .append(":").append(new_minute)
                    + " PM");
        } else if (Hour == 12) {

            new_hour = hourOfDay;
            edit_text.setText(new StringBuilder().append(new_hour)
                    .append(":").append(new_minute)
                    + " PM");
        }

        else {
            new_hour = hourOfDay;
            edit_text.setText(new StringBuilder().append(new_hour)
                    .append(":").append(new_minute)
                    + " AM");
        }

    }

};

答案 6 :(得分:0)

您可以使用接受答案中提到的相同转换逻辑 How to convert 24 hr format time in to 12 hr Format?

答案 7 :(得分:0)

这也将解决您的初始0问题

String time = "", md = " AM";
if(selectedHour == 0) selectedHour = 12;
else if(selectedHour > 12) {selectedHour -= 12; md = " PM";}
else if(selectedHour == 12) md = " PM";

if(selectedHour < 10) time = "0";
time += selectedHour;
time += ":";
if(selectedMinute < 10) time += "0";
time += selectedMinute;
time += md;
et_time.setText(time);

输出 例如:02:05 AM而不是2:5