Possible Duplicate:
Displaying the Time in AM/PM format in android
I can get the time and turn it into string using:
public static final String TIME_FORMAT = "h:mm a";
SimpleDateFormat dateTimeFormat = new SimpleDateFormat(TIME_FORMAT);
String getstring = dateTimeFormat.format(when.gettime();
but how do i get am/pm from it and turn it into string beccause i need it?
There are two patterns that we can use in SimpleDateFormat to display time. Pattern “hh:mm aa” and “HH:mm aa”, here HH is used for 24 hour format without AM/PM and the hh is used for 12 hour format with AM/PM. aa – AM/PM marker. In this example we are displaying current date and time with AM/PM marker.
try this
Calendar now = Calendar.getInstance();
int a = now.get(Calendar.AM_PM);
if(a == Calendar.AM)
System.out.println("AM"+now.get(Calendar.HOUR));
This should work:
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener()
{
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
TextView datehid = (TextView)findViewById(R.id.timehidden);
if(hourOfDay>12)
{
datehid.setText(String.valueOf(hourOfDay-12)+ ":"+(String.valueOf(minute)+"pm"));
}
if(hourOfDay==12)
{
datehid.setText("12"+ ":"+(String.valueOf(minute)+"pm"));
}
if(hourOfDay<12)
{
datehid.setText(String.valueOf(hourOfDay)+ ":"+(String.valueOf(minute)+"am"));
}
}
};
Found on an earlier question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With