Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 16
Registered: ‎09-04-2008
My Device: Not Specified

Subtracting and Adding Days Date.

[ Edited ]

Hi! I'm a C# programmer who's new to Java and I'm trying to subtract x numbers of days to a specific date. How can I do this in Java? Thank you!

 

- Ryan

Message Edited by Raime910 on 09-28-2008 04:48 PM
Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Subtracting and Adding Days Date.

Take a look at the API docs for Calendar, Date, and also look at System.currentTimeMilis()

 

 

 

New Developer
Posts: 16
Registered: ‎09-04-2008
My Device: Not Specified

Re: Subtracting and Adding Days Date.

[ Edited ]

Thanks RexDoug.

 

Another problem arises.

 

Calendar c = Calendar.getInstance();
c.set(Calendar.MONTH, mo);
c.set(Calendar.DAY_OF_MONTH, day);
c.set(Calendar.YEAR, year);
c.add(Calendar.DATE, -50);

  ^

cannot find symbol

symbol : method(int,int)


I was trying to subtract 50 days from the specified day and the .add method isn't working. Am I missing something? Thanks!

 

- Ryan

 

Message Edited by Raime910 on 09-28-2008 06:34 PM
Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Subtracting and Adding Days Date.

Can't find the Calendar.add method in the API doc, so doesn't look like that is supported.

 

One way to achieve what you want that immediately comes to mind is as follows, however you may need to be aware that this is subtracting 50*24 hours from the time, and if the 50 days included Daylight saving change, then the 50 days might more accurately contain 50*24+1 or 50*24-1 hours.

 

Calendar c = Calendar.getInstance(); c.set(Calendar.MONTH, mo); c.set(Calendar.DAY_OF_MONTH, day); c.set(Calendar.YEAR, year); long ctime = c.getTime().getTime(); long ctimeMinus50Days = ctime - 50L * ((long)DatetimeUtilities.ONEDAY); // not usre the 'longs are needed c.setTime(new Date(ctimeMinus50Days);

 

 

New Developer
Posts: 16
Registered: ‎09-04-2008
My Device: Not Specified

Re: Subtracting and Adding Days Date.

Thank you so much Peter. I'll try that.
Developer
Posts: 455
Registered: ‎01-14-2009
My Device: Apple iPhone 3GS 16GB

Re: Subtracting and Adding Days Date.

Excellent peter..Just what i wanted. Thanks
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Subtracting and Adding Days Date.

Can you mark this as solved then? 

 

Did you note my warning about Daylight Saving?

Developer
Posts: 455
Registered: ‎01-14-2009
My Device: Apple iPhone 3GS 16GB

Re: Subtracting and Adding Days Date.

Hello peter,,i didnt start this thread so i can't mark it as solved.. I wish i could Man Happy