Search code examples
calendarjava-me

I cannot set the AM_PM variable in Java 8 Micro Edition


I'm a teaching myself Java and am a beginner. As part of a larger project I am running code to determine, and then later set, the time on a Calendar object. I have scoured the internet for a way to set the AM_PM value but I cannot get it to work. Any suggestions would help.

package timethread;

import javax.microedition.midlet.MIDlet;
import java.util.Calendar;

/**
*
* @author Jaydawg
*/
public class TimeThread extends MIDlet {

@Override
public void startApp() {
    Calendar cal = Calendar.getInstance();
    System.out.println(cal.get(Calendar.AM_PM)); // Original value: it is '1' or PM
    cal.add(Calendar.AM_PM, 0); // Attempt #1
    System.out.println(cal.get(Calendar.AM_PM));

    cal.set(Calendar.AM_PM, 0); // Attempt #2
    System.out.println(cal.get(Calendar.AM_PM));

    cal.set(Calendar.AM_PM, Calendar.AM); //Attempt #3
    System.out.println(cal.get(Calendar.AM_PM));

    cal.add(Calendar.AM_PM, Calendar.AM); //Attempt #4
    System.out.println(cal.get(Calendar.AM_PM));

    int min = cal.get(Calendar.MINUTE);
    int sec = cal.get(Calendar.SECOND);
    int hour = cal.get(Calendar.HOUR);
    int AMPM = cal.get(Calendar.AM_PM);
    System.out.println(AMPM);
    String AMPMString = "AM";
    if(cal.get(Calendar.AM_PM)==1){
        AMPMString = "PM";
    }
    System.out.println("The time is " + hour + ":" + min + ":" + sec + " " + AMPMString);
}

My results were as follows:

1
1
1
1
1
1
The time is 1:20:42 PM

Solution

  • This was confirmed to be a bug by Oracle.

    https://community.oracle.com/thread/3728182?sr=inbox&ru=997534