1

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. TimeSetListener 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?

svenkapudija
  • 5,128
  • 14
  • 68
  • 96

1 Answers1

3

Use HOUR_OF_DAY in place of HOUR.

c.set(Calendar.HOUR, hours);

Replace it with:

c.set(Calendar.HOUR_OF_DAY, hours);
chown
  • 51,908
  • 16
  • 134
  • 170
Neha
  • 46
  • 2