I have a TextView in which a time is being show in the format 10:00 PM
When user Click on that View, same time must be set to the TimePicker
Dialog. I am able to set the Hour
and Minutes
to the Picker, but in AM/PM
section. I always get AM
whether user has set AM
or PM
. How can it be solved? any help is appreciated. Thanks!
Asked
Active
Viewed 4,599 times
2

Usama Sarwar
- 8,922
- 7
- 54
- 80
5 Answers
3
I had Face this Type of Similar issue.
it May Be because you might have set your hour
element as Below Way
hour = String.valueOf(cal.get(Calendar.HOUR));
Try Change it to
hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
Hope it Will Help you.

Bhavesh Patadiya
- 25,740
- 15
- 81
- 107
-
@Usama Sarwar: if the Answer solve your problem then mark it as accepted answer so that other can trust it and use it. – Bhavesh Patadiya Apr 13 '13 at 04:33
0
private Calendar calendar_date=null;
private TimePicker timePicker;
calendar_date = Calendar.getInstance();
timePicker= (TimePicker)dialog.findViewById(R.id.timePicker1);
timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker picker, int hour, int minute) {
calendar_date.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), hour, minute);
Log.i("TIME", getHourIn12Format(hour)+":"+minute+" "+getAMPM(calendar_date));
}
});
private int getHourIn12Format(int hour24) {
int hourIn12Format = 0;
if(hour24==0)
hourIn12Format = 12;
else if(hour24<=12)
hourIn12Format = hour24;
else
hourIn12Format = hour24-12;
return hourIn12Format;
}
private String getAMPM(Calendar calendar) {
return (calendar.get(Calendar.AM_PM)==(Calendar.AM))? "AM":"PM";
}

Claud Kho
- 426
- 4
- 12
0
You first provide the data into the timepicker by 24 hours format. later set
public void setIs24HourView (false);
That will do for you..

Jai Kumar
- 920
- 1
- 7
- 14
0
Try this, i hope i will solve your issue
Date date = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat("MMM,dd,yyyy hh:mm a");
String mDay_time = formatter.format(date);
For more Time Pattern Syntax

MuraliGanesan
- 3,233
- 2
- 16
- 22
0
//String selectedtime ="03:15 PM"
//convert time 12 hrs to 24 hrs
SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");
SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a");
Date date = null;
try {
date = parseFormat.parse(selectedtime);
System.out.println(parseFormat.format(date) + " = " + displayFormat.format(date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String twentyfourhrstime=displayFormat.format(date);
//split hrs and min
String[] parts111 = twentyfourhrstime.split(":");
String part11 = parts111[0]; // 004
String part22 = parts111[1];
System.out.println("value11>>>>"+part11);
System.out.println("value22>>>>"+part22);
hour = Integer.parseInt(part11);
min =Integer.parseInt(part22);
and set hour and min value to timepicker..