You Are Here:

Community: Developer Discussion Boards

#1 Old J2me Date manipulation - 2008-10-06, 11:04

Join Date: Jun 2008
Posts: 62
remyag
Offline
Regular Contributor
Hi all,

In my application I displayed the current date.When I press right arrow key the date must be incremented by 1 and when I press left arrow the date must be decremented by 1.I couldn't find any method to increment and decrement date in Date ,DateField and Calendar class in j2me.

Is there any solution.
Please reply ...

Remyag
Reply With Quote

#2 Old Re: J2me Date manipulation - 2008-10-06, 11:11

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
Hi Remyag,

as you say, there are no direct methods to increment/decrement dates, but you can easily implement them by using both Date or Calendar instances.

When dealing with Date objects, only available method is the setTime(), that accepts as argument the date milliseconds. So, let's say you want to increment your date by 1 day, you'll do something like this:
Code:
myDate.setTime(myDate.getTime() + 86400000);
where 86400000 is the number of milliseconds of 1 day.

Dealing with Calendar instances is quite easier, since it has methods to directly modify single date fields. So, incrementing by 1 day is easy like this:
Code:
yourCalendar.set(Calendar.DAY_OF_MONTH, yourCalendar.get(Calendar.DAY_OF_MONTH) + 1);
Note that, when using Calendar, you should manually check if a field increment should cause other fields to change too (e.g.: incrementing by 1 day when you're already at the end of month, will cause also month to increment by 1)

Hope it helps,
Pit


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

#3 Old Re: J2me Date manipulation - 2008-10-06, 11:27

Join Date: Jun 2008
Posts: 62
remyag
Offline
Regular Contributor
Hi jappit,

Very Thanks for your reply...
I could solved my problem.
I used the code
myDate.setTime(myDate.getTime() + 86400000);

Thanks
by
Remyag
Reply With Quote

#4 Old Re: J2me Date manipulation - 2009-05-05, 04:56

Join Date: Oct 2008
Posts: 6
ocmolina
Offline
Registered User
Quote:
Originally Posted by jappit View Post
Hi Remyag,

as you say, there are no direct methods to increment/decrement dates, but you can easily implement them by using both Date or Calendar instances.

When dealing with Date objects, only available method is the setTime(), that accepts as argument the date milliseconds. So, let's say you want to increment your date by 1 day, you'll do something like this:
Code:
myDate.setTime(myDate.getTime() + 86400000);
where 86400000 is the number of milliseconds of 1 day.

Dealing with Calendar instances is quite easier, since it has methods to directly modify single date fields. So, incrementing by 1 day is easy like this:
Code:
yourCalendar.set(Calendar.DAY_OF_MONTH, yourCalendar.get(Calendar.DAY_OF_MONTH) + 1);
Note that, when using Calendar, you should manually check if a field increment should cause other fields to change too (e.g.: incrementing by 1 day when you're already at the end of month, will cause also month to increment by 1)

Hope it helps,
Pit
Hello, I used the 86400000 and it works when you add less than 25 days to the current date. date.setTime(date.getTime() + 25 * 86400000)
but if I need to add more than 25, like 28 for instance, results are not correct. It if your date is may 1, you will get april 9. Does anyone has a fix for this?
Reply With Quote

#5 Old Re: J2me Date manipulation - 2009-05-05, 06:32

Join Date: Oct 2008
Posts: 6
ocmolina
Offline
Registered User
I just solved my problem... It results that when doing 28*86400000 an overflow is produced. So I stored 86400000 in a long variable (long day = 86400000) and now 28*day does not produce overflow and it works properly.
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
Beginners Tutorial for J2ME j2meed Mobile Java General 1 2007-08-27 16:30
Date in J2ME alonish Mobile Java General 8 2007-08-07 22:07
How can the WAP browser communicate to J2ME application? hbfornies Mobile Java General 20 2007-03-02 17:32
Can J2ME be used for sending MMS Message ? seno_ap Mobile Java General 1 2006-11-24 15:36
need urgent guidance on j2me cogni_niru Mobile Java General 1 2005-11-13 16:02

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