首页  >  文章  >  Java  >  Java 中的时间戳至今

Java 中的时间戳至今

WBOY
WBOY原创
2024-08-30 16:28:48540浏览

在Java中,时间戳被定义为一个序列,用于在任何事件发生时提供格式化或编码的数据,例如显示日期和时间,其中可以使用Java.util库将该时间戳类转换为java中的日期类,该库需要在 Java 中处理时间戳时导入。一般来说,使用Java.util类的日期类将时间戳转换为日期,用于显示包含日期和时间值的SQL时间戳,并且有多种方法将时间戳转换为日期,其中可以使用java获取时间戳。 util.Date 属于 java.util 库。

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

Java 中时间戳到日期的语法

Java 中有 3 种不同的方法将时间戳转换为日期。让我们分别看看这些不同方法的语法。

  • 使用构造函数: 将时间戳转换为日期的一种方法是使用 java.util.Date 类的构造函数来完成。让我们看看这个构造函数的语法:
Date ( long l)
  • 使用日期参考:其中有时间戳类扩展了日期类,其中时间戳类的实例被分配给日期。因此,该描述的语法如下:
Timestamp t = new Timestamp();
Date d = t;
  • 使用日历类:要从时间戳获取日期,日历类中也存在日期,也将提供日期。因此,这可以用语法来证明:
Timestamp t = new Timestamp();
Calendar c = Calendar.getInstance();
Calendar.getTime();

如何在 Java 中将时间戳转换为日期?

在Java中,时间戳被定义为使用时间戳类显示的时间和日期格式,这是使用java.util类来使用日期和日历类来完成的,而在java中有java.sql包用于使用时间戳类在java程序中实现时间戳。将时间戳转换为日期将导致以毫秒为单位显示日期和时间,这通常是人类可读的格式。因此,时间戳到日期转换的精确工作是使用 3 种不同的方式完成的,并且是使用 java.util 或 java.sql 包的时间戳类完成的。

1.使用构造函数

在Java中,有java.util.Date类,它由java.util包提供,我们将使用该类的构造函数将时间戳转换为日期。这种转换方法的工作原理是首先从 Date 类构造函数中获取 long 值作为参数,并生成时间戳对象,该对象将使用 getTime() 方法转换 long 值

示例
import java.sql.Timestamp;
import java.util.Date;
public class TimestampToDate
{
public static void main(String args[])
{
Timestamp t = new Timestamp(System.currentTimeMillis());
Date d = new Date(t.getTime());
System.out.println(" Demonstration  of timestamp to date conversion using date constructor : ");
System.out.println(" \n ");
System.out.println( d );
}
}

输出:

Java 中的时间戳至今

在上面的示例中,我们可以看到我们首先导入了 java.sql、Timestamp 包和 java.util.Date 包,为了使用日期构造函数,我们需要 Date 类,为了使用时间戳,我们使用 Timestamp 类。因此,首先我们定义并声明一个属于 Timestamp 类的变量“t”,并使用“new”定义它,并传递 currentTimeMills() 函数来显示时间戳,稍后将其转换为日期。然后我们声明 Date 类“d”的 Date 变量,该变量是使用“new”定义的,其中我们传递 getTime() 函数以将上述时间戳转换为日期格式。然后使用 println 我们将显示从获得的时间戳转换而来的日期。上述程序的输出如上图所示。

2.使用日期参考

这是 Java 中将时间戳转换为日期的另一种方法,再次使用 java.util.date 包的日期类。在此方法中,它只是扩展了日期类,其中时间戳实例直接分配给日期,因此日期对象以时间戳中较早的日期格式作为输出。让我们看下面的例子来在java中演示这种方法:

示例
import java.sql.Timestamp;
import java.util.Date;
public class TimestampToDate
{
public static void main(String args[])
{
Timestamp t = new Timestamp(System.currentTimeMillis());
Date d = t;
System.out.println(" Demonstration  of timestamp to date conversion using date reference : ");
System.out.println(" \n ");
System.out.println( d );
}
}

输出:

Java 中的时间戳至今

在上面的例子中,我们可以看到它与前面的例子类似,但上面的程序和这个程序的唯一区别是,它不是在这里定义日期类,而是直接将时间戳实例“t”分配给日期对象“d”,这里我们不使用 getTime() 函数,因为我们直接将日期对象分配给时间戳实例,因此这里的输出看起来类似于时间戳,但它是日期格式,时间也包括毫秒。这可以在上面的屏幕截图中看到。

3. Using Calendar Class

Another method of converting timestamp to date in java is using calendar class which is rarely used by any developers. We can obtain calendar class by the java.util.Calendar package where we use this calendar class along with which we have to use getInstance() function to get timestamp instance and getTime() to get the date along with time which will be converted from the timestamp. Let us see an example below:

Example
import java.sql.Timestamp;
import java.util.Date;
import java.util.Calendar;
public class TimestampToDate
{
public static void main(String args[])
{
Timestamp t = new Timestamp(System.currentTimeMillis());
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(t.getTime());
System.out.println(" Demonstration  of timestamp to date conversion using Calendar class : ");
System.out.println(" \n ");
System.out.println(calendar.getTime());
}
}

Output:

Java 中的时间戳至今

In the above example, we can see we have imported Calendar class also from java.util.Calendar package and this example are similar to the first example where we used date class. In this example, we define the calendar instance by using the getInstance() function which will be in the timestamp format later by using getTime() the timestamp instance is converted to a date object which results as shown in the output in the above screenshot.

Conclusion

In this article, we conclude that the timestamp to date conversion in java is very simple. In Java, there is java.util package which provides different classes for supporting this conversion in various ways with different classes. It depends on the user on which method to use according to the user’s requirement and time accuracy which displays the result accurately as required.

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

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