?
Solved

Calendar class in J2ME problems

Posted on 2007-07-31
7
Medium Priority
?
9,712 Views
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);
0
Comment
Question by:jonathan_otero
  • 4
  • 3
7 Comments
 
LVL 5

Expert Comment

by:qasitouch
ID: 19607228
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) {
        notifyDestroyed();
        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 =
              java.util.Calendar.getInstance(java.util.TimeZone.getDefault());

            public void itemStateChanged(Item item) {
                cal.setTime(((DateField)item).getDate());
                System.out.println("\nDate has changed");
            }
        };

        // register for events
        form.setItemStateListener(listener);
       
        // get today's date
        java.util.Date now = new java.util.Date();
        DateField dateItem = new DateField("Today's date:", DateField.DATE);
        dateItem.setDate(now);
        // add date to the Form screen
        form.append(dateItem);
        display.setCurrent(form);
    }
}



=---------------
Kind Regards,
qasitouch
0
 
LVL 5

Accepted Solution

by:
qasitouch earned 2000 total points
ID: 19607281
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() );
System.out.println(buf.toString());

it must work and ur problem is solved now

----------------
Kind Regards,
qasitouch.
0
 

Author Comment

by:jonathan_otero
ID: 19609732
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:qasitouch
ID: 19615497
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,
qasitouch.

0
 

Author Comment

by:jonathan_otero
ID: 19616469
Hi qasitouch:

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

Expert Comment

by:qasitouch
ID: 19623138
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,
qasitouch.
0
 

Author Comment

by:jonathan_otero
ID: 19625570
Hey
Thanks for the help. I am gonna try to run the program in a real device.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

840 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