如何更改android时间选择器对话输出?

时间:2013-11-21 13:23:38

标签: android timepicker

我在我的应用中使用了时间选择器对话框,所以问题很简单。我希望时间格式为07:00 AM。这是我的代码,我输出为早上7点。 提前谢谢..

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

    // while dialog box is closed, below method is called.
    public void onTimeSet(TimePicker view, int hour, int minute) {

        mCalen.set(Calendar.HOUR_OF_DAY, hour);
        mCalen.set(Calendar.MINUTE, minute);

        int hour12format = mCalen.get(Calendar.HOUR);
        hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
        minute = mCalen.get(Calendar.MINUTE);
        ampm = mCalen.get(Calendar.AM_PM);
        String ampmStr = (ampm == 0) ? "AM" : "PM";
        // Set the Time String in Button
        if(flag==111)
             from.setText(hour12format + ":" + minute + " "+ ampmStr);
        else if(flag==222)
             to.setText(hour12format + ":" + minute + " "+ ampmStr);
    }
};

1 个答案:

答案 0 :(得分:0)

我的建议。

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

// while dialog box is closed, below method is called.
public void onTimeSet(TimePicker view, int hour, int minute) {

    mCalen.set(Calendar.HOUR_OF_DAY, hour);
    mCalen.set(Calendar.MINUTE, minute);

    int hour12format = mCalen.get(Calendar.HOUR);
    hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
    minute = mCalen.get(Calendar.MINUTE);
    ampm = mCalen.get(Calendar.AM_PM);
    String ampmStr = (ampm == 0) ? "AM" : "PM";

    //Format Date and/or Time see more: http://developer.android.com/reference/java/text/SimpleDateFormat.html
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aaa");
    String strDateFormat = "";
    try {
        strDateFormat = sdf.format(mCalen..getTime());
    } catch (Exception e) {
        // ...
    }

    // Set the Time String in Button
    if(flag==111)
         from.setText(strDateFormat);
    else if(flag==222)
         to.setText(strDateFormat);
}

};

编辑:

抱歉正确的面具“HH:mm aaa”