7
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Apex 日付

Last updated at Posted at 2017-01-24

現在時刻/今日の取得

Datetime t = Datetime.now();
Date today = Date.today();
Long t = Datetime.now().getTime(); 1970年からのミリ秒

Date型を文字列にした時のフォーマット

2017-03-31 Date
2017-01-26T07:12:03.000Z Datetime

フォーマットを指定してDatetime型を文字列にする (TimezoneはAsia/Tokyo)

dt.format('yyyy/MM/dd HH:mm', UserInfo.getTimeZone().getId())

データ変換 (Date <-> String)

String test_str = '2017-1-1 15:30:00';
Datetime test_dt = Datetime.valueOfGmt(test_str);`
//Datetime.valueOf()で変換するとGMT時間となり -9h されてしまうことに注意
Date dt = Date.valueOf('2019-1-20');

startDateTime.format('yyyyMMdd')`

DatetimeからDate

Date mdate = lastUpdateDatetime.date(); //date()でDate型を取得

ミリ秒数からの変換

Long msec = mydatetime.getTime(); //1970 年 1 月 1 日 00:00:00 (GMT) を起点とした指定したミリ秒数(1341828183000L)
Datetime mydatetime = Datetime.newInstance(msec);

日付の差分を求める(Date.daysBetween())

Date lastUpdateDate = Datetime.newInstance(lastUpdateTime).date();
Integer sabun = Date.today().daysBetween(lastUpdateDate);

GMT時間をローカル時間に補正する数値の算出

/*
 * get Timezone offset
 */ 
public static Double getTimezoneOffset(){
    TimeZone tz = UserInfo.getTimeZone();
    return tz.getOffset(DateTime.now()) / (1000 * 3600 * 24.0);
}

7
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?