Facing issue with Calendar object

I have a task in which i need to set hour, minute, meridian programmatically to Calendar object and need to display time in a format hh:mm a. Here below is my code so far.

Calendar calendar = (Calendar)dateNtime.clone();
        calendar.set(Calendar.HOUR, 12);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.AM_PM, 1);

SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm a");
    String str = dateFormat.format(calendar.getTimeInMillis());

Where dateNTime is an existing calendar object which i have to use in constructing new one.

All is going fine except only a case while i set 12PM. it always format hh:mm a and results 12:00AM while it should be 12:00PM.

please help if anybody have a good experience with Calendar object and it's known issue or provide me if there is a good tutorial link.

Jon Skeet
people
quotationmark

The HOUR field is documented as:

Field number for get and set indicating the hour of the morning or afternoon. HOUR is used for the 12-hour clock (0 - 11).

So instead of setting it to 12, you should set it to 0.

Personally I'd just set the HOUR_OF_DAY field, adding 12 hours if you want to make it PM - and don't set the AM_PM field at all.

people

See more on this question at Stackoverflow