You Are Here:

Community: Developer Discussion Boards

#1 Old Unhappy how to get current time of device in j2me? - 2008-05-22, 11:16

Join Date: Jan 2008
Posts: 272
manojkumar.m
Offline
Regular Contributor
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?
Reply With Quote

#2 Old Re: how to get current time of device in j2me? - 2008-05-22, 11:28

Join Date: Nov 2007
Posts: 2,029
Location: Rome, Italy
Send a message via MSN to jappit Send a message via Skype™ to jappit
jappit's Avatar
jappit
Offline
Forum Nokia Champion
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


www.jappit.com - mobile and web blog
Reply With Quote

#3 Old Re: how to get current time of device in j2me? - 2008-05-22, 12:06

Join Date: Jan 2008
Posts: 272
manojkumar.m
Offline
Regular Contributor
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?
Reply With Quote

#4 Old Re: how to get current time of device in j2me? - 2008-05-22, 12:10

Join Date: Nov 2007
Posts: 2,029
Location: Rome, Italy
Send a message via MSN to jappit Send a message via Skype™ to jappit
jappit's Avatar
jappit
Offline
Forum Nokia Champion
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


www.jappit.com - mobile and web blog
Reply With Quote

#5 Old Re: how to get current time of device in j2me? - 2008-05-22, 12:14

Join Date: Jan 2008
Posts: 272
manojkumar.m
Offline
Regular Contributor
CLDC 1.1 and MIDP 2.1
I am using only standard APIs.
Reply With Quote

#6 Old Re: how to get current time of device in j2me? - 2008-05-22, 12:19

Join Date: Nov 2007
Posts: 2,029
Location: Rome, Italy
Send a message via MSN to jappit Send a message via Skype™ to jappit
jappit's Avatar
jappit
Offline
Forum Nokia Champion
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


www.jappit.com - mobile and web blog
Reply With Quote

#7 Old Re: how to get current time of device in j2me? - 2008-05-22, 12:23

Join Date: Jan 2008
Posts: 272
manojkumar.m
Offline
Regular Contributor
Though I changed the emulator settings to MIDP 2.0, it still shows the same error message.
Reply With Quote

#8 Old Re: how to get current time of device in j2me? - 2008-05-22, 12:27

Join Date: Jan 2008
Posts: 272
manojkumar.m
Offline
Regular Contributor
Thankyou jappit,
now i changed the emulator settings as suggested you.
now it is installed on my devices.
once again thanks for quick response.
Reply With Quote

#9 Old Re: how to get current time of device in j2me? - 2008-05-22, 12:31

Join Date: Jan 2008
Posts: 272
manojkumar.m
Offline
Regular Contributor
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?
Reply With Quote

#10 Old Re: how to get current time of device in j2me? - 2008-05-22, 12:45

Join Date: Nov 2007
Posts: 2,029
Location: Rome, Italy
Send a message via MSN to jappit Send a message via Skype™ to jappit
jappit's Avatar
jappit
Offline
Forum Nokia Champion
Mumble... do both phones give you the wrong time? Or one only (which one)?


www.jappit.com - mobile and web blog
Reply With Quote

#11 Old Re: how to get current time of device in j2me? - 2008-05-22, 18:23

Join Date: Apr 2003
Posts: 6,408
Location: USA, CA
hartti's Avatar
hartti
Offline
Nokia Expert
Could be related
http://wiki.forum.nokia.com/index.ph...on_S60_MIDlets

Hartti
Reply With Quote

#12 Old Re: how to get current time of device in j2me? - 2008-05-25, 13:02

Join Date: Jan 2008
Posts: 272
manojkumar.m
Offline
Regular Contributor
Thank you very much Hartti for your quick response.
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

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

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
How to install a self-signed J2ME application in a mobile device? shreyasbellary Mobile Java Networking & Messaging & Security 17 2009-04-30 20:44
6230, I close the connector next time midlet is started device not found anymore olepogam Bluetooth Technology 3 2006-10-03 22:54
J2ME Polish Version 1.0 enough Mobile Java General 0 2004-07-27 02:10
Can J2ME access the images that save on the device binhtt Mobile Java General 1 2004-03-09 22:00

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia