Reputation: 3895
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:
It always shows "AM" even if the string has "PM". Can anyone help me to fix this .
Upvotes: 1
Views: 3848
Reputation: 1
Use below Code to add convert from 24-hour format to 12-hour format and add AM/PM.
String time = ((hourOfDay > 12) ? hourOfDay % 12 : hourOfDay) + ":" + (minute < 10 ? ("0" + minute) : minute) + " " + ((hourOfDay >= 12) ? "PM" : "AM");
Please find the entire code of android timepicker below.
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(CreateNewBatchActivity.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
String time = ((hourOfDay > 12) ? hourOfDay % 12 : hourOfDay) + ":" + (minute < 10 ? ("0" + minute) : minute) + " " + ((hourOfDay >= 12) ? "PM" : "AM");
buttonBatchStartTime.setText(time);
}
}, mHour, mMinute, false);
timePickerDialog.show();
Upvotes: 0
Reputation: 195
The String = aTime
is a 24 hour
formatted string
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();
Upvotes: 0
Reputation: 533
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.
NOTE: this works for both spinner and clock mode
Upvotes: 2