ホームページ  >  記事  >  Java  >  java.util.Date

java.util.Date

王林
王林オリジナル
2024-08-30 15:49:14917ブラウズ

Java の日付と時刻は、java.util.Date クラスと呼ばれるクラスによって表されます。このクラスは、Java の時刻と日付を処理するメソッドとコンストラクタ、および Serializable、Comparable を提供します。 Cloneable インターフェースは java.util.Date クラスによって実装され、java.sql.Date、java.sql.Time、および java.sql.Timestamp インターフェースは java.util.Date クラスの基本クラスです。

java.util.Date のコンストラクター

java.util.Date クラスのコンストラクターは次のとおりです:

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

1. Date(): Date() コンストラクターを使用して現在の時刻と日付を表す日付オブジェクトが作成されます。

2. Date(long ミリ秒): 日付オブジェクトは、Date(long ミリ秒) コンストラクターを使用して、1970 年 1 月 1st 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

説明: 上記のプログラムでは、1 つの日付を格納するために dat1 という変数が定義されています。次に、別の日付を格納するために dat2 という変数が定義されます。次に、boolean after(Date date) メソッドを使用して、dat1 が dat2 の日付の後に来るかどうかを確認し、返される出力が true または false であるかどうかを確認します。次に、boolean before(Date date) メソッドを使用して、dat1 が dat2 の日付より前に来ているかどうかを確認し、返される出力が true または false であるかどうかを確認します。

3. Object clone(): object 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

説明: 上記のプログラムでは、日付クラスのインスタンスが作成されます。次に、日付クラス インスタンスで clone メソッドが呼び出され、指定された日付のクローンが作成されます。

4. intcompareTo(Date date): int CompareTo(Date date) メソッドは、現在の日付とパラメーターとして指定された日付を比較するために使用されます。現在の日付がパラメーターとして指定された日付と同じである場合、出力はゼロとして返されます。現在の日付がパラメータとして指定された日付より前の場合、出力はゼロより小さい値が返されます。現在の日付がパラメーターとして指定された日付より後の場合、出力はゼロより大きい値が返されます。

5. booleanequals(Date date): booleanquals(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

説明: 上記のプログラムでは、1 つの日付を格納するために date クラスのインスタンスが作成されます。次に、別の日付を保存するために日付クラスのインスタンスが作成されます。次に、compareTo メソッドを使用して、前に定義した 2 つの変数を使用して保存された 2 つの日付を比較します。次に、2 つの日付を比較した結果が表示されます。次に、equals メソッドを使用して、前に定義した 2 つの変数を使用して保存された 2 つの日付が等しいかどうかを確認します。

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(): string 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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。