ホームページ  >  記事  >  Java  >  Javaインスタント

Javaインスタント

WBOY
WBOYオリジナル
2024-08-30 15:49:36532ブラウズ

Java Instant は、Java の事前定義されたメソッドのセットであり、プログラム内の特定の機能を実行するためにコード ブロックで使用できます。最も一般的に使用される Java インスタント メソッドのいくつかは、toString()、parse()、ofEpochMilli()、getEpochSecond()、getNano()、plusMillis(long milliToAdd)、plusNanos(long nanosToAdd)、plusSeconds(long SecondsToAdd)、minusSeconds です。 (long 秒ToSubtract)、minusMillis(long millisToSubtract)、minusNanos(long nanosToSubtract)、compareTo(Instant otherInstant)、isAfter(Instant otherInstant)、および isBefore(Instant otherInstant)。 Java のこの機能は、効率の高さ、優れたパフォーマンス、再利用性、最適化されたコード長などで知られています。

構文

Instant クラスは、Instant クラスのインスタンスを作成するための複数の静的ファクトリ メソッドを提供します。以下は、さまざまな標準値を持つ Instant クラスのインスタンスを取得できる静的メソッドです。

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

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

Instant instant = Instant.now();

メソッド now() は、システム クロックから現在の瞬間を返します。このインスタンスは、開始時刻または EPOCH からの合計ナノ秒数を表します。これは、1970 年 1 月 1 日の初めからカウントされた時間です。インスタントを取得するこのメソッドは、マシンのタイムスタンプを表すのに便利で、他のメソッドよりも頻繁に使用されます。

Instant instant = Instant.EPOCH;

この静的フィールドは、正確な EPOCH 時間を表す Instant クラスのインスタンスを返します。

Instant instant = Instant.MAX;

この静的フィールドは、インスタンスの最大値を表す Instant クラスのインスタンスを返します。

Instant instant = Instant.MIN;

この静的フィールドは、インスタンスの最小値を表すインスタント クラスのインスタンスを返します。値は負です。

Java Instant のメソッド

それでは、ここからが Instant クラスの主要部分または使用法です。以下は、Instant クラスに属する主なメソッドの一部のリストです。

  • toString(): このメソッドは、ISO-8601 標準を使用して文字列形式でインスタンスを表すために、Object クラスからオーバーライドされます。

以下は、Instant クラスのインスタンスを取得するために使用できるメソッドです。

  • parse(): このメソッドはテキスト文字列を引数として受け取り、渡された同じ値を表すインスタンス クラスのインスタンスを返します。文字列は UTC で即時有効である必要があります。
  • ofEpochMilli(): このメソッドは、入力長 (ミリ秒) を引数として受け取り、渡された同じ値を表すインスタンス クラスのインスタンスを返します。このメソッドは、java.util.Date オブジェクトを Instant に変換するために使用できます。

Instant クラスのインスタンスは、util.Date (ミリ秒精度) と比較して、ナノ秒精度で時間を表します。したがって、インスタントは、その値を保存するためにより多くのストレージ (ロングよりも大きい) を必要とします。したがって、インスタント クラスは秒の値を long 変数に格納し、残りのナノ秒の値を int 変数に格納します。以下のメソッドを使用して、これらの個別の値にアクセスできます。

  • getEpochSecond(): このメソッドは単に EPOCH から秒を返します。
  • getNano(): このメソッドは、タイムラインまたは秒の開始からナノ秒数を返します。

以下は瞬時の操作や計算に使用できるメソッドです。

  • plusMillis(long millisToAdd): このメソッドは、インスタントで渡された多くのミリ秒を追加し、新しいインスタントを返します。
注意: Instant クラスは不変クラスであるため、操作操作ごとに新しいインスタンスを返すことに注意してください。
  • plusNanos(long nanosToAdd): 前のものと似ていますが、ナノ秒が追加されています。
  • plusSeconds(long SecondsToAdd): 秒の加算。

マイナスまたは減算演算にも同様のメソッドが存在します。これらのメソッドは、インスタントから経過した秒数を減算し、新しいインスタントを返します。

  • minusSeconds(長い秒数を減算する)
  • minusMillis(long millisToSubtract)
  • minusNanos(long nanosToSubtract)

以下は 2 つの瞬間を比較するために使用できるメソッドです。

  • compareTo(Instant otherInstant): This method will compare the one instant with the passed one. Returns the integer value negative if it is less and positive if it is greater.
  • isAfter(Instant otherInstant): This method checks if the passed instant is after the current instant. Returns true or false.
  • isBefore(Instant otherInstant): Similar to the previous one, checks if the passed instant is before the current instant. Returns true or false.

Examples to Implement Java Instant

Below are the examples of implementing java instant:

1. toString()

Code:

import java.time.Instant;
public class app {
public static void main(String[] args) {
Instant instant = Instant.now();
System.out.println( instant.toString() );
}
}

Output:

Javaインスタント

The output will be the time of the system running the code.

2. getEpochSecond()

Code:

import java.time.Instant;
public class app {
public static void main(String[] args) {
Instant instant = Instant.now();
System.out.println( instant.getEpochSecond() );
}
}

Output:

Javaインスタント

3. getNano()

Code:

import java.time.Instant;
public class app {
public static void main(String[] args) {
Instant instant = Instant.now();
System.out.println( instant.getNano() );
}
}

Output:

Javaインスタント

4. plusSeconds()

Code:

import java.time.Instant;
public class app {
public static void main(String[] args) {
Instant instant = Instant.now();
System.out.println( instant );
instant = instant.plusSeconds( 3600 );
System.out.println( instant );
}
}

Output:

Javaインスタント

We have added exactly 3600 seconds, i.e. 1 hour, to the current instant, as it can be seen in the output that time is increased by 1 hour.

5. compareTo()

Code:

import java.time.Instant;
public class app {
public static void main(String[] args) {
Instant instant = Instant.now();
System.out.println( instant );
Instant instant2 = instant.plusSeconds( 3600 );
System.out.println( instant.compareTo( instant2 ) );
}
}

Output:

Javaインスタント

Here, we are comparing two instants, current with the instant having added 1 more hour. As the current instance is less than instance2, the output is negative.

Conclusion

So, we have seen the Instant class introduced from Java 8. This class represents the seconds from the start of the timeline with nanoseconds precision. This class is useful to generate the timestamp, which will represent the system time. We have seen different types of instants which we can get and different methods useful for calculations, creating a new instantly, comparing instants, getting seconds and nanoseconds differently, etc. Instant class is immutable and provides thread safety as compared to the old Date object. The Instant class is much more powerful than the old time-date API.

以上がJavaインスタントの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

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