Loading
メール配信ドメイン検証状況の確認方法のご案内続きを読む
ヘルプサイトの計画メンテナンスに関するお知らせ続きを読む
ただいま大変多くのお問い合わせをいただいており、ご連絡までにお時間を頂戴しております続きを読む

Apex Datetime Class Sometimes Returns Incorrect Year

公開日: Oct 13, 2022
説明
The Apex Datetime Class is commonly used to format datetime values into local times. Depending on the date value, the formatted value could display the wrong calendar year.
解決策
The sample code below has a date near the end of the calendar year and uses 'YYYY' for the year format so it would return the formatted year as 2020 rather than the expected value of 2019. The Apex Datetime class uses the Java SimpleDateFormat which utilizes the capital 'Y' to return the value for the calendar year of that week and the lowercase 'y' as the year for the day. In order to ensure your code returns the year for a particular day, use the lowercase 'y' format for the dateFormatString.
 
Datetime GMTDate = 
  Datetime.newInstanceGmt(2019, 12, 30, 6, 0, 0);
String strConvertedDate = 
  GMTDate.format('MM/dd/YYYY HH:mm:ss', 
                 'Asia/Dubai');
The sample code returns the following values when using the different year formats discussed above:

strConvertedDate = '12/30/2020 10:00:00' when 'YYYY' is used
strConvertedDate = '12/30/2019 10:00:00' when 'yyyy' is used

Java References:
SimpleDateFormat
GregorianCalendar 
ナレッジ記事番号

000380991

 
読み込み中
Salesforce Help | Article