Get Current Timestamp Using Java

 Posted On  | Yashwant Chavan 

Sample code to get the Java Timestamp with and without Date Object.

Java Timestamp (TimestampExample.java)

Below code is used to get the Timestamp using Java. In for loop we are going to display the current timestamps using Date object.

package com.technicalkeeda.app;

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

public class TimestampExample {
    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {
            Date date = new Date();
            System.out.println("Current Timestamp:- " + new Timestamp(date.getTime()));
        }
    }

}

Output

Current Timestamp:- 2013-05-13 18:53:25.375
Current Timestamp:- 2013-05-13 18:53:25.381
Current Timestamp:- 2013-05-13 18:53:25.381
Current Timestamp:- 2013-05-13 18:53:25.381
Current Timestamp:- 2013-05-13 18:53:25.382
Current Timestamp:- 2013-05-13 18:53:25.382
....
....
....
....
Current Timestamp:- 2013-05-13 18:53:25.382
Current Timestamp:- 2013-05-13 18:53:25.406
Current Timestamp:- 2013-05-13 18:53:25.406
Current Timestamp:- 2013-05-13 18:53:25.406
Current Timestamp:- 2013-05-13 18:53:25.407
Current Timestamp:- 2013-05-13 18:53:25.407
Current Timestamp:- 2013-05-13 18:53:25.407
Current Timestamp:- 2013-05-13 18:53:25.407

Get Timestamp, without Date Object

new Timestamp(System.currentTimeMillis());
System.out.println("Other Way :- " + new Timestamp(System.currentTimeMillis()));


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0318 seconds.