この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

Newer Version Available

This content describes an older version of this product. View Latest

Date クラス

Date ��リミティブデータ型のメソッドが含まれます。

名前空間

System

使用方法

Date についての詳細は、「Date データ型」を参照してください。

Date のメソッド

Date のメソッドは次のとおりです。

addDays(additionalDays)

指定した追加日数を date に加算します。

署名

public Date addDays(Integer additionalDays)

パラメーター

additionalDays
型: Integer

戻り値

型: Date

1Date myDate = Date.newInstance(1960, 2, 17);
2Date newDate = mydate.addDays(2);

addMonths(additionalMonths)

指定した追加月数を date に加算します。

署名

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);

addYears(additionalYears)

指定した追加年数を date に加算します。

署名

public Date addYears(Integer additionalYears)

パラメーター

additionalYears
型: Integer

戻り値

型: Date

1date myDate = date.newInstance(1983, 7, 15);
2date newDate = myDate.addYears(2);
3date expectedDate = date.newInstance(1985, 7, 15);
4system.assertEquals(expectedDate, newDate);

day()

date の day-of-month コンポーネントを返します。

署名

public Integer day()

戻り値

型: Integer

1date myDate = date.newInstance(1989, 4, 21);
2Integer day = myDate.day();
3system.assertEquals(21, day);

dayOfYear()

date の day-of-year コンポーネントを返します。

署名

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);

daysInMonth(year, month)

指定された yearmonth の月の日数を返します (1 = 1 月)。

署名

public static Integer daysInMonth(Integer year, Integer month)

パラメーター

year
型: Integer
month
型: Integer

戻り値

型: Integer

次の例は、1960 年の 2 月の日数を検索します。

1Integer numberDays = date.daysInMonth(1960, 2);

format()

コンテキストユーザーのロケールを使用して、date を文字列として返します。

署名

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);

isLeapYear(year)

指定した年がうるう年の場合、true を返します。

署名

public static Boolean isLeapYear(Integer year)

パラメーター

year
型: Integer

戻り値

型: Boolean

1system.assert(Date.isLeapYear(2004));

isSameDay(dateToCompare)

メソッドをコールした日付が指定された日付と同じ場合、true を返します。

署名

public Boolean isSameDay(Date dateToCompare)

パラメーター

dateToCompare
型: Date

戻り値

型: Boolean

1date myDate = date.today();
2date dueDate = date.newInstance(2008, 1, 30);
3boolean dueNow = myDate.isSameDay(dueDate);

month()

date の month コンポーネントを返します (1 =1 月)。

署名

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);

newInstance(year, month, day)

yearmonth (1= 1 月)、day の integer 表現から date を構築します。

署名

public static Date newInstance(Integer year, Integer month, Integer day)

パラメーター

year
型: Integer
month
型: Integer
day
型: Integer

戻り値

型: Date

次の例では、1960 年 2 月 17 日を作成します。

1Date myDate = date.newinstance(1960, 2, 17);

parse(stringDate)

string から date を構築します。string の形式は、ローカルの日付形式によって異なります。

署名

public static Date parse(String stringDate)

パラメーター

stringDate
型: String

戻り値

型: Date

次の例は、いくつかのロケールで機能します。

1date mydate = date.parse('12/27/2009');

today()

現在の日付を現在のユーザーのタイムゾーンで返します。

署名

public static Date today()

戻り値

型: Date

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)

指定した string の値を含む date を返します。

署名

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)

指定されたオブジェクトを Date に変換します。このメソッドを使用して、履歴管理項目の値または Date 値を表すオブジェクトを変換します。

署名

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}

バージョン管理動作の変更

Date.valueOf は、次のリリースでバージョン管理されています。
API バージョン 33.0 以前
Datetime オブジェクトを指定して Date.valueOf をコールすると、メソッドは時、分、秒、ミリ秒のセットを含む Date 値を返します。
API バージョン 34.0 ~ API バージョン 53.0
Datetime オブジェクトを指定して Date.valueOf をコールすると、メソッドは Datetime を時間の情報がない有効な Date に変換します。ただし、結果は、Datetime オブジェクトを初期化した方法によって変化します。たとえば、Datetime オブジェクトを初期化するとき�� Datetime.valueOf(stringDate) を使用した場合、返される Date 値には、時間 (時) の情報が含まれます。Datetime オブジェクトを初期化するときに Datetime.newInstance(year, month, day, hour, minute, second) を使用した場合、返される Date 値には、時間の情報は含まれません。
API バージョン 54.0 以降
Datetime オブジェクトを指定して Date.valueOf をコールすると、メソッドはオブジェクトを時間の情報がない有効な Date に変換します。

year()

date の year コンポーネントを返します。

署名

public Integer year()

戻り値

型: Integer

1date myDate = date.newInstance(1988, 12, 17);
2system.assertEquals(1988, myDate.year());