×

Discussion Board

Results 1 to 12 of 12
  1. #1
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    272

    Unhappy how to get current time of device in j2me?

    Hello friends,

    I want to know the current time(hh:mm:ss) of device by writing simple midlet. I dont know how to proceed. could you please tell me how to achieve this?

  2. #2
    Nokia Developer Champion
    Join Date
    Nov 2007
    Location
    Rome, Italy
    Posts
    2,406

    Re: how to get current time of device in j2me?

    This code should work for you:
    Code:
    Date d = new Date();
    		
    Calendar c = Calendar.getInstance();
    		
    c.setTime(d);
    		
    String time = c.get(Calendar.HOUR_OF_DAY) + ":" + 
    	c.get(Calendar.MINUTE) + ":" + 
    	c.get(Calendar.SECOND);
    If you want 12h-based hour, you can get it with c.get(Calendar.HOUR) and check if is AM/PM with the Calendar.AM_PM property.

    Pit

  3. #3
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    272

    Re: how to get current time of device in j2me?

    Hi jappit,
    thanks for your reply, but when I executed the application as you suggestd, its working fine on emulator, but when istalled on Nokia E61i and Nokia 6300, it is not installing and showing an error message : application not compatible with this phone.
    my code is :

    public class DateAndTime extends MIDlet implements CommandListener
    {
    Display disp;

    Form form = new Form("Data and Time");
    Command start = new Command("start", Command.SCREEN, 1);
    Command exit = new Command("Exit", Command.EXIT, 0);
    Date d ;
    Calendar c = Calendar.getInstance();
    String time;
    public void startApp()
    {
    form.append("TIME IS : ");
    form.addCommand(start);
    form.addCommand(exit);
    form.setCommandListener(this);
    disp = Display.getDisplay(this);
    disp.setCurrent(form);
    }

    public void pauseApp()
    {
    }

    public void destroyApp(boolean uncond)
    {
    }

    public void commandAction(Command cmd, Displayable s)
    {
    if (cmd == exit)
    {
    notifyDestroyed();
    }
    else
    if (cmd == start)
    {
    d = new Date();
    c.setTime(d);
    time = c.get(Calendar.HOUR_OF_DAY) + ":" +c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND);
    form.append("" + time);
    form.append("\n");
    }
    }
    }

    what is the problem?

  4. #4
    Nokia Developer Champion
    Join Date
    Nov 2007
    Location
    Rome, Italy
    Posts
    2,406

    Re: how to get current time of device in j2me?

    Which CLDC and MIDP versions are you using? Also, are you using any non-standard APIs (even if it seems you're not from your code)?

    Pit

  5. #5
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    272

    Re: how to get current time of device in j2me?

    CLDC 1.1 and MIDP 2.1
    I am using only standard APIs.

  6. #6
    Nokia Developer Champion
    Join Date
    Nov 2007
    Location
    Rome, Italy
    Posts
    2,406

    Re: how to get current time of device in j2me?

    Both Nokia E61i and 6300 support MIDP 2.0, not MIDP 2.1, so you should change that setting. To be sure about phone specifications and supported APIs, always check here:

    http://www.forum.nokia.com/devices/matrix_all_1.html

    Pit

  7. #7
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    272

    Re: how to get current time of device in j2me?

    Though I changed the emulator settings to MIDP 2.0, it still shows the same error message.

  8. #8
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    272

    Re: how to get current time of device in j2me?

    Thankyou jappit,
    now i changed the emulator settings as suggested you.
    now it is installed on my devices.
    once again thanks for quick response.

  9. #9
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    272

    Re: how to get current time of device in j2me?

    One more doubt, when I started the midlet on my mobile, the current time is 15:59:1.

    and the midlet shows the current time as 15:29:01.

    I dont know why there is a difference of 30 minutes.

    what might be the problem? any suggestions?

  10. #10
    Nokia Developer Champion
    Join Date
    Nov 2007
    Location
    Rome, Italy
    Posts
    2,406

    Re: how to get current time of device in j2me?

    Mumble... do both phones give you the wrong time? Or one only (which one)?

  11. #11
    Super Contributor
    Join Date
    Apr 2003
    Location
    USA, CA
    Posts
    7,191

    Re: how to get current time of device in j2me?


  12. #12
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    272

    Re: how to get current time of device in j2me?

    Thank you very much Hartti for your quick response.

Similar Threads

  1. How to install a self-signed J2ME application in a mobile device?
    By shreyasbellary in forum Mobile Java Networking & Messaging & Security
    Replies: 17
    Last Post: 2009-04-30, 19:44
  2. Replies: 3
    Last Post: 2006-10-03, 21:54
  3. J2ME Polish Version 1.0
    By enough in forum Mobile Java General
    Replies: 0
    Last Post: 2004-07-27, 01:10
  4. Can J2ME access the images that save on the device
    By binhtt in forum Mobile Java General
    Replies: 1
    Last Post: 2004-03-09, 21:00

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×