首頁 > Java > java教程 > 主體

java.util.Date

王林
發布: 2024-08-30 15:49:14
原創
913 人瀏覽過

java中的日期和時間由一個名為java.util.Date類別的類別來表示,該類別提供了處理java中的時間和日期的方法和建構子以及Serialized、Comparable等。和 Cloneable 介面由 java.util.Date 類別實現,java.sql.Date、java.sql.Time 和 java.sql.Timestamp 介面是 java.util.Date 類別的基底類別。

java.util.Date 的建構子

java.util.Date 類別的建構子是:

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

1。 Date(): 使用 Date() 建構子建立一個日期對象,該物件表示目前時間和日期。

2。 Date(long milliseconds): 使用 Date(long milliseconds) 建構子建立自 1st 1970 年 1 月 00:00:00 GMT 起指定毫秒數的日期物件.

java.util.Date 的方法

java.util.Date 類別的方法有:

1。 booleanafter(Date date): Boolean after(Date date) 方法用來檢查目前日期是否晚於指定日期為參數。

2。 booleanbefore(Date date): Boolean before(Date date) 方法用來檢查目前日期是否早於指定日期為 qparameter。

以下是 java.util.Date 的範例:

代碼:

import java.util.Date;
public class Example
{
public static void main(String[] args)
{
// a variable called dat1 is defined to store one date
Date dat1=new Date(2020,6,01);
//a variable called dat2 is defined to store another date
Date dat2=new Date(2021,6,01);
//boolean after(Date date) method is used to check if dat1 comes after dat2 date
System.out.println("The date represented by the variable dat1 is after the date represented by the variable dat2 : "+dat1.after(dat2));
//boolean before(Date date) method is used to check if dat1 comes before dat2 date
System.out.println("The date represented by the variable dat1 is before the date represented by the variable dat2 : "+dat1.before(dat2));
}
}
登入後複製

輸出:

java.util.Date

說明:在上面的程式中,定義了一個名為 dat1 的變數來儲存一個日期。然後定義一個名為 dat2 的變數來儲存另一個日期。然後使用 boolean after(Date date) 方法檢查 dat1 是否在 dat2 日期之後,傳回的輸出是 true 或 false。然後使用 boolean before(Date date) 方法檢查 dat1 是否早於 dat2 日期,傳回的輸出是 true 或 false。

3。物件clone():物件clone()方法用於傳回目前日期的複製物件。

以下是 java.util.Date 的範例:

代碼:

import java.util.Date;
public class Main
{
public static void main(String[] args)
{
//an instance of the date class is created
Date dat=new Date(2020,6,01);
//clone method is called on the instance of the date class to create a clone of the given date
System.out.println("The clone of the given date is : "+dat.clone());
}
}
登入後複製

輸出:

java.util.Date

說明:在上面的程式中,建立了日期類別的實例。然後在日期類別實例上呼叫克隆方法來建立給定日期的克隆。

4。 intcompareTo(Date date): intcompareTo(Date date) 方法用於將目前日期與指定為參數的日期進行比較。如果目前日期與指定為參數的日期相同,則輸出會傳回零。如果目前日期早於指定為參數的日期,則傳回的輸出小於零。如果目前日期晚於參數指定的日期,則傳回的輸出大於零。

5。 booleanquals(Date date): boolean equals(Date date) 方法用於比較目前日期與參數指定的日期是否相等。如果當前日期與參數指定的日期相同,則輸出傳回 true,否則輸出傳回 false。

以下是 java.util.Date 的範例:

代碼:

import java.util.Date;
public class Example
{
public static void main(String[] args)
{
//an instance of the date class is created to store one date
Date dat1=new Date(2020,6,01);
//an instance of the date class is created to store another date
Date dat2=new Date(2021,6,01);
//compareTo method is used to compare the two dates stored using the two variables defined before
int comp=dat1.compareTo(dat2);
//the result is displayed after comparing the two dates
System.out.println("The result after comparing the two values is : "+comp);
//Equals() method of date class is used to check if the two dates specified are equal
System.out.println("The dates stored in the two variables are equal : "+dat1.equals(dat2));
}
}
登入後複製

輸出:

java.util.Date

說明:在上面的程式中,建立了一個日期類別的實例來儲存一個日期。然後建立日期類別的實例來儲存另一個日期。然後使用compareTo方法來比較使用先前定義的兩個變數儲存的兩個日期。然後比較兩個日期後顯示結果。然後使用 equals 方法檢查使用先前定義的兩個變數儲存的兩個日期是否相等。

6。 static Date from(Instant instant): static Date from(Instant instant) 方法用於從即時日期傳回日期物件的實例。

7。 long getTime(): long getTime() 方法用來傳回日期物件表示的時間。

8。 inthashCode():int hashCode()方法用於傳回日期物件表示的雜湊碼值。

9。 Instant toInstant():Instant toInstant()方法用於將當前日期轉換為即時物件。

10。 String toString():字串 toString() 方法用於將給定日期轉換為即時物件。

以下是 java.util.Date 的範例:

代碼:

import java.time.Instant;
import java.util.Date;
public class Example
{
public static void main(String[] args)
{
//an instance of the date class is created which stores the present date
Date dat=new Date();
//instant date and time is obtained using instant.now() method
Instant inst= Instant.now();
//the instant date and time is displayed by using from() method
System.out.println("The instant date is  : "+dat.from(inst));
//getTime() method is used to obtain the number of milliseconds since January 1st 1970
System.out.println("The number of milliseconds since January 1, 1970, 00:00:00 GTM : "+dat.getTime());
//hashcode() method is used to obtain the hash code of the given date
System.out.println("The hash code for the given date is : "+dat.hashCode());
//toInstant() method is called to convert the present date to the instant object
System.out.println("The instant object after converting the present date : "+dat.toInstant());
//toString() method is used to convert the date to string
System.out.println("The date after conversion to string is : "+dat.toString());
}
}
登入後複製

輸出:

java.util.Date

Explanations: In the above program, an instance of the date class is created to store the present date. Then instant date and time are obtained using instant.now() method. Then the instant date and time are displayed by using from the () method. Then getTime() method is used to obtain the number of milliseconds since January 1st, 1970. Then hashcode() method is used to obtain the hash code of the given date. Then toInstant() method is called to convert the present date to the instant object. Then toString() method is used to convert the date to string.

11. void setTime(long time): void setTime(long time) method is used to set the present date and time to the time specified as a parameter.

Below are the examples of java.util.Date:

Code:

import java.util.Date;
public class Example
{
public static void main(String[] args)
{
//an instance of the date class is created to store one date
Date dat=new Date(2020,6,01);
long lil=2000;
//settime() method is used to set the time to the given time specified
dat.setTime(lil);
System.out.println("The time after setting the time to given time is : "+dat.toString());
}
}
登入後複製

Output:

java.util.Date

Explanations: In the above program, an instance of the date class is created to store one date. Then setTime() method id used to set the present time to the given date.

Conclusion

In this tutorial, we understand the concept of java.util.Date in Java through definition, its constructors and methods through programming examples and their outputs.

以上是java.util.Date的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!