Dunkey Dunkey - 2 months ago 10
Android Question

Display 12hh format in Android TimePicker Dialog Fragment

I'm doing an android app where I have to choose a specific time.

This what I've done so far:

public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// use the current time as the default values for the picker
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);


// create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute, false
); // DateFormat.is24HourFormat(getActivity())
}

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
view.setIs24HourView(false);
String time;

if ( minute < 10 ) {
_minute = ":0" + minute;
} else {
_minute = ":" + minute;
}


Log.w("TimePicker", "Time = " + time);


EditText activityButton = (EditText)getActivity().findViewById(R.id.etTime);
activityButton.setText( time );


}


What I want to achieve is:

6:30 PM


But all I get is:

18:30


I want to 12 H format not the 24h. How can I do this? Help is much appreciated thanks.

Answer

Try changing

 int hour = c.get(Calendar.HOUR_OF_DAY);

to

int hour = c.get(Calendar.HOUR);

bcoz I see on

public static final int HOUR_OF_DAY

Added in API level 1

Field number for get and set indicating the hour of the day. HOUR_OF_DAY is used for the 24-hour clock. E.g., at 10:04:15.250 PM the HOUR_OF_DAY is 22.

http://developer.android.com/reference/java/util/Calendar.html#HOUR_OF_DAY

Comments