How to set DateTime with current year using Apex in Salesforce?

How to set DateTime with current year using Apex in Salesforce?

Sample code:

/*
Constructs a DateTime from the specified date and time in the local time zone.
Date.today() returns the current date in the current user's time zone.
*/
Date dtToday = Date.today();
/* Setting October 1st of current Year */
Date myDate = Date.newInstance( dtToday.year(), 10, 1 );
Time myTime = Time.newInstance( 3, 3, 3, 0 );
DateTime objDT = DateTime.newInstance( myDate, myTime );
/* Using format() since teh objDT return value will be in GMT */
system.debug( 'Date Time is ' + objDT.format() );

Output:

Leave a Reply