Srinivasan Srinivasan - 2 months ago 18
Java Question

java get date marker field(am/pm)

I need to get the field AM/PM in date object. How can i get it?

This is my code.

String startTime ="01:05 PM";
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");

Date st = sdf.parse(startTime);

Answer

You could use a Calendar.

Calendar cal = Calendar.getInstance();
cal.setTime(st);
if (cal.get(Calendar.AM_PM) == Calendar.PM) {
  ...
}

Make sure you don't have a time zone mismatch when you do this.