How to get current timestamps in Java

To get the current timestamp in Java :


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

Here are two Java examples to show you how to get current timestamps in Java. (Updated with Java 8)

1. java.sql.Timestamp

Two methods to get the current java.sql.Timestamp

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));
        
    }

}

Output


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

In Java 8, you can convert java.sql.Timestamp to the new 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());

    }

}

Output


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

References

  1. java.sql.Timestamp JavaDoc
  2. java.time.Instant JavaDoc

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
10 Comment threads
11 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
19 Comment authors
Memek pak abrahamkrishnateja addalamkyongjosephTripl3Fac3 Recent comment authors
newest oldest most voted
Latest Tutorials
Guest
Latest Tutorials

For Sql date and Time Stamp

java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
Timestamp timestamp = new Timestamp(date.getTime());
preparedStatement.setTimestamp(1, timestamp);

Kareen Tariman
Guest
Kareen Tariman

This helped. Thanks!

Memek pak abraham
Guest
Memek pak abraham

bau memek sih awkokwokow

krishnateja addala
Guest
krishnateja addala

hi sir/madam this is Teja i hava one problem of getting data from excelsheet in java in that excelsheet i had column called date column in excelsheet one task is given for me if date column cell is empty we should raise popup message like date column is empty like this i have written code but its showing parsed exception to my code please go through bellow code package com.hospital.patient; public class Exceldata { public static final String FilePath = “C:\\Users\\HP\\Documents\\patients.xlsx”; //static refcount; //@SuppressWarnings(“static-access”) public static List convert() throws EncryptedDocumentException, InvalidFormatException, IOException, ParseException { List patientList = new ArrayList(); Workbook… Read more »

joseph
Guest
joseph

I really love your blog my friend it saved my live so much time thanks a lot for what you do sincerly yours :)

Prem Kumar
Guest
Prem Kumar

My point of view is we should use the Calendar class instead of Date.

pooja
Guest
pooja

hello sir/madam,

I have one problem please help me anyone

I have hard code date in my java code for 1 min to 8 hours with 6 currency pairs like
java.sql.Timestamp timestamp = Timestamp.valueOf(“2012-02-24 20:00:00”);
then its working fine.

If i have add for live chart like
java.sql.Timestamp timestamp = new Timestamp(System.currentTimeMillis());

or
java.util.Date date= new java.util.Date();
java.sql.Timestamp timestamp = new Timestamp(today.getTime());

then its taking lot of time to plot the jfreechart graph .

so give me some suggestion or any commands need to add in my java code.

Its urgent please.

Ko Lawson
Guest
Ko Lawson

This is what I have been searching for days. Thanks !
Both overloaded constructors work great.

Morten Slott Hansen
Guest
Morten Slott Hansen

This is simpler and avoids using Date

new Timestamp(System.currentTimeMillis());
Rubens Mariuzzo
Guest
Rubens Mariuzzo

Love this implementation.

Ritvik
Guest
Ritvik

^^ This is great!

Mark Jansen
Guest
Mark Jansen

This is an awesome solution…..

Siddhanta
Guest
Siddhanta

Thanks alot

Ko Lawson
Guest
Ko Lawson

This is what I have been searching for days. Thanks !

j
Guest
j

Thanks for this!

mkyong
Guest
mkyong

You’re welcome.

Krishna
Guest
Krishna

i like u r website and how to ask questions

mkyong
Guest
mkyong

Thanks, article is updated.

Tripl3Fac3
Guest
Tripl3Fac3

I would rather do it like this, because the output will only be like >> [16:34:54] “YOUR TEXT HERE…” That means you can actually look how long it exactly took to complete a command/loop through the code. package org.solarvision.client; import java.sql.Timestamp; public class TimeStamp { @SuppressWarnings(“deprecation”) public static String getCurrentTimeStamp() { java.util.Date date= new java.util.Date(); Timestamp time = new Timestamp(date.getTime()); return “[” + time.getHours() + “:” + time.getMinutes() + “:” + time.getSeconds() + “] “; } }

huzzlah
Guest
huzzlah

real nice and all, but your output is not a timestamp. a timestamp is the number of seconds since january 1 1970. what you’re creating is some sort of arbitrarily formatted date string.

get with it, please.

mkyong
Guest
mkyong

Article is updated, to return the number of seconds since january 1 1970, use this timestamp.getTime()