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:
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:
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中文网其他相关文章!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要整理了Stream流的概念和使用的相关问题,包括了Stream流的概念、Stream流的获取、Stream流的常用方法等等内容,下面一起来看一下,希望对大家有帮助。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Dreamweaver Mac版
视觉化网页开发工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Dreamweaver CS6
视觉化网页开发工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3 Linux新版
SublimeText3 Linux最新版