SoFunction
Updated on 2025-03-08

Various ways to convert java timestamps to date format

Convert java timestamp to date format

Here are several implementation methods for converting Java timestamps to date format:

Method 1: Use Java's Date class and SimpleDateFormat class

long timestamp = 1617142526000L; // Timestamp valueDate date = new Date(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = (date);
(formattedDate);

Method 2: Use Java 8's LocalDateTime class and DateTimeFormatter class

long timestamp = 1617142526000L; // Timestamp valueInstant instant = (timestamp);
LocalDateTime date = (instant, ());
DateTimeFormatter formatter = ("yyyy-MM-dd HH:mm:ss");
String formattedDate = (formatter);
(formattedDate);

Method 3: Use the Instant class and DateTimeFormatter class in the package of Java 8

long timestamp = 1617142526000L; // Timestamp valueInstant instant = (timestamp);
DateTimeFormatter formatter = ("yyyy-MM-dd HH:mm:ss");
ZonedDateTime zonedDateTime = (instant, ());
String formattedDate = (formatter);
(formattedDate);

Method 4: Use the Instant class and DateTimeFormatter class in the Java 8 package, and use a custom time zone

long timestamp = 1617142526000L; // Timestamp valueInstant instant = (timestamp);
DateTimeFormatter formatter = ("yyyy-MM-dd HH:mm:ss");
ZonedDateTime zonedDateTime = (instant, ("Asia/Shanghai"));
String formattedDate = (formatter);
(formattedDate);

Method 5: Use the DateUtils class of the Apache Commons Lang library

import .;
import ;
long timestamp = 1617142526000L; // Timestamp valueDate date = new Date(timestamp);
String formattedDate = (date, "yyyy-MM-dd HH:mm:ss");
(formattedDate);

The above are several implementation methods for converting Java timestamps to date formats. They are all feasible and the appropriate method can be selected according to the specific needs.

This is the end of this article about converting java timestamps to date format. For more related content on converting java timestamps to date, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!