Search Here

Apex Date datatype

 In Apex, the Date data type is used to represent dates without a time component. It stores date values in the format YYYY-MM-DD. The Date data type is commonly used for date-related operations and calculations where time information is not required.

public class DateExample {

    public static void main() {

        Date today = Date.today();

        Date nextWeek = today.addDays(7);

        

        System.debug('Today: ' + today);

        System.debug('Next week: ' + nextWeek);

        

        // Comparing dates

        if (nextWeek > today) {

            System.debug('Next week is after today.');

        } else {

            System.debug('Next week is not after today.');

        }

    }

}


We declare a Date variable today and initialize it with the current date using the Date.today() method.


We use the addDays() method to calculate the date for next week and store it in the nextWeek variable.


We use System.debug() to print the values of today and nextWeek to the debug log.


We use an if statement to compare the dates. If nextWeek is after today, we print a message indicating that. Otherwise, we print a different message.




Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.