How to Get Current Timestamps in Java

This article shows how to get the current date time or timestamps in Java.


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

  // 2025-03-07 21:34:46.504
  // Get current java.sql.Timestamp
  Timestamp timestamp = new Timestamp(System.currentTimeMillis());
  
  // 2025-03-07 21:34:46.504
  // Get current java.sql.Timestamp from a Date
  Date date = new Date();
  Timestamp timestamp2 = new Timestamp(date.getTime());

  // convert Instant to java.sql.Timestamp
  Timestamp ts = Timestamp.from(Instant.now());

  // convert ZonedDateTime to Instant to java.sql.Timestamp
  Timestamp ts = Timestamp.from(ZonedDateTime.now().toInstant());

  // convert java.sql.Timestamp to Instant
  Instant instant = ts.toInstant();

Table of contents

1. Using java.time.Instant (Java 8+)

Starting with Java 8, the modern and preferred way to get a timestamp is by using the Instant class:

CurrentTimestampExample.java

package com.mkyong;

import java.time.Instant;

public class CurrentTimestampExample {

    public static void main(String[] args) {
        
        Instant currentTimestamp = Instant.now();
        System.out.println("Current Timestamp: " + currentTimestamp);
        
    }

}

Output:

Terminal

Current Timestamp: 2025-03-07T13:22:21.899489300Z

2. Using LocalDateTime (Java 8+)

If we want a human-readable timestamp, try LocalDateTime.

LocalDateTimeExample.java

package com.mkyong;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeExample {
    
    public static void main(String[] args) {
        
        LocalDateTime currentDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println("Current Timestamp: " + currentDateTime.format(formatter));
        
    }
    
}

Output:

Terminal

Current Timestamp: 2025-03-07 13:22:21

3. Using java.sql.Timestamp

The java.sql.Timestamp class is widely used in JDBC programming to handle timestamps, especially when interacting with databases:

Timestamp.java

package com.mkyong;

import java.sql.Timestamp;

public class TimestampExample {
    
    public static void main(String[] args) {
        
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        System.out.println("Current Timestamp: " + timestamp);
        
    }
    
}

Output:

Terminal

Current Timestamp: 2025-03-07 21:34:46.504

3.1 Using java.util.Date with Timestamp

Alternatively, we can convert a Date object to a java.sql.Timestamp:

TimestampFromDateExample.java

package com.mkyong;

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

public class TimestampFromDateExample {
    
    public static void main(String[] args) {
        
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        System.out.println("Current Timestamp: " + timestamp);
        
    }
    
}

Output:

Terminal

Current Timestamp: 2025-03-07 21:34:46.504

3.2 Format java.sql.Timestamp with SimpleDateFormat

TimeStampExample.java

package com.mkyong.wpbackend;

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

public class TimeStampExample {

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

    // 2025-03-07T21:29:25.187+08:00
    private static final SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");

    // 2025-03-07 21:29:25
    private static final SimpleDateFormat sdf3 = 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);                      // 2025-03-07 21:29:25.187

        // method 2 - via Date, legacy
        Date date = new Date();
        System.out.println(new Timestamp(date.getTime()));  // 2025-03-07 21:29:25.187

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

        System.out.println(sdf1.format(timestamp));         // 2025.03.07.21.29.25

        System.out.println(sdf2.format(timestamp));         // 2025-03-07T21:29:25.187+08:00

        System.out.println(sdf3.format(timestamp));         // 2025-03-07 21:29:25

    }
}

Output:

Terminal

2025-03-07 21:29:25.187
2025-03-07 21:29:25.187
1741354165187
2025.03.07.21.29.25
2025-03-07T21:29:25.187+08:00
2025-03-07 21:29:25

4. Converting Instant to/from Timestamp

Below is an example to convert a java.sql.Timestamp to/from a java.time.Instant.

InstantTimestampConversion.java

package com.mkyong;

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

public class InstantTimestampConversion {

    public static void main(String[] args) {

        // Current timestamp
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        System.out.println("Timestamp: " + timestamp);

        // Convert Timestamp to Instant
        Instant instant = timestamp.toInstant();
        System.out.println("Instant: " + instant);

        // Convert Instant to Timestamp
        Timestamp tsFromInstant = Timestamp.from(instant);
        System.out.println("Timestamp from Instant: " + tsFromInstant);
    }

}

Output:

Terminal

Timestamp: 2025-03-07 21:42:45.496
Instant: 2025-03-07T13:42:45.496Z
Timestamp from Instant: 2025-03-07 21:42:45.496

5. JDBC Example – Insert Timestamp into Database

Here’s a practical JDBC example using timestamps:

JdbcExample.java

package com.mkyong.database;

import java.math.BigDecimal;
import java.sql.*;
import java.time.LocalDateTime;

public class JdbcExample {

    private static final String SQL_INSERT = "INSERT INTO EMPLOYEE (NAME, SALARY, CREATED_DATE) VALUES (?,?,?)";

    public static void main(String[] args) {

        try (Connection conn = DriverManager.getConnection(
                "jdbc:postgresql://127.0.0.1:5432/test", "postgres", "password");
             PreparedStatement preparedStatement = conn.prepareStatement(SQL_INSERT)) {

            preparedStatement.setString(1, "mkyong");
            preparedStatement.setBigDecimal(2, new BigDecimal("799.88"));
            preparedStatement.setTimestamp(3, Timestamp.valueOf(LocalDateTime.now()));

            // preparedStatement.setTimestamp(3, Timestamp.from(ZonedDateTime.now().toInstant()));
            // preparedStatement.setTimestamp(3, Timestamp.from(Instant.now()));

            int row = preparedStatement.executeUpdate();

            // rows affected
            System.out.println(row); //1

        } catch (SQLException e) {
            System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

6. Summary

  • Use java.time.Instant and LocalDateTime for modern Java applications.
  • Use java.sql.Timestamp when working with JDBC and databases.

7. References

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

9 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Latest Tutorials
12 years ago

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

Ajay Singh
3 years ago

Thanks Dude. Your small code snippets must have helped millions of developers. I use them all the time.

joseph
11 years ago

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

pooja
14 years ago

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
13 years ago
Reply to  pooja

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

Kareen Tariman
13 years ago

This helped. Thanks!

huzzlah
14 years ago

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
9 years ago
Reply to  huzzlah

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