1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Salesforce】Apexの日付比較メソッドの使い方と注意点

Posted at

SalesforceのApexで日付を比較するには

DateクラスのisSameDayメソッドを使用する。

■Date クラス
https://developer.salesforce.com/docs/atlas.ja-jp.226.0.apexcode.meta/apexcode/apex_methods_system_date.htm

isSameDayメソッドの使い方

Date型の変数を2つ作成し、isSameDayメソッドをコールすれば比較結果が返ってくる。

Date date1 = Date.newInstance(2020, 1, 1);
Date date2 = Date.newInstance(2020, 1, 1);
Boolean result = date1.isSameDay(date2);
System.debug('result: ' + result);

isSameDayメソッドの引数にnullを指定してみた

isSameDayメソッドの引数にnullを指定するとどうなるか検証してみた。
結果はfalseになりそうだが、エラーであった。

Date date1 = Date.newInstance(2020, 1, 1);
Date date2 = null;
Boolean result = date1.isSameDay(date2);
System.debug('result: ' + result);

/* エラー内容
Line: 3, Column: 1
System.NullPointerException: Argument cannot be null.
*/

まとめ

isSameDayメソッドにnullを指定できないことが分かった。
処理によってはnullが引数になるパターンも発生しうるため、nullの場合はメソッドをコールしないように注意する必要がある。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?