搜索

Java时间戳

Aug 30, 2024 pm 03:39 PM
java

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应用程序受益?平台独立性如何使企业级的Java应用程序受益?May 03, 2025 am 12:23 AM

Java在企业级应用中被广泛使用是因为其平台独立性。1)平台独立性通过Java虚拟机(JVM)实现,使代码可在任何支持Java的平台上运行。2)它简化了跨平台部署和开发流程,提供了更大的灵活性和扩展性。3)然而,需注意性能差异和第三方库兼容性,并采用最佳实践如使用纯Java代码和跨平台测试。

考虑到平台独立性,Java在物联网(物联网)设备的开发中扮演什么角色?考虑到平台独立性,Java在物联网(物联网)设备的开发中扮演什么角色?May 03, 2025 am 12:22 AM

JavaplaysigantroleiniotduetoitsplatFormentence.1)itallowscodeTobewrittenOnCeandrunonVariousDevices.2)Java'secosystemprovidesuseusefidesusefidesulylibrariesforiot.3)

描述一个方案,您在Java中遇到了一个特定于平台的问题以及如何解决。描述一个方案,您在Java中遇到了一个特定于平台的问题以及如何解决。May 03, 2025 am 12:21 AM

ThesolutiontohandlefilepathsacrossWindowsandLinuxinJavaistousePaths.get()fromthejava.nio.filepackage.1)UsePaths.get()withSystem.getProperty("user.dir")andtherelativepathtoconstructthefilepath.2)ConverttheresultingPathobjecttoaFileobjectifne

Java平台独立对开发人员有什么好处?Java平台独立对开发人员有什么好处?May 03, 2025 am 12:15 AM

Java'splatFormIndenceistificantBecapeitAllowSitallowsDevelostWriTecoDeonCeandRunitonAnyPlatFormwithAjvm.this“ writeonce,runanywhere”(era)橱柜橱柜:1)交叉plat formcomplibility cross-platformcombiblesible,enablingDeploymentMentMentMentMentAcrAptAprospOspOspOssCrossDifferentoSswithOssuse; 2)

将Java用于需要在不同服务器上运行的Web应用程序的优点是什么?将Java用于需要在不同服务器上运行的Web应用程序的优点是什么?May 03, 2025 am 12:13 AM

Java适合开发跨服务器web应用。1)Java的“一次编写,到处运行”哲学使其代码可在任何支持JVM的平台上运行。2)Java拥有丰富的生态系统,包括Spring和Hibernate等工具,简化开发过程。3)Java在性能和安全性方面表现出色,提供高效的内存管理和强大的安全保障。

JVM如何促进Java的'写作一次,在任何地方运行”(WORA)功能?JVM如何促进Java的'写作一次,在任何地方运行”(WORA)功能?May 02, 2025 am 12:25 AM

JVM通过字节码解释、平台无关的API和动态类加载实现Java的WORA特性:1.字节码被解释为机器码,确保跨平台运行;2.标准API抽象操作系统差异;3.类在运行时动态加载,保证一致性。

Java的较新版本如何解决平台特定问题?Java的较新版本如何解决平台特定问题?May 02, 2025 am 12:18 AM

Java的最新版本通过JVM优化、标准库改进和第三方库支持有效解决平台特定问题。1)JVM优化,如Java11的ZGC提升了垃圾回收性能。2)标准库改进,如Java9的模块系统减少平台相关问题。3)第三方库提供平台优化版本,如OpenCV。

说明JVM执行的字节码验证的过程。说明JVM执行的字节码验证的过程。May 02, 2025 am 12:18 AM

JVM的字节码验证过程包括四个关键步骤:1)检查类文件格式是否符合规范,2)验证字节码指令的有效性和正确性,3)进行数据流分析确保类型安全,4)平衡验证的彻底性与性能。通过这些步骤,JVM确保只有安全、正确的字节码被执行,从而保护程序的完整性和安全性。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。