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
Nishit MistryyyNishit Mistryyy 

Test class for date

I have a code where I am getting the year, month and date from the date which is passed as a string, i need to write the test class for it. 
Here is the class:
public class DateHelper {
    
    /*
        Integer year_;
        Integer month_;
        Integer day_;
    */
    
    public static datetime Get_Date_FromYYYYMMDD(String DateString)
    {
        Integer year =  integer.valueof(DateString.substring(0, 4));
        Integer month = integer.valueof(DateString.substring(5, 7));
        Integer day = integer.valueof(DateString.substring(8, 10));
        
        return Date.newInstance(year, month, day);
    }
    
    /*
    public date GetFromYYYYMMDD_P
    {
        get
        {
            
            return Date.today();
        }
    }*/

}
Best Answer chosen by Nishit Mistryyy
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Nishanth,

I hope it is just to change Date String to Datetime.

Please use below test class. It covers 100% of code.
 
@istest
public class DateHelperTest {
static testmethod void dateTest(){
    
    String Datestr='2022-01-22';
    datetime mt=DateHelper.Get_Date_FromYYYYMMDD(Datestr);

}
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,