From your code, it looks like you are using Date objects. Instead, I recommend using calendar objects, if possible. If necessary, you can get the Date object from the Calendar object using the Calendar getTime()
method.
If you have a Calendar object, you can do the following:
Calendar cal = ...; String ampm = DateUtils.getAMPMString(cal.get(Calendar.AM_PM));
DateUtils
is part of the Android platform and returns back to API level 3. getAMPMString()
returns a localized version of AM / PM. See docs on the Android developer site.
If you do not have a Calendar object available, you can create it from a date object:
Date date = ...; Calendar cal = Calendar.getInstance(); cal.setTime(date);
However, since the calendar is on the hard side, you are better off working with calendar objects directly, rather than creating and spoofing them.
source share