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 

Method that can accept a date as parameter?

I'm trying to write a method that will take a date and give the fiscal year it's in. Here's my code, which compiles:
public class dateTest {

    public static string giveFY(date givenDate) {
    system.debug('givendate is ' + givenDate);    
    integer currentFiscalOrSchoolYear ;
        if (givenDate.Month() >= 7){
            currentFiscalOrSchoolYear = givenDate.Year() + 1;
        }
        else {
            currentFiscalOrSchoolYear = givenDate.Year();
        }
        system.debug('the month of givenDate is: ' + currentFiscalOrSchoolYear);
        //Get the last two digits
        string fy = (string.valueof(currentFiscalOrSchoolYear).right(2));
        system.debug('fy to be returned: ' + fy);
        return fy;
    }
	
}

But when I try to use this Execute Anonymous, I get an error "   Line: 2, Column: 16 Unexpected token '('.   "
system.debug('putting 2018-04-11 into dateTest');
dateTest.giveFY(2018-04-11);
system.debug('FY is: ' + fy);

system.debug('putting 2019-04-11 into dateTest');
dateTest.giveFY(2019-04-11);
system.debug('FY is: ' + fy);

 
Best Answer chosen by Michael Kolodner 13
Raj VakatiRaj Vakati
My mistake.. i am sorry.. try this code please
 
system.debug('putting 2018-04-11 into dateTest');
String dateFY18 = dateTest.giveFY(Date.newinstance(2018, 04, 11));
system.debug('FY is: ' + dateFY18);

system.debug('putting 2019-04-11 into dateTest');
String dateFY19 = dateTest.giveFY(Date.newinstance(2019,04,11));
system.debug('FY is: ' + dateFY19);

 

All Answers

Raj VakatiRaj Vakati
Try like this

 
system.debug('putting 2018-04-11 into dateTest');
dateTest.giveFY(Date.newinstance(2018, 04, 11););
system.debug('FY is: ' + fy);

system.debug('putting 2019-04-11 into dateTest');
dateTest.giveFY(Date.newinstance(2019,04,11));
system.debug('FY is: ' + fy);

 
Michael Kolodner 13Michael Kolodner 13
Same error.

Meanwhile, a question. I understand what you're doing with the Date.newinstance(). But is there any way to actually just type a data and hand it to a method?
Raj VakatiRaj Vakati
My mistake.. i am sorry.. try this code please
 
system.debug('putting 2018-04-11 into dateTest');
String dateFY18 = dateTest.giveFY(Date.newinstance(2018, 04, 11));
system.debug('FY is: ' + dateFY18);

system.debug('putting 2019-04-11 into dateTest');
String dateFY19 = dateTest.giveFY(Date.newinstance(2019,04,11));
system.debug('FY is: ' + dateFY19);

 
This was selected as the best answer
Michael Kolodner 13Michael Kolodner 13
Thank you!