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

Java タイムスタンプ

PHPz
PHPzオリジナル
2024-08-30 15:39:021061ブラウズ

Java Timestamp は java.util.Date の薄いラッパーに属しており、JDBC API によって SQL TIMESTAMP 型の値として認識されるようにします。タイムスタンプは、タイムスタンプ値の JDBC 構文をエスケープするためのフォーマットや解析などの操作をサポートしています。

広告 このカテゴリーの人気コース JAVA マスタリー - スペシャライゼーション | 78 コース シリーズ | 15 回の模擬テスト

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

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

この Java タイムスタンプの精度は次のいずれかであることがわかっています:

  • 19。これは、yyyy-mm-dd hh:mm:ss 形式の文字の合計数です。
  • 20 + s の精度。これは、yyyy-mm-dd hh:mm:ss 形式の文字の合計数です。s は、指定されたタイムスタンプのスケールを示し、小数秒の精度です。

注: タイムスタンプは、タイプ java.util.Date の複合に属し、別のナノ秒値を持ちます。格納される値は、java.util.Date コンポーネントの整数秒です。ナノは別々のものであり、小数秒に属します。メソッド Timestamp.equals(Object) は、java.sql.Timestamp のインスタンスに属さないオブジェクトが渡された場合、true を返しません。これは、Nanos コンポーネントが現時点では不明であるためです。これにより、java.util.Date.equals(Object) メソッドと比較して、Timestamp.equals(Object) メソッドが非対称になります。基礎となる java.util.Date 実装はハッシュコード メソッドによって使用されるため、Nano は計算に含まれません。

構文:

Timestamp(long time)

ミリ秒の時間値を使用して、タイムスタンプ値を構築します。

Java タイムスタンプのメソッド

Java でのタイムスタンプ関数の仕組み: タイムスタンプは多くのメソッドで使用でき、そのうちのいくつかについては以下で詳しく説明します。

1.後

現在の Timestamp オブジェクトが指定されたオブジェクトよりも新しいかどうかを示します。

public boolean after(Timestamp tm)

tm がこの Timestamp オブジェクトと比較する Timestamp の値である場合、この関数はブール値を返します。このタイムスタンプ値が後の場合は true、その逆の場合は true。

2.前

現在の Timestamp オブジェクトが指定されたオブジェクトの前にあるかどうかを示します。

public boolean before(Timestamp tm)

ここで、tm は Timestamp の値であり、この関数で Timestamp オブジェクトを比較するとブール値が返されます。このタイムスタンプ値が前である場合は true、その逆の場合は true。

3.比較

これは、指定された Timestamp オブジェクトとこのオブジェクトを比較する比較関数です。

public int compareTo(Timestamp tm)

tm がこの Timestamp オブジェクトと比較されるオブジェクトである場合、この関数は両方のオブジェクトが等しい場合には 0 の値を返します。このオブジェクトが指定されたオブジェクトの前にある場合は 0 より小さい値、Timestamp オブジェクトが指定された Timestamp オブジェクトの後である場合は 0 より大きい値。

4.等しい

この関数を使用すると、指定された 2 つの Timestamp オブジェクトが等しいかどうかを確認できます。

public boolean equals(Timestamp tm)

tm が Timestamp 値を参照している場合、それをこの Timestamp オブジェクトと比較する必要があります。この関数はブール値を返します。値が等しい場合は true が返され、そうでない場合は false が返されます。

5. getTime

この関数は、この Timestamp オブジェクトによって示されるデフォルトの日付値 1970 年 1 月 1 日 00:00:00 GMT から始まる合計ミリ秒数を示します。

public long getTime()

この関数は、Date クラスの getTime 関数が存在する場合、それをオーバーライドします。上記のように、デフォルトの日付からミリ秒数を返します。

6. getNanos

この関数は、Nanos でこの Timestamp オブジェクトの値を取得するために使用されます。

public int getNanos()

この関数は、オブジェクトの小数秒パラメータを返します。

7.インスタント

これは、Timestamp オブジェクトをインスタントに変更するために使用されます。この変換中に、このタイムスタンプと同じライン上の点を示すインスタントが作成されます。

public Instant toInstant()

この関数は、Date クラスの toInstant メソッドをオーバーライドします。タイムライン上の同じ点を表すインスタントを返します。

8. setTime

この関数は、1970 年 1 月 1 日 00:00:00 GMT 以降の時間をミリ秒単位で示すように Timestamp オブジェクトを設定します。

public void setTime(long time)

この関数は、Date クラスのメソッド setTime をオーバーライドします。入力パラメータの時間 (ミリ秒単位) がかかります。

9. getTime

この関数は、この Timestamp オブジェクトが表す 1970 年 1 月 1 日 00:00:00 GMT からの時間をミリ秒単位で取得するために使用されます。

public long getTime()

この関数は、上記のデフォルト時間からの時間をミリ秒単位で返します。

10.

の値

このメソッドは、JDBC タイムスタンプ エスケープ形式に属する String オブジェクトをタイムスタンプ値型に変換します。

public static Timestamp valueOf(String str)

An str parameter is the timestamp of format yyyy-[m]m-[d]d hh:mm:ss and the fractional seconds can be ignored. The leading zero given for mm and dd can also be ignored. This function returns its respective Timestamp value. And throws an IllegalArgumentException when the parameter is given is not of the mentioned format.

Examples to Implement Java Timestamp

Below are the examples of Java Timestamp:

Example #1

Code:

// Java program to demonstrate the
// functionalit of getTime() function
import java.sql.*;
class Demo {
public static void main(String args[])
{
// Here we are creating 2 timestamp objects
Timestamp tm = new Timestamp(2000);
// Displaying the created timestamp object
System.out.println("The Timestamp time is : "
+ tm.toString());
System.out.println("The Time in milliseconds is : "
+ tm.getTime());
}
}

Output:

Java タイムスタンプ

In the above example, we are first creating a timestamp object. Then we are printing out using two different functions toString and getTime to check the output.

Example #2

Code:

// Below Java code is to showcase the
// functionality of getTime() function
import java.sql.*;
public class Example {
public static void main(String args[])
{
// Creating 2 timestamp objects
Timestamp tm = new Timestamp(-2000);
// Display the timestamp object
System.out.println("Timestamp time is : "
+ tm.toString());
System.out.println("Total time in milliseconds : "
+ tm.getTime());
}
}

Output:

Java タイムスタンプ

In the above example, we are first creating 2 timestamp objects and using the getTime() function to fetch the timestamp object’s time and initialize it to a time before the default time of Jan 1 1970. Hence the negative long value given does the purpose.

Advantages of using Timestamp in Java

  • We can notice a clear separation with the Timestamp object’s help between two different time duration, especially Instant and Duration or fragment-related definitions. Ex LocalDate, LocalTime.
  • In comparison with java.Util.The date this Timestamp object allows a better set of functions for manipulating and calculating the logic.
  • It also covers the conversion of units with the help of Duration.toDays().
  • It also covers the Timezone hell with the help of ZonedDateTime.

Conclusion

As seen above, the Timestamp is an extension of java.util.Date class is used as a wrapper to that class in JDBC API to maintain specific SQL requirements. It is mostly used when working with databases and gives output in nanoseconds precision.

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

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