On January 27th, this site will be read-only as we migrate to Oracle Forums for an improved community experience. You will not be able to initiate activity until January 30th, when you will be able to use this site as normal.

    Forum Stats

  • 3,889,626 Users
  • 2,269,768 Discussions
  • 7,916,798 Comments

Discussions

I cannot set the AM_PM Variable in my Calendar Object in ME.

2956737
2956737 Member Posts: 5
edited Jun 4, 2015 9:55AM in Java ME Embedded

I'm trying to determine the time and then later set the time on a Calendar object. I have tried everything I can think of and have scoured the internet but I cannot get it to work. My code is below.

I'm trying to switch the AM_PM variable in a variety of ways and I'm having trouble. No matter which way I try and set it, I cannot get it to change.

Someone on StackOverflow suggest that this may be a bug? What am I missing here?

    public void startApp() {

        Calendar cal = Calendar.getInstance();

        System.out.println(cal.get(Calendar.AM_PM)); // Original value: it is 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));

      

        cal.roll(Calendar.AM, true); //Attempt #5

        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);

    }

This is my output:

    1

    1

    1

    1

    1

    1

    1

    The time is 4:24:56 PM

Any ideas would be greatly appreciated!

Answers

  • Andrey.P-Oracle
    Andrey.P-Oracle Member Posts: 157
    edited Jun 2, 2015 1:34PM

    Hi!

    Pardon for taking so long to respond.

    Indeed I as well lean towards thinking it's a bug. Indeed there might be reasons for this behavior so I need more time to investigate. Anyway, the problem is that the AM_PM field is only taken in account when you have set the HOUR field as well (and have not set HOUR_OF_DAY BTW). So the following code works:

            cal.set(Calendar.HOUR, cal.get(Calendar.HOUR));

            cal.set(Calendar.AM_PM, 0); // Attempt #2

            System.out.println(cal.get(Calendar.AM_PM));

    So may I hope this unblocks you in whatever you're doing? However for the sake of clarity of the behavior I'll investigate further

    Regards,

    Andrey

  • 2956737
    2956737 Member Posts: 5
    edited Jun 3, 2015 8:39PM

    Thank you so much for helping me out. This was driving me crazy!

  • Andrey.P-Oracle
    Andrey.P-Oracle Member Posts: 157
    edited Jun 4, 2015 9:55AM

    Confirmed to be a bug. Will be fixed in next version of Java ME

This discussion has been closed.