Date クラス
Date のメソッド
Date のメソッドは次のとおりです。
addMonths(additionalMonths)
署名
public Date addMonths(Integer additionalMonths)
パラメータ
- additionalMonths
- 型: Integer
戻り値
型: Date
例
1date myDate = date.newInstance(1990, 11, 21);
2date newDate = myDate.addMonths(3);
3date expectedDate = date.newInstance(1991, 2, 21);
4system.assertEquals(expectedDate, newDate);day()
署名
public Integer day()
戻り値
型: Integer
例
1date myDate = date.newInstance(1989, 4, 21);
2Integer day = myDate.day();
3system.assertEquals(21, day);dayOfYear()
署名
public Integer dayOfYear()
戻り値
型: Integer
例
1date myDate = date.newInstance(1998, 10, 21);
2Integer day = myDate.dayOfYear();
3system.assertEquals(294, day);daysBetween(secondDate)
署名
public Integer daysBetween(Date secondDate)
パラメータ
- secondDate
- 型: Date
戻り値
型: Integer
使用方法
メソッドをコールする日付が secondDate の後に発生する場合、戻り値は負になります。
例
1Date startDate = Date.newInstance(2008, 1, 1);
2Date dueDate = Date.newInstance(2008, 1, 30);
3Integer numberDaysDue = startDate.daysBetween(dueDate);format()
署名
public String format()
戻り値
型: String
例
1// In American-English locale
2date myDate = date.newInstance(2001, 3, 21);
3String dayString = myDate.format();
4system.assertEquals('3/21/2001', dayString);month()
署名
public Integer month()
戻り値
型: Integer
例
1date myDate = date.newInstance(2004, 11, 21);
2Integer month = myDate.month();
3system.assertEquals(11, month);monthsBetween(secondDate)
署名
public Integer monthsBetween(Date secondDate)
パラメータ
- secondDate
- 型: Date
戻り値
型: Integer
例
1Date firstDate = Date.newInstance(2006, 12, 2);
2Date secondDate = Date.newInstance(2012, 12, 8);
3Integer monthsBetween = firstDate.monthsBetween(secondDate);
4System.assertEquals(72, monthsBetween);toStartOfMonth()
署名
public Date toStartOfMonth()
戻り値
型: Date
例
1date myDate = date.newInstance(1987, 12, 17);
2date firstDate = myDate.toStartOfMonth();
3date expectedDate = date.newInstance(1987, 12, 1);
4system.assertEquals(expectedDate, firstDate);toStartOfWeek()
署名
public Date toStartOfWeek()
戻り値
型: Date
例
たとえば、アメリカのロケールでは週は日曜日に始まり、ヨーロッパでは月曜日に始まります。次に例を示します。
1Date myDate = Date.today();
2Date weekStart = myDate.toStartofWeek();valueOf(stringDate)
署名
public static Date valueOf(String stringDate)
パラメータ
- stringDate
- 型: String
戻り値
型: Date
使用方法
指定した文字列は、ローカルタイムゾーンの標準の日付形式「yyyy-MM-dd HH:mm:ss」を使用する必要があります。
例
1string year = '2008';
2string month = '10';
3string day = '5';
4string hour = '12';
5string minute = '20';
6string second = '20';
7string stringDate = year + '-' + month
8 + '-' + day + ' ' + hour + ':' +
9minute + ':' + second;
10
11Date myDate = date.valueOf(stringDate);valueOf(fieldValue)
署名
public static Date valueOf(Object fieldValue)
パラメータ
- fieldValue
- 型: Object
戻り値
型: Date
使用方法
項目が date 項目の場合は、AccountHistory など、履歴 sObject の OldValue 項目または NewValue 項目でこのメソッドを使用します。
例
次の例では、履歴管理項目を Date 値に変換します。
1List<AccountHistory> ahlist = [SELECT Field,OldValue,NewValue FROM AccountHistory];
2for(AccountHistory ah : ahlist) {
3 System.debug('Field: ' + ah.Field);
4 if (ah.field == 'MyDate__c') {
5 Date oldValue = Date.valueOf(ah.OldValue);
6 Date newValue = Date.valueOf(ah.NewValue);
7 }
8}year()
署名
public Integer year()
戻り値
型: Integer
例
1date myDate = date.newInstance(1988, 12, 17);
2system.assertEquals(1988, myDate.year());