Salesforce apex date methods with practice examples
Date is one of the primitive data type in salesforce. Mastering how to handle dates is key, as they're the building blocks for various data manipulation.
In this guide we will dives into date manipulation in salesforce apex, explaining essential date class and methods and showing you how to use them in real-world situations.
Key topics
How to write a date in Apex
In Salesforce apex, dates are represented by the Date class. You can create a date using the Date.newInstance method or by parsing a date string.
Here is an example :
Date myDate = Date.newInstance(2024, 8, 7);
System.debug('Date: ' + myDate); // Date: 2024-08-07 00:00:00
What are the apex date methods
Salesforce apex offers a robust set of date manipulation methods. These methods help developers efficiently work with dates within their apex code.
The following are some of the most important date methods available in Salesforce.
1. addDays
This function Adds a specified number of days to the date.
Date originalDate = Date.newInstance(2024, 8, 7);
Date newDate = originalDate.addDays(10);
System.debug('New Date: ' + newDate); // New Date: 2024-08-17 00:00:00
2. addMonths
This function adds a specified number of months to a given date date.
Date originalDate = Date.newInstance(2024, 8, 7);
Date newDate = originalDate.addMonths(2);
System.debug('New Date: ' + newDate); // New Date: 2024-10-07 00:00:00
3. addYears
This function Adds a specified number of a given date date.
Date mainDate = Date originalDate = Date.newInstance(2024, 8, 7);
Date newDate = originalDate.addYears(1);
System.debug('New Date: ' + newDate); // New Date: 2025-08-07 00:00:00
4. day
This function Returns the day of month component of a date.
Date testDate = Date.newInstance(2024, 8, 7)
Integer day = testDate.day();
System.debug('Day: ' + Day); // Day : 7
5. dayOfYear
This function Returns the day of year component of a date.
Date testDate = date.newInstance(2024, 08, 07);
Integer day = testDate.dayOfYear();
System.debug('Day: ' + Day); // Day : 220
6. daysBetween
Use this function to find the number of days between two dates.
Date startDate = Date.newInstance(2024, 8, 7);
Date endDate = Date.newInstance(2024, 9, 7);
Integer daysBetween = startDate.daysBetween(endDate);
System.debug('Days Between: ' + daysBetween); // Days Between: 31
7. format
This function Returns the date as a string using the default date format.
Date testDate = Date.newInstance(2024, 8, 7);
String formattedDate = testDate.format();
System.debug('Formatted Date: ' + formattedDate); // Formatted Date: 2024-08-07 00:00:00
8. isLeapYear
This function Returns true if the specified year is a leap year.
Date testDate = Date.newInstance(2024, 8, 7);
Boolean isLeap = Date.isLeapYear(testDate.year());
System.debug('Is Leap Year: ' + isLeap); // Is Leap Year: true
9. month
This function returns the month component of the date.
Date testDate = Date.newInstance(2024, 8, 7);
Integer month = testDate.month();
System.debug('Month: ' + month); // Month: 8
10. year
This function returns the year from a given date.
Date testDate = Date.newInstance(2024, 8, 7);
Integer year = testDate.year();
System.debug('Year: ' + year); // Year: 2024
11. toStartOfMonth
This function returns the first day of the month.
Date testDate = Date.newInstance(2024, 8, 7);
Date startOfMonth = testDate.toStartOfMonth();
System.debug('Start of Month: ' + startOfMonth); // Start of Month: 2024-08-01 00:00:00
12. toEndOfMonth
This function Returns a new date that is the last day of the month.
Date testDate = Date.newInstance(2024, 8, 7);
Date endOfMonth = testDate.toEndOfMonth();
System.debug('End of Month: ' + endOfMonth); // End of Month: 2024-08-31 00:00:00
13. valueOf
This function returns a date that contains the value of the specified string.
String month = '10';
String day = '5';
String hour = '12';
String minute = '20';
String second = '20';
String stringDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
Date testDate = Date.valueOf(stringDate);
System.debug('Date: ' + testDate); // Outputs: Date: 2024-10-05 12:20:20
14. parse
Constructs a Date from a String. The format of the String depends on the local date format.
String inputDate = '07/08/2024';
date outputDate = date.parse(inputDate);
system.debug('The date is '+outputDate); // The date is 2024-08-07 00:00:00
15. newInstance
Constructs a date from integer representations of the year, month, and day.
Date sampleDate = date.newinstance(2024, 8, 07);
System.debug('The date is '+sampleDate); // The date is 2024-08-07 00:00:00
Apex date practice examples
Following are a list of practice examples to learn and use the apex date methods. Click the following button to see all apex practice problems.
Visit all practice problemsCalculate the age from date of birth
Write an apex method to calculate the age based on date of birth. The given apex method accepts age, a date input parameter. Find the age for a person based on date of birth. Solve this practice example here.
Apex method to determine if the policy is within the waiting period
Practice using Apex date methods to determine if the policy is within the waiting period, given the policy start date. Solve this practice example here.
Determine subscription end date from today's date
Apex date practice example to determine the subscription end date from today based on the advance payments. Solve this practice example here.
Determine the number of days remaining to complete probation
Apex date practice example to determine the number of days remaining to complete an employee's probation period based on a given start date. Solve this practice example here.
Find the payment date for processing the employee's payroll
Practice an Apex date example to determine the payment date for processing employee payroll.Solve this practice example here.
Determine contract renewal notice date based on the start date
Practice an Apex example to determine the renewal notice date for sending contract renewal reminders to clients. Solve this practice example here.