Calendar class in J2ME problems

Posted on 2007-07-31
Last Modified: 2013-11-24
I have tried to use the following method;
calendar = Calendar.getInstance();

to use a calendar using the default time zone, but it doesn't work. For example it give me the 5:00pm when
it should be 1:00pm. Can someone tell me what is the problem ?

        calendar = Calendar.getInstance();
        calendar.setTime(new Date());

        // to convert the current date to an String
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DATE);
        date = (month<10? "0": "")+ month+"/"+(day<10? "0": "") + day + "/" + (year<10? "0": "")+year;

        // to convert the current time to an String
        String[] AMPM = {"AM", "PM"};
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);
        String am_pm = (AMPM[calendar.get(Calendar.AM_PM)]);
        time = (hour + ":" + (minute<10? "0": "") + minute + am_pm);
Question by:jonathan_otero
    LVL 5

    Expert Comment

    try this code, i m sure, your problem will be solved.

    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;

    public class EventEx2 extends MIDlet {
        Display display;

        public EventEx2() {
            display = Display.getDisplay(this);

        public void destroyApp (boolean unconditional) {
            System.out.println("App destroyed ");

        public void pauseApp () {
            display = null;
            System.out.println("App paused.");

        public void startApp () {
            Form form = new Form("Change Date");
            ItemStateListener listener = new ItemStateListener() {
                java.util.Calendar cal =

                public void itemStateChanged(Item item) {
                    System.out.println("\nDate has changed");

            // register for events
            // get today's date
            java.util.Date now = new java.util.Date();
            DateField dateItem = new DateField("Today's date:", DateField.DATE);
            // add date to the Form screen

    Kind Regards,
    LVL 5

    Accepted Solution

    as for i understand u want to get current time. for that u dont have to use calender.

    use code like this

    StringBuffer buf;
    buf.append( "Current time is  = " );
    buf.append( System.currentTimeMillis() );

    it must work and ur problem is solved now

    Kind Regards,

    Author Comment

    Hi gasitouch;

    I have tried both and I'm still getting the wrong time. I dont know what I am doing wrong I have tried to use the default time zone and everything and nothing works. Please let me know if you find something.
    LVL 5

    Expert Comment

    hmm, do you have checked the time zone of the mobile on which you are running this midlet??

    plz go n change the time-zone. u can change it through Settings of your phone.
    run the same midlet and change the time zone each time and see wht result u get.

     this will solve your problem for sure
    and one thing more if it also doesn't work there is an option "Auto time Zone", try that too.

    Kind Regards,


    Author Comment

    Hi qasitouch:

    I'm still running all this in the emulator. Dou you think this is a problem ?
    LVL 5

    Expert Comment

    yeh, i think, plz run and test it on some mobile. if you get problems with the time, chk the time zone etc. as i said before.

    Kind Regards,

    Author Comment

    Thanks for the help. I am gonna try to run the program in a real device.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    The viewer will learn how to implement Singleton Design Pattern in Java.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now