function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RossGRossG 

add a year in apex

This is a ridiculously simple question and just involves date formatting.  Hoping one of the brains on here can figure this out before I do.

 

I just want to add 1 year to the current year.

 

So if I've got

 

DateTime dt=System.now();
String str=dt.format('yyyy');

 

I just want to add 1 year to that string, so I can refer to it in my code.  That's it.

 

This has got to be super easy I just am too dumb to see it yet.

 

Thanks guys

Devendra@SFDCDevendra@SFDC

Hi,

 

You can use addYears() method of DataTime.

 

DateTime newDate = System.Now().addYears(1);

 

DateTime Methods documentation:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_datetime.htm

 

Hope this helps :)

 

Thanks,

Devendra

 

 

Jerun JoseJerun Jose
Integer nextYear = system.today().year() + 1;
KrForceKrForce
Date dt= Date.newInstance(datefield.year()+1, datefield.month(), datefield.Day());