The timestamp in Java represents the number of milliseconds that have passed since 1970. The types include the current timestamp (System.currentTimeMillis()) and the specified timestamp (Instant.ofEpochMilli(long millis)). Can be converted to a readable string through the SimpleDateFormat class and used in logging events, sorting, creating unique identifiers, caches, and databases.
Time stamp in java
Time stamp in Java represents the since Unix epoch (January 1, 1970 The number of milliseconds since 00:00:00 GMT). It is a long (64-bit) number used to accurately record the time an event occurred.
Types and acquisition methods
There are two main types of timestamps in Java:
System.currentTimeMillis()
method to obtain and return the number of milliseconds since the Unix epoch of the current system time. Instant.ofEpochMilli(long millis)
method to create a specified timestamp. The millis parameter represents the number of milliseconds elapsed since the Unix epoch. Conversion and formatting
Time stamps can be converted into readable time strings through the SimpleDateFormatter
class. For example:
<code class="java">SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formattedDate = sdf.format(new Date(timestamp));</code>
Uses
Time stamps in Java have a wide range of uses, including:
Usage examples
The following example shows how to use timestamps in Java:
<code class="java">// 获取当前时间戳 long currentTimeMillis = System.currentTimeMillis(); // 创建指定时间戳(2023年1月1日00:00:00 GMT) long epochMillis = 1640995200000L; Instant instant = Instant.ofEpochMilli(epochMillis); // 转换时间戳为字符串 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formattedDate = sdf.format(new Date(currentTimeMillis));</code>
The above is the detailed content of What does timestamp mean in java. For more information, please follow other related articles on the PHP Chinese website!