首页  >  文章  >  Java  >  Java时间戳

Java时间戳

PHPz
PHPz原创
2024-08-30 15:39:021062浏览

Java Timestamp 属于 java.util.Date 的瘦包装器,并允许 JDBC API 将其识别为 SQL TIMESTAMP 类型值。 Timestamp 支持格式化和解析等操作,以帮助转义时间戳值的 JDBC 语法。

广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

已知此 Java 时间戳的精度为:

  • 19,即 yyyy-mm-dd hh:mm:ss 格式的字符总数。
  • 20 + s 精度,即格式为 yyyy-mm-dd hh:mm:ss 的字符总数,其中 s 表示给定时间戳的小数位数,即小数秒精度。

注意: 时间戳属于 java.util.Date 类型的组合,并且具有单独的纳秒值。 java.util.Date 组件中存储的值是整数秒。 Nano 是独立的,属于小数秒。当传递一个不属于java.sql.Timestamp实例的对象时,Timestamp.equals(Object)方法不会返回true。这是因为 Nanos 组件迄今为止尚不清楚。与 java.util.Date.equals(Object) 方法相比,这导致 Timestamp.equals(Object) 方法不对称。 hashcode 方法将使用底层 java.util.Date 实现,因此 Nanos 不会包含在计算中。

语法:

Timestamp(long time)

使用毫秒时间值,构造一个时间戳值。

Java 时间戳的方法

Java中时间戳函数的工作原理:时间戳可以在很多方法中使用,下面详细解释其中的一些方法-

1.之后

显示当前时间戳对象是否晚于给定对象。

public boolean after(Timestamp tm)

其中tm是Timestamp的值,我们将其与这个Timestamp对象进行比较,该函数返回一个布尔值;如果此时间戳值较晚则为 true,反之亦然。

2.之前

显示当前时间戳对象是否早于给定对象。

public boolean before(Timestamp tm)

其中 tm 是 Timestamp 的值,我们将 Timestamp 对象与此函数进行比较,返回一个布尔值;如果此时间戳值之前,则为 true,反之亦然。

3.比较

这是一个比较函数,用于将给定的 Timestamp 对象与此对象进行比较。

public int compareTo(Timestamp tm)

其中 tm 是要与此 Timestamp 对象进行比较的对象,当两个对象相等时,该函数返回 0 值;当该对象位于给定对象之前时,为任何小于 0 的值;当 Timestamp 对象位于给定时间戳对象之后时,为大于 0 的值。

4.等于

使用此函数,我们可以检查两个给定的 Timestamp 对象是否相等。

public boolean equals(Timestamp tm)

其中 tm 指的是 Timestamp 值,我们必须将它与这个 Timestamp 对象进行比较。该函数返回布尔值;如果值相等则返回 true,否则返回 false。

5.获取时间

此函数给出从默认日期值 1970 年 1 月 1 日 00:00:00 GMT 开始的总毫秒数,该值由此 Timestamp 对象指示。

public long getTime()

此函数将覆盖 Date 类的 getTime 函数(如果存在)。它返回距离默认日期的毫秒数,如上所示。

6.获取纳米

此函数用于获取此 Timestamp 对象的 Nanos 值。

public int getNanos()

此函数返回对象的小数第二个参数。

7.立即

这用于将 Timestamp 对象更改为 Instant。在这个转换过程中,会创建一个 Instant,它表示线上的点,与这个 Timestamp 相同。

public Instant toInstant()

该函数重写了 Date 类的 toInstant 方法。它返回代表时间线上同一点的瞬间。

8.设置时间

此函数设置一个 Timestamp 对象来指示 1970 年 1 月 1 日 00:00:00 GMT 之后的时间(以毫秒为单位)。

public void setTime(long time)

该函数重写了 Date 类中的 setTime 方法。需要输入参数time,即毫秒数。

9.获取时间

此函数用于获取此 Timestamp 对象所代表的自 1970 年 1 月 1 日 00:00:00 GMT 以来的毫秒数。

public long getTime()

此函数返回自上述默认时间以来的时间(以毫秒为单位)。

10。值

该方法将属于 JDBC 时间戳转义格式的 String 对象转换为 Timestamp 值类型。

public static Timestamp valueOf(String str)

An str parameter is the timestamp of format yyyy-[m]m-[d]d hh:mm:ss and the fractional seconds can be ignored. The leading zero given for mm and dd can also be ignored. This function returns its respective Timestamp value. And throws an IllegalArgumentException when the parameter is given is not of the mentioned format.

Examples to Implement Java Timestamp

Below are the examples of Java Timestamp:

Example #1

Code:

// Java program to demonstrate the
// functionalit of getTime() function
import java.sql.*;
class Demo {
public static void main(String args[])
{
// Here we are creating 2 timestamp objects
Timestamp tm = new Timestamp(2000);
// Displaying the created timestamp object
System.out.println("The Timestamp time is : "
+ tm.toString());
System.out.println("The Time in milliseconds is : "
+ tm.getTime());
}
}

Output:

Java时间戳

In the above example, we are first creating a timestamp object. Then we are printing out using two different functions toString and getTime to check the output.

Example #2

Code:

// Below Java code is to showcase the
// functionality of getTime() function
import java.sql.*;
public class Example {
public static void main(String args[])
{
// Creating 2 timestamp objects
Timestamp tm = new Timestamp(-2000);
// Display the timestamp object
System.out.println("Timestamp time is : "
+ tm.toString());
System.out.println("Total time in milliseconds : "
+ tm.getTime());
}
}

Output:

Java时间戳

In the above example, we are first creating 2 timestamp objects and using the getTime() function to fetch the timestamp object’s time and initialize it to a time before the default time of Jan 1 1970. Hence the negative long value given does the purpose.

Advantages of using Timestamp in Java

  • We can notice a clear separation with the Timestamp object’s help between two different time duration, especially Instant and Duration or fragment-related definitions. Ex LocalDate, LocalTime.
  • In comparison with java.Util.The date this Timestamp object allows a better set of functions for manipulating and calculating the logic.
  • It also covers the conversion of units with the help of Duration.toDays().
  • It also covers the Timezone hell with the help of ZonedDateTime.

Conclusion

As seen above, the Timestamp is an extension of java.util.Date class is used as a wrapper to that class in JDBC API to maintain specific SQL requirements. It is mostly used when working with databases and gives output in nanoseconds precision.

以上是Java时间戳的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Java min()下一篇:Java URLConnection