开发者

AM/PM DateFormat substitution at TimePickerDialog

I have some layout/button and onClick I get TimePickerDialog with current time (previously defined from Calendar):

    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 is in 12h mode - it has AM/PM mark. T开发者_StackOverflowimeSetListener looks like

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

and updateTimeRow() is

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

The problem is that time set in TimePickerDialog 06:00 PM (which really is 18:00, I checked via logging) I get 06:00 AM in my timeBody textView. If I set it to 06:00 AM (which really is 06:00) in timeBody textView I get 06:00 PM. Why?


Use HOUR_OF_DAY in place of HOUR.

c.set(Calendar.HOUR, hours);

Replace it with:

c.set(Calendar.HOUR_OF_DAY, hours);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜