我无法在Java 8 Micro Edition中设置AM_PM变量

时间:2015-05-29 20:32:34

标签: calendar java-me

我是自学Java而且是初学者。作为一个更大的项目的一部分,我运行代码来确定,然后设置Calendar对象上的时间。我已经在互联网上搜索了一个设置AM_PM值的方法,但我无法让它工作。任何建议都会有帮助。

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

我的结果如下:

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

2 个答案:

答案 0 :(得分:0)

可能是Java 8 Micro Edition中的Calendar有错误吗?

这适用于Java 7:

Calendar c = Calendar.getInstance();
c.set(Calendar.AM_PM, Calendar.AM);
System.out.println(c.get(Calendar.AM_PM));
c.set(Calendar.AM_PM, Calendar.PM);
System.out.println(c.get(Calendar.AM_PM));

输出:

0
1

答案 1 :(得分:0)