LoginSignup
56
50

More than 3 years have passed since last update.

[Android] 現在日時の取得方法

Last updated at Posted at 2014-05-30

概要

端末の現在日時を「yyyy/MM/dd HH:mm:ss」形式の文字列で取得する。

サンプルコード

MainActivity.java
/**
 * 現在日時をyyyy/MM/dd HH:mm:ss形式で取得する.<br>
 */
public static String getNowDate(){
    final DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    final Date date = new Date(System.currentTimeMillis());
    return df.format(date);
}

ポイント

  1. SimpleDateFormatのコンストラクタの引数で取得する日時形式を決定している。
  2. System.currentTimeMillis()で端末時刻を通算ミリ秒で取得し、Dataのコンストラクタの引数に渡すことで現在時刻のDataインスタンスを取得している。
  3. DataFormat#format()に現在時刻のDataインスタンスを引数として渡し、現在日時を指定した形式で取得している。
56
50
1

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
56
50