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

Newer Version Available

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

Date クラス

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

名前空間

System

使用方法

Date についての詳細は、プリミティブデータ型を参照してください。

Date メソッド

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

addDays(Integer)

指定した addlDays の数を date に追加します。

署名

public Date addDays(Integer addlDays)

パラメータ

addlDays
型: Integer

戻り値

型: Date

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

addMonths(Integer)

指定した addlMonths の数を date に追加します。

署名

public Date addMonths(Integer addlMonths)

パラメータ

addlMonths
型: 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(Integer)

指定した addlYears の数を date に追加します。

署名

public Date addYears(Integer addlYears)

パラメータ

addlYears
型: 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(Date)

メソッドをコールした日付と指定された日付の間の日数を返します。

署名

public Integer daysBetween(Date compDate)

パラメータ

compDate
型: Date

戻り値

型: Integer

使用方法

メソッドをコールする日付が compDate の後に発生する場合、戻り値は負になります。

1Date startDate = Date.newInstance(2008, 1, 1);
2Date dueDate = Date.newInstance(2008, 1, 30);
3Integer numberDaysDue = startDate.daysBetween(dueDate);

daysInMonth(Integer, Integer)

指定された 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(Integer)

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

署名

public static Boolean isLeapYear(Integer year)

パラメータ

year
型: Integer

戻り値

型: Boolean

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

isSameDay(Date)

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

署名

public Boolean isSameDay(Date compDate)

パラメータ

compDate
型: 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(Date)

メソッドをコールした日付と指定された日付の間の月数を返します。日数の差異は無視されます。

署名

public Integer monthsBetween(Date compDate)

パラメータ

compDate
型: 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(Integer, Integer, Integer)

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

署名

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

パラメータ

year
型: Integer
month
型: Integer
Date
型: Integer

戻り値

型: Date

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

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

parse(String)

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

署名

public static Date parse(String Date)

パラメータ

Date
型: 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(String)

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

署名

public static Date valueOf(String toDate)

パラメータ

toDate
型: 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(Object)

指定した履歴管理項目の値を date に変換します。

署名

public static Date valueOf(Object fieldValue)

パラメータ

fieldValue
型: Object

戻り値

型: Date

使用方法

項目が date 項目の場合は、AccountHistory など、履歴 sObject の OldValue 項目または NewValue 項目でこのメソッドを使用します。

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

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

署名

public Integer year()

戻り値

型: Integer

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