首頁  >  文章  >  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套件提供的java.util.Date類,我們將使用該類別的建構子將時間戳轉換為日期。這種轉換方法的工作原理是首先從 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