Home > Java > javaTutorial > How to Convert Epoch Timestamps to MySQL Timestamps in Java?

How to Convert Epoch Timestamps to MySQL Timestamps in Java?

DDD
Release: 2024-12-21 04:58:09
Original
509 people have browsed it

How to Convert Epoch Timestamps to MySQL Timestamps in Java?

How to Convert Epoch to MySQL Timestamp in JAVA?

The epoch timestamp is a simple numeric representation of a point in time. It is the number of seconds that have elapsed since the Unix epoch, which is January 1, 1970 at midnight Coordinated Universal Time (UTC).

In Java, you can get the current epoch timestamp as follows:

long epochNow = System.currentTimeMillis() / 1000;
Copy after login

To convert an epoch timestamp to a MySQL timestamp, you can use the following code:

String mySQLtimestamp = new Timestamp(epochTimestamp * 1000).toString();
Copy after login

For example, the following code will convert the current epoch timestamp to a MySQL timestamp:

long epochNow = System.currentTimeMillis() / 1000;
String mySQLtimestamp = new Timestamp(epochNow * 1000).toString();
Copy after login

This will produce the following output:

2013-09-23 18:57:32.123
Copy after login

Java.time

With the release of Java SE 8 in March 2014, the outdated and error-prone legacy Date-Time API (java.util Date-Time types and their formatting type, SimpleDateFormat etc.) was supplanted by java.time, the modern Date-Time API*. The following table depicts the mapping of ANSI SQL types with java.time types:

ANSI SQL Java SE 8
DATE LocalDate
TIME LocalTime
TIMESTAMP LocalDateTime
TIME WITH TIMEZONE OffsetTime
TIMESTAMP WITH TIMEZONE OffsetDateTime

The above is the detailed content of How to Convert Epoch Timestamps to MySQL Timestamps in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template