Hello I’d like to convert 24 hr to 12 hr in Android. The time comes from user input in the timepicker dialog, then display it in a textview. If the user clicks on the PM button in the picker dialog, it should automatically display that in a textview. For examaple, the user clicks on 3:00 AM, it should display in the textview as 3:00 AM, while another user clicks on 3:00 PM, it should display in the textview 3:00 PM not 15:00. Any help please?
Please do check the code I have below:
public void onTimeSet( TimePicker view, int selectedHour,
int selectedMinute ) {
// Gets current time
Calendar c = Calendar.getInstance();
hour = selectedHour;
minute = selectedMinute;
// set waking time into textview
String getWT = wakingtime.getText().toString();
wakingtime.setText( new StringBuilder().append( pad( hour ))
.append( ":" ).append( pad( minute )));
// Assign hour set in the picker
c.set( Calendar.HOUR, selectedHour );
c.set( Calendar.MINUTE, selectedMinute );
// Have Calendar calculate the substraction of hours and minutes
c.add( Calendar.HOUR, SUB_HOUR );
c.add( Calendar.MINUTE, SUB_MINUTE );
// Get the hour and the minute calculated
hour = c.get( Calendar.HOUR );
minute = c.get( Calendar.MINUTE );
I beg your pardon if you don’t see any formatting..
Changue this:
By this: