SoFunction
Updated on 2025-04-11

Three ways to pass timestamps to the front end of Java backend

1. Several ways to pass timestamps to the front end

use()

This is the easiest way to return the number of milliseconds since January 1, 1970 (UTC) and can be passed directly to the front end.

long timestamp1 = ();

use

Java 8 introduces packages that can use Instant to get timestamps that are accurate to milliseconds.

Instant now2 = ();
long timestamp2 = ();

Use LocalDateTime or ZonedDateTime

If you need more complex time processing (such as time with time zone), you can use ZonedDateTime or LocalDateTime.

ZonedDateTime now3 = ();
long timestamp3 = ().toEpochMilli();

2. Common ways and processing methods for front-end and back-end time delivery

1. Pass with timestamp (Timestamp)

Front-end—> Back-end:

Pass timestamps to the backend via parameters of JSON objects or HTTP requests.

const timestamp = (); // Get the current timestamp (milliseconds)//orconst timestamp = (() / 1000); // Get the current timestamp (seconds)

The backend receives the timestamp and converts:

long timestamp = 1695521234567L; // Millisecond-level timestamp passed by the front endInstant instant = (timestamp); // Convert to InstantLocalDateTime dateTime = (instant, ()); // Convert to local time

2. Passing using ISO 8601 format

ISO 8601 is an international standard date and time format, commonly used to pass times with time zone information. The format is similar to: 2024-09-24T14:48:00Z or 2024-09-24T14:48:00+00:00.

Front-end—> Back-end:

Pass the ISO format time to the backend via JSON object or HTTP request.

const isoTime = new Date().toISOString(); // Convert to ISO 8601 format

The backend receives ISO 8601 and parses:

String isoTime = "2024-09-24T14:48:00Z"; // ISO time string passed by the front endInstant instant = (isoTime); // parse ISO time string to InstantZonedDateTime zonedDateTime = (()); // Convert to time with time zone

3. Use formatted date strings

Sometimes, the front and back ends need to pass a custom date format (e.g. YYYY-MM-DD HH:mm:ss). This format is often used for database interaction or simplified display.

Front-end—> Back-end:

Pass the formatted time string to the backend via the HTTP requested parameter or JSON object.

const formattedDate = new Date().toLocaleString('en-GB', { timeZone: 'UTC' }); // For example: 2024-09-24 14:48:00

The backend receives and parses the formatted time string:

String formattedDate = "2024-09-24 14:48:00"; // The time string passed by the front endDateTimeFormatter formatter = ("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = (formattedDate, formatter);

Things to note

  1. Time zone issues:
    1. If the backend and frontend are in different time zones, time zone management is very important. Use ISO 8601 format to explicitly include time zone information to reduce errors.
    2. ZonedDateTime and Instant in Java 8 are more convenient to handle time with time zones, while the front-end can use() or to handle time zones.
  2. Time accuracy issues:
    1. The front-end is generally in milliseconds (()), while some back-end systems may be passed in seconds, so you need to pay attention to unit conversion.
    2. If using timestamps, make sure the front and back ends agree on the units of timestamps (milliseconds or seconds).
  3. JSON serialization problem:
    1. Sometimes the backend returns time information through JSON format, and it should ensure that the serialization format of the time is in line with expectations. You can use libraries such as Jackson to configure the serialization/deserialization format of dates.

Summarize

Common methods for passing time in front and back ends include:

  • Timestamp: Simple and convenient for time calculation, passing milliseconds or seconds.
  • ISO 8601 format: standardized time format, suitable for information transmission with time zone.
  • Custom formatted date string: suitable for display and database interaction.

This is the introduction to this article about three ways to pass timestamps to the front-end in Java back-end. For more related content about Java back-end in Java back-end, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!