TimePickerDialog上的AM / PM DateFormat替换

时间:2011-08-29 16:58:46

标签: android datetimepicker date-format

我有一些布局/按钮和 onClick 我得到 TimePickerDialog 当前时间(之前从日历定义):

    RelativeLayout time = (RelativeLayout) findViewById(R.id.time_layout);
    time.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            TimePickerDialog dateDialog = new TimePickerDialog(SelectDateTimeActivity.this,
                    TimeSetListener,
                    hours, minutes, false);
            dateDialog.show();
        }
    });

TimePicker 处于 12h模式 - 它有 AM / PM 标记。 TimeSetListener 看起来像

private TimePickerDialog.OnTimeSetListener TimeSetListener = new TimePickerDialog.OnTimeSetListener() {
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        hours = hourOfDay;
        minutes = minute;
        updateTimeRow();
    }
};

updateTimeRow()

private void updateTimeRow() {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR, hours);
    c.set(Calendar.MINUTE, minutes);

    timeBody.setText("@ " + (String) (DateFormat.format("hh:mm aa", c.getTime())));
}

问题是在TimePickerDialog中设置的时间 06:00 PM (实际上是18:00,我通过记录检查)我在 06:00 AM timeBody textView。如果我在timeBody textView中将其设置为 06:00 AM (实际上是06:00),我会 06:00 PM 。为什么呢?

1 个答案:

答案 0 :(得分:3)

使用HOUR_OF_DAY代替HOUR。

c.set(Calendar.HOUR, hours);

将其替换为:

c.set(Calendar.HOUR_OF_DAY, hours);