なぜこのステートメントがtrueを返しますか?私はDate
とDateTime
の変数を比較していることを理解していますが、私はより技術的な説明を探しています。日時と日付の比較
DateTime dt = DateTime.newInstance(2012,04,30,0,0,0);
system.debug(dt > Date.valueOf('2012-04-30'));
また、2012年4月30日の前に(DT変数の)のDateTime値もtrueを返すでしょうか?
なぜこのステートメントがtrueを返しますか?私はDate
とDateTime
の変数を比較していることを理解していますが、私はより技術的な説明を探しています。日時と日付の比較
DateTime dt = DateTime.newInstance(2012,04,30,0,0,0);
system.debug(dt > Date.valueOf('2012-04-30'));
また、2012年4月30日の前に(DT変数の)のDateTime値もtrueを返すでしょうか?
Superfell
は、これはおそらく、比較のために日に付加されるタイムゾーンの変換ならびに真夜中(00:00:00
)によるものであることをコメントで指摘しました。短編小説:比較のために、真夜中が日付に追加されているため、彼はそれについて正しいものでした。詳細は以下の通りです。
これを完全に理解するには、実際にこれを確認する必要がありました。だから、私はコードのブロックを書きました。
for(Integer mo=4; mo<6; mo++) // test April (4) and May (5) only
for(Integer d=(mo==4?29:0); d<=(mo==5?2:30); d++) // test between 2012-04-29 and 2012-05-30
for(Integer h=0; h<12; h++)
for(Integer m=0; m<60; m++)
{
// this simulates a CreatedDate field (note it is generated using the newInstanceGMT method and not simply newInstance)
// DateTime fields in Salesforce are stored in GMT then converted to the current User's time-zone.
DateTime dt1 = DateTime.newInstanceGMT(2012,mo,d,h,m,0);
Date dt2 = Date.valueOf('2012-04-30');
if(dt1 > dt2) {
system.debug(LoggingLevel.INFO,'DateTime:'+dt1+' > Date:'+dt2); }
}
結果のデバッグログが表示される:
USER_DEBUG|[9]|INFO|DateTime:2012-04-30 00:01:00 > Date:2012-04-30 00:00:00
USER_DEBUG|[9]|INFO|DateTime:2012-04-30 00:02:00 > Date:2012-04-30 00:00:00
USER_DEBUG|[9]|INFO|DateTime:2012-04-30 00:03:00 > Date:2012-04-30 00:00:00
...
USER_DEBUG|[9]|INFO|DateTime:2012-05-02 11:57:00 > Date:2012-04-30 00:00:00
USER_DEBUG|[9]|INFO|DateTime:2012-05-02 11:58:00 > Date:2012-04-30 00:00:00
USER_DEBUG|[9]|INFO|DateTime:2012-05-02 11:59:00 > Date:2012-04-30 00:00:00
Date.valueOf('2012-05-01');
と比較することであろうsimplistソリューションを。ただし、DateTimeタイプのnewInstanceGMT
メソッドを使用してCreatedDateフィールドと比較することは可能です。 DateTimeメソッドを使用すると、真夜中を考慮する必要があります。
DateTime dt1 = DateTime.newInstanceGMT(2012,mo,d,h,m,0); // simulated CreatedDate field
Date dt2 = Date.valueOf('2012-05-01');
if(dt1 > dt2) {
system.debug(LoggingLevel.INFO,'DateTime:'+dt1+' > Date:'+dt2); }
OR
DateTime dt1 = DateTime.newInstanceGMT(2012,mo,d,h,m,0); // simulated CreatedDate field
DateTime dt2 = DateTime.newInstanceGMT(2012,04,30,12,59,59);
if(dt1 > dt2) {
system.debug(LoggingLevel.INFO,'DateTime:'+dt1+' > Date:'+dt2); }
どちらの方法は、所望の結果が得られた:
USER_DEBUG|[9]|INFO|DateTime:2012-05-01 00:00:00 > Date:2012-04-30 12:59:59
USER_DEBUG|[9]|INFO|DateTime:2012-05-01 00:01:00 > Date:2012-04-30 12:59:59
USER_DEBUG|[9]|INFO|DateTime:2012-05-01 00:02:00 > Date:2012-04-30 12:59:59
...
USER_DEBUG|[9]|INFO|DateTime:2012-05-02 11:57:00 > Date:2012-04-30 12:59:59
USER_DEBUG|[9]|INFO|DateTime:2012-05-02 11:58:00 > Date:2012-04-30 12:59:59
USER_DEBUG|[9]|INFO|DateTime:2012-05-02 11:59:00 > Date:2012-04-30 12:59:59
私はそのおそらくTZ関連だと思い、日付だけvalがどのTZ情報を持っているとは思いません。 – superfell
良い考えですが、DateTime.newInstance(2012,04,30,12,0,0);のDateTimeもtrueを返し、12時以降はタイムゾーンの相違を補っています。 –
ですが、それは常に日付よりも長くなります。 (私は日付が実際に比較のための日付+真夜中であると思います) – superfell