SoFunction
Updated on 2025-03-02

Detailed explanation of various operation methods for timestamping in Java

1. What is Java timestamp

In Java, timestamps usually refer to midnight on January 1, 1970 (UTC) the number of milliseconds since.

This concept is mainly done in JavaClass andClass to represent

In Java 8 and later versions, new dates and times have been introducedAPI,Right nowPackage, providing more functionality and flexibility.

2. Get the current time stamp

1. Use the () method

long currentTimestamp = ();

2. Use Class

Classes can be used to represent specific moments, which contain midnight since January 1, 1970 (UTC) the number of milliseconds since.

// Create a Date object to represent the current timeDate now = new Date();
long timestamp = (); // Get the timestamp
// Create a Date object from a known timestamplong timestamp = ... // Suppose you have a timestampDate date = new Date(timestamp);

3. Use Class

Class isa subclass of which provides higher precision time representations including nanoseconds

// Create a Timestamp object to represent the current timeTimestamp now = new Timestamp(());

// Create Timestamp object from a known timestamplong timestamp = ... // Suppose you have a timestampTimestamp ts = new Timestamp(timestamp);

Packages in 8

Java 8 introduces a new datetime API that provides better classes for handling dates and times. in,InstantClasses can be used to represent timestamps.

// Get the Instant object of the current timeInstant now = ();
long timestamp = (); // Get the timestamp
// Create an Instant object from a known timestamplong timestamp = ... // Suppose you have a timestampInstant instant = (timestamp);

3. Format the timestamp

To convert a timestamp to a readable datetime format, you can useSimpleDateFormat(Before Java 8) orDateTimeFormatter(Java 8 and later).

1. UseSimpleDateFormat

// Suppose you have a timestamplong timestamp = ...

// Create SimpleDateFormat objectSimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

// Convert timestamps to Date objects and format themString formattedDate = (new Date(timestamp));
(formattedDate);

2. Use DateTimeFormatter

// Suppose you have a timestamplong timestamp = ...

// Create a DateTimeFormatter objectDateTimeFormatter dtf = ("yyyy-MM-dd HH:mm:ss");

// Convert timestamps to Instant objects, then convert them to LocalDateTime objects, and finally format themInstant instant = (timestamp);
LocalDateTime ldt = (instant, ());
String formattedDate = (dtf);
(formattedDate);

4. Conversion of timestamps and strings

1. Convert timestamp to string

// Suppose you have a timestamplong timestamp = ...

// Use SimpleDateFormatSimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = (new Date(timestamp));

// Use DateTimeFormatterDateTimeFormatter dtf = ("yyyy-MM-dd HH:mm:ss");
Instant instant = (timestamp);
LocalDateTime ldt = (instant, ());
String dateStr = (dtf);

2. Convert string to timestamp

// Suppose you have a string representation of date and timeString dateStr = "2023-04-01 12:34:56";

// Use SimpleDateFormatSimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
    Date date = (dateStr);
    long timestamp = ();
} catch (ParseException e) {
    ();
}

// Use DateTimeFormatterDateTimeFormatter dtf = ("yyyy-MM-dd HH:mm:ss");
LocalDateTime ldt = (dateStr, dtf);
Instant instant = (()).toInstant();
long timestamp = ();

5. Time zone processing

Time zone is a very important factor when dealing with timestamps. In JavaZoneIdClasses can be used to represent different time zones.

// Get the offset of the current time zoneZoneId currentZone = ();
ZonedDateTime zdt = (currentZone);
long offsetInMillis = ().getTotalSeconds() * 1000;

6. Addition and subtraction of timestamps

To add or subtract the timestamp, you can useClass or directly add or subtract milliseconds.

// Suppose you have a timestamplong timestamp = ...

// Add one hour time stampInstant instant = (timestamp);
Duration oneHour = (1);
Instant newInstant = (oneHour);
long newTimestamp = ();

// Or directly add or subtract the millisecondslong newTimestamp = timestamp + (60 * 60 * 1000); // One hour added

Summarize

This is the end of this article about the detailed explanation of various timestamp operations in Java. For more related timestamp content in Java, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!