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

Datetime クラス

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

名前空間

System

使用方法

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

Datetime のメソッド

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

addDays(additionalDays)

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

署名

public Datetime addDays(Integer additionalDays)

パラメータ

additionalDays
型: Integer

戻り値

型: Datetime

1Datetime myDateTime = Datetime.newInstance(1960, 2, 17);
2Datetime newDateTime = myDateTime.addDays(2);
3Datetime expected = Datetime.newInstance(1960, 2, 19);
4System.assertEquals(expected, newDateTime);

addHours(additionalHours)

指定した時間数を datetime に加算します。

署名

public Datetime addHours(Integer additionalHours)

パラメータ

additionalHours
型: Integer

戻り値

型: Datetime

1DateTime myDateTime = DateTime.newInstance(1997, 1, 31, 7, 8, 16);
2DateTime newDateTime = myDateTime.addHours(3);
3DateTime expected = DateTime.newInstance(1997, 1, 31, 10, 8, 16);
4System.assertEquals(expected, newDateTime);

addMinutes(additionalMinutes)

指定した分数を datetime に加算します。

署名

public Datetime addMinutes(Integer additionalMinutes)

パラメータ

additionalMinutes
型: Integer

戻り値

型: Datetime

1DateTime myDateTime = DateTime.newInstance(1999, 2, 11, 8, 6, 16);
2DateTime newDateTime = myDateTime.addMinutes(7);
3DateTime expected = DateTime.newInstance(1999, 2, 11, 8, 13, 16);
4System.assertEquals(expected, newDateTime);

addMonths(additionalMonths)

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

署名

public Datetime addMonths(Integer additionalMonths)

パラメータ

additionalMonths
型: Integer

戻り値

型: Datetime

1DateTime myDateTime = DateTime.newInstance(2000, 7, 7, 7, 8, 12);
2DateTime newDateTime = myDateTime.addMonths(1);
3DateTime expected = DateTime.newInstance(2000, 8, 7, 7, 8, 12);
4System.assertEquals(expected, newDateTime);

addSeconds(additionalSeconds)

指定した秒数を datetime に加算します。

署名

public Datetime addSeconds(Integer additionalSeconds)

パラメータ

additionalSeconds
型: Integer

戻り値

型: Datetime

1DateTime myDateTime = DateTime.newInstance(2001, 7, 19, 10, 7, 12);
2DateTime newDateTime = myDateTime.addSeconds(4);
3DateTime expected = DateTime.newInstance(2001, 7, 19, 10, 7, 16);
4System.assertEquals(expected, newDateTime);

addYears(additionalYears)

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

署名

public Datetime addYears(Integer additionalYears)

パラメータ

additionalYears
型: Integer

戻り値

型: Datetime

1DateTime myDateTime = DateTime.newInstance(2009, 12, 17, 13, 6, 6);
2DateTime newDateTime = myDateTime.addYears(1);
3DateTime expected = DateTime.newInstance(2010, 12, 17, 13, 6, 6);
4System.assertEquals(expected, newDateTime);

date()

コンテキストユーザのローカルタイムゾーンで datetime の date コンポーネントを返します。

署名

public Date date()

戻り値

型: Date

1DateTime myDateTime = DateTime.newInstance(2006, 3, 16, 12, 6, 13);
2Date myDate = myDateTime.date();
3Date expected = Date.newInstance(2006, 3, 16);
4System.assertEquals(expected, myDate);

dateGMT()

GMT タイムゾーンで datetime の date コンポーネントを返します。

署名

public Date dateGMT()

戻り値

型: Date

1// California local time, PST
2DateTime myDateTime = DateTime.newInstance(2006, 3, 16, 23, 0, 0);
3Date myDate = myDateTime.dateGMT();
4Date expected = Date.newInstance(2006, 3, 17);
5System.assertEquals(expected, myDate);

day()

コンテキストユーザのローカルタイムゾーンで datetime の day-of-month コンポーネントを返します。

署名

public Integer day()

戻り値

型: Integer

1DateTime myDateTime = DateTime.newInstance(1986, 2, 21, 23, 0, 0);
2System.assertEquals(21, myDateTime.day());

dayGmt()

GMT タイムゾーンで datetime の day-of-month コンポーネントを返します。

署名

public Integer dayGmt()

戻り値

型: Integer

1// California local time, PST
2DateTime myDateTime = DateTime.newInstance(1987, 1, 14, 23, 0, 3);
3System.assertEquals(15, myDateTime.dayGMT());

dayOfYear()

コンテキストユーザのローカルタイムゾーンで datetime の day-of-year コンポーネントを返します。

署名

public Integer dayOfYear()

戻り値

型: Integer

たとえば、2008 年 2 月 5 日午前 8 時 30 分 12 秒は、day 36 です。

1Datetime myDate = Datetime.newInstance(2008, 2, 5, 8, 30, 12);
2system.assertEquals(myDate.dayOfYear(), 36);

dayOfYearGmt()

GMT タイムゾーンで datetime の day-of-year コンポーネントを返します。

署名

public Integer dayOfYearGmt()

戻り値

型: Integer

1// This sample assumes we are in the PST timezone
2DateTime myDateTime = DateTime.newInstance(1999, 2, 5, 23, 0, 3);
3// January has 31 days + 5 days in February = 36 days
4// dayOfYearGmt() adjusts the time zone from the current time zone to GMT
5// by adding 8 hours to the PST time zone, so it's 37 days and not 36 days 
6System.assertEquals(37, myDateTime.dayOfYearGmt());

format()

日付をローカルタイムゾーンに変換し、変換した日付をコンテキストユーザのロケールを使用して形式設定された文字列として返します。タイムゾーンを指定できない場合は、GMT が使用されます。

署名

public String format()

戻り値

型: String

1DateTime myDateTime = DateTime.newInstance(1993, 6, 6, 3, 3, 3);
2system.assertEquals('6/6/1993 3:03 AM', mydatetime.format());

format(dateFormatString)

日付をローカルタイムゾーンに変換し、変換した日付を提供された Java の SimpleDateFormatを使用して文字列として返します。タイムゾーンを指定できない場合は、GMT が使用されます。

署名

public String format(String dateFormatString)

パラメータ

dateFormatString
型: String

戻り値

型: String

使用方法

Java の SimpleDateFormat についての詳細は、「Java SimpleDateFormat」を参照してください。

1Datetime myDT = Datetime.now();
2String myDate = myDT.format('h:mm a');

format(dateFormatString, timezone)

日付を指定されたタイムゾーンに変換し、変換した日付を提供された Java の SimpleDateFormat を使用して文字列として返します。提供されたタイムゾーンが適切でない場合、GMT が使用されます。

署名

public String format(String dateFormatString, String timezone)

パラメータ

dateFormatString
型: String
timezone
型: String
timezone 引数の有効なタイムゾーン値は、Java の TimeZone.getAvailableIDs メソッドから返されるタイムゾーンに対応する Java TimeZone クラスのタイムゾーンです。3 文字の省略名ではなく、タイムゾーンの完全名を使用することをお勧めします。

戻り値

型: String

使用方法

Java の SimpleDateFormat についての詳細は、「Java SimpleDateFormat」を参照してください。

次の例では format を使用して、GMT 日付を America/New_York タイムゾーンに変換し、指定された日付形式を使用してフォーマットします。
1Datetime GMTDate = 
2  Datetime.newInstanceGmt(2011,6,1,12,1,5);
3String strConvertedDate = 
4  GMTDate.format('MM/dd/yyyy HH:mm:ss', 
5                 'America/New_York');
6// Date is converted to 
7// the new time zone and is adjusted
8// for daylight saving time.
9System.assertEquals(
10  '06/01/2011 08:01:05', strConvertedDate);

formatGmt(dateFormatString)

提供された Java の SimpleDateFormat と GMT タイムゾーンを使用して、datetime を文字列として返します。

署名

public String formatGmt(String dateFormatString)

パラメータ

dateFormatString
型: String

戻り値

型: String

使用方法

Java の SimpleDateFormat についての詳細は、「Java SimpleDateFormat」を参照してください。

1DateTime myDateTime = DateTime.newInstance(1993, 6, 6, 3, 3, 3);
2String formatted = myDateTime.formatGMT('EEE, MMM d yyyy HH:mm:ss');
3String expected = 'Sun, Jun 6 1993 10:03:03';
4System.assertEquals(expected, formatted);

formatLong()

日付をローカルタイムゾーンに変換し、変換した日付を長い日付形式で返します。

署名

public String formatLong()

戻り値

型: String

1// Passing local date based on the PST time zone
2Datetime dt = DateTime.newInstance(2012,12,28,10,0,0);
3// Writes 12/28/2012 10:00:00 AM PST
4System.debug('dt.formatLong()=' + dt.formatLong());

getTime()

この DateTime オブジェクトで表された 1970 年 1 月 1 日 0 時 0 分 0 秒 (GMT) を起点としたミリ秒数を返します。

署名

public Long getTime()

戻り値

型: Long

1DateTime dt = DateTime.newInstance(2007, 6, 23, 3, 3, 3);
2Long gettime = dt.getTime();
3Long expected = 1182592983000L;
4System.assertEquals(expected, gettime);

hour()

コンテキストユーザのローカルタイムゾーンで datetime の hour コンポーネントを返します。

署名

public Integer hour()

戻り値

型: Integer

1DateTime myDateTime = DateTime.newInstance(1998, 11, 21, 3, 3, 3);
2System.assertEquals(3 , myDateTime.hour());

hourGmt()

GMT タイムゾーンで datetime の hour コンポーネントを返します。

署名

public Integer hourGmt()

戻り値

型: Integer

1// California local time
2DateTime myDateTime = DateTime.newInstance(2000, 4, 27, 3, 3, 3);
3System.assertEquals(10 , myDateTime.hourGMT());

isSameDay(dateToCompare)

コンテキストユーザのローカルタイムゾーンで、メソッドをコールした datetime と指定された datetime が同じ場合、true を返します。

署名

public Boolean isSameDay(Datetime dateToCompare)

パラメータ

dateToCompare
型: Datetime

戻り値

型: Boolean

1datetime myDate = datetime.now();
2datetime dueDate = 
3     datetime.newInstance(2008, 1, 30);
4boolean dueNow = myDate.isSameDay(dueDate);

millisecond()

コンテキストユーザのローカルタイムゾーンで datetime の millisecond コンポーネントを返します。

署名

public Integer millisecond()

戻り値

型: Integer

1DateTime myDateTime = DateTime.now();
2system.debug(myDateTime.millisecond());

millisecondGmt()

GMT タイムゾーンで datetime の millisecond コンポーネントを返します。

署名

public Integer millisecondGmt()

戻り値

型: Integer

1DateTime myDateTime = DateTime.now();
2system.debug(myDateTime.millisecondGMT());

minute()

コンテキストユーザのローカルタイムゾーンで datetime の minute コンポーネントを返します。

署名

public Integer minute()

戻り値

型: Integer

1DateTime myDateTime = DateTime.newInstance(2001, 2, 27, 3, 3, 3);
2system.assertEquals(3, myDateTime.minute());

minuteGmt()

GMT タイムゾーンで datetime の minute コンポーネントを返します。

署名

public Integer minuteGmt()

戻り値

型: Integer

1DateTime myDateTime = DateTime.newInstance(2002, 12, 3, 3, 3, 3);
2system.assertEquals(3, myDateTime.minuteGMT());

month()

コンテキストユーザのローカルタイムゾーンで datetime の month コンポーネントを返します (1 =1 月)。

署名

public Integer month()

戻り値

型: Integer

1DateTime myDateTime = DateTime.newInstance(2004, 11, 4, 3, 3, 3);
2system.assertEquals(11, myDateTime.month());

monthGmt()

GMT タイムゾーンで datetime の month コンポーネントを返します (1 = 1 月)。

署名

public Integer monthGmt()

戻り値

型: Integer

1DateTime myDateTime = DateTime.newInstance(2006, 11, 19, 3, 3, 3);
2system.assertEquals(11, myDateTime.monthGMT());

newInstance(milliseconds)

datetime を構築し、1970 年 1 月 1 日 00:00:00 (GMT) を起点とした指定したミリ秒数を表すように初期化します。

署名

public static Datetime newInstance(Long milliseconds)

パラメータ

milliseconds
型: Long

戻り値

型: Datetime

日付は GMT タイムゾーンで返されます。

1Long longtime = 1341828183000L;
2DateTime dt = DateTime.newInstance(longtime);
3DateTime expected = DateTime.newInstance(2012, 7, 09, 3, 3, 3);
4System.assertEquals(expected, dt);

newInstance(date, time)

ローカルタイムゾーンの指定された日時から datetime を構築します。

署名

public static Datetime newInstance(Date date, Time time)

パラメータ

date
型: Date
time
型: Time

戻り値

型: Datetime

日付は GMT タイムゾーンで返されます。

1Date myDate = Date.newInstance(2011, 11, 18);
2Time myTime = Time.newInstance(3, 3, 3, 0);
3DateTime dt = DateTime.newInstance(myDate, myTime);
4DateTime expected = DateTime.newInstance(2011, 11, 18, 3, 3, 3);
5System.assertEquals(expected, dt);

newInstance(year, month, day)

ローカルタイムゾーンの午前 0 時に、指定した年、月 (1 = 1 月)、日の integer 表現から datetime を構築します。

署名

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

パラメータ

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

戻り値

型: Datetime

日付は GMT タイムゾーンで返されます。

1datetime myDate = datetime.newInstance(2008, 12, 1);

newInstance(year, month, day, hour, minute, second)

ローカルタイムゾーンで、指定した年、月 (1 = 1 月)、日、時、分、および秒の integer 表現から datetime を構築します。

署名

public static Datetime newInstance(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer second)

パラメータ

year
型: Integer
month
型: Integer
day
型: Integer
hour
型: Integer
minute
型: Integer
second
型: Integer

戻り値

型: Datetime

日付は GMT タイムゾーンで返されます。

1Datetime myDate = Datetime.newInstance(2008, 12, 1, 12, 30, 2);

newInstanceGmt(date, time)

GMT タイムゾーンの指定された日時から datetime を構築します。

署名

public static Datetime newInstanceGmt(Date date, Time time)

パラメータ

date
型: Date
time
型: Time

戻り値

型: Datetime

1Date myDate = Date.newInstance(2013, 11, 12);
2Time myTime = Time.newInstance(3, 3, 3, 0);
3DateTime dt = DateTime.newInstanceGMT(myDate, myTime);
4DateTime expected = DateTime.newInstanceGMT(2013, 11, 12, 3, 3, 3);
5System.assertEquals(expected, dt);

newInstanceGmt(year, month, date)

GMT タイムゾーンの午前 0 時に、指定した年、月 (1 = 1 月)、日の integer 表現から datetime を構築します。

署名

public static Datetime newInstanceGmt(Integer year, Integer month, Integer date)

パラメータ

year
型: Integer
month
型: Integer
date
型: Integer

戻り値

型: Datetime

1DateTime dt = DateTime.newInstanceGMT(1996, 3, 22);

newInstanceGmt(year, month, date, hour, minute, second)

GMT タイムゾーンで、指定した年、月 (1 = 1 月)、日、時、分、および秒の integer 表現から datetime を構築します。

署名

public static Datetime newInstanceGmt(Integer year, Integer month, Integer date, Integer hour, Integer minute, Integer second)

パラメータ

year
型: Integer
month
型: Integer
date
型: Integer
hour
型: Integer
minute
型: Integer
second
型: Integer

戻り値

型: Datetime

1//California local time
2DateTime dt = DateTime.newInstanceGMT(1998, 1, 29, 2, 2, 3);
3DateTime expected = DateTime.newInstance(1998, 1, 28, 18, 2, 3);
4System.assertEquals(expected, dt);

now()

GMT カレンダーに基づいて、現在の datetime を返します。

署名

public static Datetime now()

戻り値

型: Datetime

返される datetime の形式は 'MM/DD/YYYY HH:MM PERIOD' です。

1datetime myDateTime = datetime.now();

parse(datetimeString)

ユーザロケールのローカルタイムゾーンおよび形式で指定された文字列で datetime を構築します。

署名

public static Datetime parse(String datetimeString)

パラメータ

datetimeString
型: String

戻り値

型: Datetime

日付は GMT タイムゾーンで返されます。

次の例では parse を使用して、英語 (アメリカ) ロケール形式の文字列として渡される日付から datetime を作成します。使用しているロケールによっては、日付文字列の形式を変更する必要があります。
1Datetime dt = DateTime.parse('10/14/2011 11:46 AM');
2String myDtString = dt.format();
3system.assertEquals(myDtString, '10/14/2011 11:46 AM');

second()

コンテキストユーザのローカルタイムゾーンで datetime の second コンポーネントを返します。

署名

public Integer second()

戻り値

型: Integer

1DateTime dt = DateTime.newInstanceGMT(1999, 9, 22, 3, 1, 2);
2System.assertEquals(2, dt.second());

secondGmt()

GMT タイムゾーンで datetime の second コンポーネントを返します。

署名

public Integer secondGmt()

戻り値

型: Integer

1DateTime dt = DateTime.newInstance(2000, 2, 3, 3, 1, 5);
2System.assertEquals(5, dt.secondGMT());

time()

コンテキストユーザのローカルタイムゾーンで datetime の time コンポーネントを返します。

署名

public Time time()

戻り値

型: Time

1DateTime dt = DateTime.newInstance(2002, 11, 21, 0, 2, 2);
2Time expected = Time.newInstance(0, 2, 2, 0);
3System.assertEquals(expected, dt.time());

timeGmt()

GMT タイムゾーンで datetime の time コンポーネントを返します。

署名

public Time timeGmt()

戻り値

型: Time

1// This sample is based on the PST time zone
2DateTime dt = DateTime.newInstance(2004, 1, 27, 4, 1, 2);
3Time expected = Time.newInstance(12, 1, 2, 0);
4// 8 hours are added to the time to convert it from
5// PST to GMT
6System.assertEquals(expected, dt.timeGMT());

valueOf(dateTimeString)

指定した文字列の値を含む datetime を返します。

署名

public static Datetime valueOf(String dateTimeString)

パラメータ

dateTimeString
型: String

戻り値

型: Datetime

日付は GMT タイム���ーンで返されます。

使用方法

指定した文字列は、ローカルタイムゾーンの標準の日付形式「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 + '-' + day + ' ' + hour + ':' 
8    + minute +  ':' + second;
9
10Datetime myDate = Datetime.valueOf(stringDate);

valueOf(fieldValue)

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

署名

public static Datetime valueOf(Object fieldValue)

パラメータ

fieldValue
型: Object

戻り値

型: Datetime

使用方法

項目が datetime 項目の場合は、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 == 'MyDatetime__c') {
5    Datetime oldValue = Datetime.valueOf(ah.OldValue);
6    Datetime newValue = Datetime.valueOf(ah.NewValue);
7  }
8}

valueOfGmt(dateTimeString)

指定した文字列の値を含む datetime を返します。

署名

public static Datetime valueOfGmt(String dateTimeString)

パラメータ

dateTimeString
型: String

戻り値

型: Datetime

使用方法

指定した文字列は、GMT タイムゾーンの標準の日付形式「yyyy-MM-dd HH:mm:ss」を使用する必要があります。

1// California locale time
2string year = '2009';
3string month = '3';
4string day = '5';
5string hour = '5';
6string minute = '2';
7string second = '2';
8string stringDate = year + '-' + month + '-' + day + ' ' + hour + ':' 
9    + minute +  ':' + second;
10
11Datetime myDate = Datetime.valueOfGMT(stringDate);
12
13DateTime expected = DateTime.newInstance(2009, 3, 4, 21, 2, 2);
14System.assertEquals(expected, myDate);

year()

コンテキストユーザのローカルタイムゾーンで datetime の year コンポーネントを返します。

署名

public Integer year()

戻り値

型: Integer

1DateTime dt = DateTime.newInstance(2012, 1, 26, 5, 2, 4);
2System.assertEquals(2012, dt.year());

yearGmt()

GMT タイムゾーンで datetime の year コンポーネントを返します。

署名

public Integer yearGmt()

戻り値

型: Integer

1DateTime dt = DateTime.newInstance(2012, 10, 4, 6, 4, 6);
2System.assertEquals(2012, dt.yearGMT());