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.

DateクラスのparseとvalueOf

Posted at

salesforceに日付に対し、許容できる範囲があります。それは1700/1/1〜4000/12/31です。

文字列から日付に変換する時、許容範囲内の変換処理は上記の二つのメソッドが区別がないです。

範囲内の日付変換.cls
Date d1 = Date.parse('2000/1/1');
System.debug('parse = ' + d1); // parse = 2000-01-01 00:00:00
Date d2 = Date.valueOf('2000-1-1');
System.debug('valueOf = ' + d2); // valueOf = 2000-01-01 00:00:00

許容範囲外の変換処理は上記の二つのメソッドの挙動が違います。

範囲外の日付変換.cls
Date d1 = Date.parse('1699/1/1');
System.debug('parse = ' + d1); // System.TypeException: Invalid date: 1699/1/1
Date d2 = Date.valueOf('1699-1-1');
System.debug('valueOf = ' + d2); // valueOf = 1699-01-01 00:00:00

まとめ

salesforce許容範囲外の日付変換をする時、 Date.parseはExceptionが発生しますが、Date.valueOfは正しく変換されます。

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?