This article shows few Java examples to get the current date time or timestamp in Java. (Updated with Java 8).
Code snippets
// 2021-03-24 16:48:05.591 Timestamp timestamp = new Timestamp(System.currentTimeMillis()); // 2021-03-24 16:48:05.591 Date date = new Date(); Timestamp timestamp2 = new Timestamp(date.getTime()); // convert Instant to Timestamp Timestamp ts = Timestamp.from(Instant.now()) // convert ZonedDateTime to Instant to Timestamp Timestamp ts = Timestamp.from(ZonedDateTime.now().toInstant())); // convert Timestamp to Instant Instant instant = ts.toInstant();
1. Java Timestamp examples
The below program uses java.sql.Timestamp
to get the current timestamp and format the display with SimpleDateFormat
.
TimeStampExample.java
package com.favtuts.time; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.Date; public class CurrentTimestamp { // 2021.03.24.16.34.26 private static final SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss"); // 2021-03-24T16:44:39.083+08:00 private static final SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); // 2021-03-24 16:48:05 private static final SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static void main(String[] args) { currentTimestampWithJavaSqlTimestamp(); } static void currentTimestampWithJavaSqlTimestamp() { // method 1 Timestamp timestamp = new Timestamp(System.currentTimeMillis()); System.out.println(timestamp); // 2021-03-24 16:34:26.666 // method 2 - via Date Date date = new Date(); System.out.println(new Timestamp(date.getTime())); // 2021-03-24 16:34:26.666 // number of milliseconds since January 1, 1970, 00:00:00 GMT System.out.println(timestamp.getTime()); // 1616574866666 System.out.println(sdf1.format(timestamp)); // 2021.03.24.16.34.26 System.out.println(sdf2.format(timestamp)); // 2021-03-24T16:48:05.591+08:00 System.out.println(sdf3.format(timestamp)); // 2021-03-24 16:48:05 } }
Output
2022-06-04 10:57:32.247
2022-06-04 10:57:32.247
1654315052247
2022.06.04.10.57.32
2022-06-04T10:57:32.247+07:00
2022-06-04 10:57:32
2. Convert Instant to/from Timestamp
This example shows how to convert the new Java 8 java.time.Instant
to and from the legacy java.sql.Timestamp
.
// convert Instant to Timestamp
Timestamp ts = Timestamp.from(Instant.now())
// convert Timestamp to Instant
Instant instant = ts.toInstant();
InstantExample.java
package com.favtuts.time; import java.sql.Timestamp; import java.time.Instant; public class CurrentTimestamp { public static void main(String[] args) { convertInstantToFromTimestamp(); } static void convertInstantToFromTimestamp() { Timestamp timestamp = new Timestamp(System.currentTimeMillis()); System.out.println(timestamp); // 2021-03-24 17:12:03.311 System.out.println(timestamp.getTime()); // 1616577123311 // Convert Timestamp to Instant Instant instant = timestamp.toInstant(); System.out.println(instant); // 2021-03-24T09:12:03.311Z System.out.println(instant.toEpochMilli()); // 1616577123311 // Convert Instant to Timestamp Timestamp tsFromInstant = Timestamp.from(instant); System.out.println(tsFromInstant.getTime()); // 1616577123311 } }
Output
2022-06-04 11:06:22.038
1654315582038
2022-06-04T04:06:22.038Z
1654315582038
1654315582038
3. Insert Timestamp into a table
The java.sql.Timestamp
is still widely used in JDBC programming. See the below conversions:
// Java 8, java.time.* // convert LocalDateTime to Timestamp preparedStatement.setTimestamp(1, Timestamp.valueOf(LocalDateTime.now())); // convert Instant to Timestamp preparedStatement.setTimestamp(1, Timestamp.from(Instant.now())); // Convert ZonedDateTime to Instant to Timestamp preparedStatement.setTimestamp(3, Timestamp.from(ZonedDateTime.now().toInstant()));
The below example is a JDBC example of inserting a Timestamp
into the table.
JdbcExample.java
package com.favtuts.time; import java.math.BigDecimal; import java.sql.*; import java.time.LocalDateTime; public class CurrentTimestamp { private static final String SQL_INSERT = "INSERT INTO EMPLOYEE (NAME, SALARY, CREATED_DATE) VALUES (?,?,?)"; public static void main(String[] args) { convertInstantToFromTimestamp(); } static void insertTimestampIntoTable() { try (Connection conn = DriverManager.getConnection( "jdbc:postgresql://127.0.0.1:5432/test", "postgres", "password"); PreparedStatement preparedStatement = conn.prepareStatement(SQL_INSERT)) { preparedStatement.setString(1, "favtuts"); 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(); } } }
Note
More examples to get current date time or timestamp in Java.
Download Source Code
$ git clone https://github.com/favtuts/java-core-tutorials-examples
$ cd java-basic/time