TimePicker - how to set AM or PM?


TimePicker - how to set AM or PM?



I am new to android ,In my app I have a timepicker here I need to programmatically set the hour,minute,AM/PM values .



The value of the time is "11:11:AM" ,In this I have set the hour and time and it is working fine .but I don't know how to set the AM/PM values .



code:


if (!(mTime == null)) {
String timme = mTime;
String time = timme.split(":");
int hour = Integer.parseInt(time[0].trim());
int min = Integer.parseInt(time[1].trim());
String amPm = ((time[2].trim()));

mTimePicker.setIs24HourView(false);
mTimePicker.setHour(hour);
mTimePicker.setMinute(min);
}



Image:



enter image description here



It always shows "AM" even if the string has "PM".
Can anyone help me to fix this .





This might be helpful: stackoverflow.com/questions/11381985/…
– AMACB
Jun 29 at 16:44




3 Answers
3



Android automatically sets AM/PM based on time it is supplied in 24 hours format. To clarify:



You have to set TimePicker to 12 hour mode by calling


yourtimepicker.setIs24HourView(false)



and then supply time in 24 hour format using


yourtimepicker.setHour(22)



If i were you i would take string apart and check if last part was AM or PM and if it is PM i would add 12 to hour value, so if it was 11:11:PM i would get 23 hours, give it to android and let it take care of everything else.


11:11:PM



NOTE: this works for both spinner and clock mode





Thanks a lot you saved my day @JustHobby
– Zhu
Jun 29 at 17:16





No problem, any time.
– JustHobby
Jun 29 at 17:33



call like this ,


mTimePicker.setIs24HourView(false);



Learn more here





I already tried this but not worked @Gobu CSG
– Zhu
Jun 29 at 16:51





@Zhu can u share ur screen
– Gobu CSG
Jun 29 at 16:52





I have updated the image and It always shows AM even if the string has PM @GobuCSG
– Zhu
Jun 29 at 16:58





hey dude, just u pass 24 hrs format it will make it 12 hrs format that's all.
– Gobu CSG
Jun 29 at 17:01



The String = aTime is a 24 hour formatted string


String = aTime


24 hour


mTimePicker = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
String timeSet;
if (selectedHour > 12) {
selectedHour -= 12;
timeSet = "PM";
} else if (selectedHour == 0) {
selectedHour += 12;
timeSet = "AM";
} else if (selectedHour == 12)
timeSet = "PM";
else
timeSet = "AM";
String minutes = "";
if (selectedMinute < 10)
minutes = "0" + selectedMinute;
else
minutes = String.valueOf(selectedMinute);

String aTime = new StringBuilder().append(selectedHour).append(':')
.append(minutes).append(" ").append(timeSet).toString();

relatedWorkTime.setText(aTime);

}
}, hour, minute, false);
mTimePicker.setTitle("Select Time");
mTimePicker.show();






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift