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
Michael Kolodner 13Michael Kolodner 13 

Get next September 1 as a Date?

How can I take Today() and then get back, for example, the  next September 1 that will come?
Basically, if Today() returns 8/10/2019, I want to replace the month value with "9" and the day value with "1" but I don't know how to write it so that Apex will accept my manipulated XX/XX/XXXX as a date.
Best Answer chosen by Michael Kolodner 13
Maharajan CMaharajan C
Hi Micheal,

Please use the below code:

Date today = system.Today();
Date newDate = date.newinstance(today.year(), 9, 1);
system.debug('@@@ newDate ==> '+ newDate );

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C

All Answers

Alain CabonAlain Cabon
You want to parse: 9/1/2019 with Apex ?

date mydate1 = date.parse('12/27/2009');
date mydate2 = date.parse('9/1/2009');
date mydate3 =  date.parse('09/01/2009');
​​​​
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm

 
Maharajan CMaharajan C
Hi Micheal,

Please use the below code:

Date today = system.Today();
Date newDate = date.newinstance(today.year(), 9, 1);
system.debug('@@@ newDate ==> '+ newDate );

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C
This was selected as the best answer
Michael Kolodner 13Michael Kolodner 13
Thanks! With that hint to go on, I've got this working:
public class testDateSetter {
    
    // Gives September 1 of the current FY for use as a start date on Fall programs.
    public static date giveFallStart() {
        date septOne;
        if (System.today().Month() >= 7) {
            septOne = date.newinstance(Date.Today().Year(), 9, 1);
        }
        else {
            septOne = date.newinstance(Date.Today().Year(), 9, 1)-365;
        }
		return septOne;
    }