It's All Writing.

Writing makes you happy.

How to select sObject with date type condition in correct timezone at Apex ( and SOQL )

Environment

  • Apex

Code

For example I assume that you get a variable of two dates-type as follows already.

gist.github.com

Please refer to last entry for details of the FiscalYearCalculation.getLastFiscalYearPeriod method.

Now, we'll select opportunities which created in last fiscal year as below.

gist.github.com

String.valueOf(Date) method converts from a date to a correct date's format string which is usable in SOQL condition.
For setting correct timezone, You also aren't supposed to forget to use convertTimeZone.