目次
java.util.Date のコンストラクター
java.util.Date のメソッド
Conclusion

java.util.Date

Aug 30, 2024 pm 03:49 PM
java

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 までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

PHP対Python:違いを理解します PHP対Python:違いを理解します Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHPは、シンプルな構文と高い実行効率を備えたWeb開発に適しています。 2。Pythonは、簡潔な構文とリッチライブラリを備えたデータサイエンスと機械学習に適しています。

PHP:Web開発の重要な言語 PHP:Web開発の重要な言語 Apr 13, 2025 am 12:08 AM

PHPは、サーバー側で広く使用されているスクリプト言語で、特にWeb開発に適しています。 1.PHPは、HTMLを埋め込み、HTTP要求と応答を処理し、さまざまなデータベースをサポートできます。 2.PHPは、ダイナミックWebコンテンツ、プロセスフォームデータ、アクセスデータベースなどを生成するために使用され、強力なコミュニティサポートとオープンソースリソースを備えています。 3。PHPは解釈された言語であり、実行プロセスには語彙分析、文法分析、編集、実行が含まれます。 4.PHPは、ユーザー登録システムなどの高度なアプリケーションについてMySQLと組み合わせることができます。 5。PHPをデバッグするときは、error_reporting()やvar_dump()などの関数を使用できます。 6. PHPコードを最適化して、キャッシュメカニズムを使用し、データベースクエリを最適化し、組み込み関数を使用します。 7

Java 8 Stream Foreachから休憩または戻ってきますか? Java 8 Stream Foreachから休憩または戻ってきますか? Feb 07, 2025 pm 12:09 PM

Java 8は、Stream APIを導入し、データ収集を処理する強力で表現力のある方法を提供します。ただし、ストリームを使用する際の一般的な質問は次のとおりです。 従来のループにより、早期の中断やリターンが可能になりますが、StreamのForeachメソッドはこの方法を直接サポートしていません。この記事では、理由を説明し、ストリーム処理システムに早期終了を実装するための代替方法を調査します。 さらに読み取り:JavaストリームAPIの改善 ストリームを理解してください Foreachメソッドは、ストリーム内の各要素で1つの操作を実行する端末操作です。その設計意図はです

PHP対その他の言語:比較 PHP対その他の言語:比較 Apr 13, 2025 am 12:19 AM

PHPは、特に迅速な開発や動的なコンテンツの処理に適していますが、データサイエンスとエンタープライズレベルのアプリケーションには良くありません。 Pythonと比較して、PHPはWeb開発においてより多くの利点がありますが、データサイエンスの分野ではPythonほど良くありません。 Javaと比較して、PHPはエンタープライズレベルのアプリケーションでより悪化しますが、Web開発により柔軟性があります。 JavaScriptと比較して、PHPはバックエンド開発により簡潔ですが、フロントエンド開発のJavaScriptほど良くありません。

PHP対Python:コア機能と機能 PHP対Python:コア機能と機能 Apr 13, 2025 am 12:16 AM

PHPとPythonにはそれぞれ独自の利点があり、さまざまなシナリオに適しています。 1.PHPはWeb開発に適しており、組み込みのWebサーバーとRich Functionライブラリを提供します。 2。Pythonは、簡潔な構文と強力な標準ライブラリを備えたデータサイエンスと機械学習に適しています。選択するときは、プロジェクトの要件に基づいて決定する必要があります。

PHPの影響:Web開発など PHPの影響:Web開発など Apr 18, 2025 am 12:10 AM

phphassiblasifly-impactedwebdevevermentandsbeyondit.1)itpowersmajorplatformslikewordpratsandexcelsindatabase interactions.2)php'sadaptableability allowsitale forlargeapplicationsusingframeworkslikelavel.3)

PHP:多くのウェブサイトの基礎 PHP:多くのウェブサイトの基礎 Apr 13, 2025 am 12:07 AM

PHPが多くのWebサイトよりも優先テクノロジースタックである理由には、その使いやすさ、強力なコミュニティサポート、広範な使用が含まれます。 1)初心者に適した学習と使用が簡単です。 2)巨大な開発者コミュニティと豊富なリソースを持っています。 3)WordPress、Drupal、その他のプラットフォームで広く使用されています。 4)Webサーバーとしっかりと統合して、開発の展開を簡素化します。

PHP対Python:ユースケースとアプリケーション PHP対Python:ユースケースとアプリケーション Apr 17, 2025 am 12:23 AM

PHPはWeb開発およびコンテンツ管理システムに適しており、Pythonはデータサイエンス、機械学習、自動化スクリプトに適しています。 1.PHPは、高速でスケーラブルなWebサイトとアプリケーションの構築においてうまく機能し、WordPressなどのCMSで一般的に使用されます。 2。Pythonは、NumpyやTensorflowなどの豊富なライブラリを使用して、データサイエンスと機械学習の分野で驚くほどパフォーマンスを発揮しています。

See all articles