Discussions
Categories
- 385.5K All Categories
- 5.1K Data
- 2.5K Big Data Appliance
- 2.5K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 584 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 666 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
I cannot set the AM_PM Variable in my Calendar Object in ME.

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
-
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
-
Thank you so much for helping me out. This was driving me crazy!
-
Confirmed to be a bug. Will be fixed in next version of Java ME