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?
Use HOUR_OF_DAY in place of HOUR.
Replace it with: