Javaで現在のタイムスタンプを取得するには:

Timestamp timestamp = new Timestamp(System.currentTimeMillis());//2016-11-16 06:43:19.77

Javaで現在のタイムスタンプを取得する方法を示す2つのJavaの例を示します。 (Java 8で更新されました)

1. java.sql.Timestamp

現在の `java.sql.Timestamp`を取得する2つのメソッド

TimeStampExample.java

package com.mkyong.date;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeStampExample {

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");

    public static void main(String[]args) {

       //method 1
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        System.out.println(timestamp);

       //method 2 - via Date
        Date date = new Date();
        System.out.println(new Timestamp(date.getTime()));

       //return number of milliseconds since January 1, 1970, 00:00:00 GMT
        System.out.println(timestamp.getTime());

       //format timestamp
        System.out.println(sdf.format(timestamp));

    }

}

出力

2016-11-16 06:43:19.77
2016-11-16 06:43:19.769
1479249799770
2016.11.16.06.43.19

2. java.time.Instant

Java 8では、

java.sql.Timestamp`を新しい

java.time.Instant`に変換することができます

InstantExample.java

package com.mkyong.date;

import java.sql.Timestamp;
import java.time.Instant;

public class InstantExample {

    public static void main(String[]args) {

        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        System.out.println(timestamp);

       //return number of milliseconds since January 1, 1970, 00:00:00 GMT
        System.out.println(timestamp.getTime());

       //Convert timestamp to instant
        Instant instant = timestamp.toInstant();
        System.out.println(instant);

       //return number of milliseconds since the epoch of 1970-01-01T00:00:00Z
        System.out.println(instant.toEpochMilli());

       //Convert instant to timestamp
        Timestamp tsFromInstant = Timestamp.from(instant);
        System.out.println(tsFromInstant.getTime());

    }

}

出力

2016-11-16 06:55:40.11
1479250540110
2016-11-15T22:55:40.110Z
1479250540110
1479250540110

参考文献

JavaDoc]。

https://docs/

JavaDoc]

リンク://タグ/日付/[日付]リンク://タグ/インスタント/[インスタント]

java.time


timestamp