ホームページ  >  記事  >  Java  >  Java での日付までのタイムスタンプ

Java での日付までのタイムスタンプ

WBOY
WBOYオリジナル
2024-08-30 16:28:48540ブラウズ

Java では、タイムスタンプは、日付と時刻の表示などのイベントが発生するたびに、フォーマットされたデータまたはエンコードされたデータを提供するためのシーケンスとして定義されます。このタイムスタンプ クラスは、Java.util ライブラリを使用して Java の日付クラスに変換できます。 Java でタイムスタンプを処理するときにインポートされます。一般に、タイムスタンプは、日付と時刻の値の両方を含む SQL タイムスタンプを表示する Java.util クラスの date クラスを使用して日付に変換されます。Java を使用してこのタイムスタンプを取得できる場合、タイムスタンプを日付に変換する方法がいくつかあります。 java.util ライブラリに属する​​ util.Date。

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

Java での日付までのタイムスタンプの構文

Java でタイムスタンプを日付に変換するには 3 つの異なる方法があります。これらのさまざまなメソッドの構文を個別に見てみましょう。

  • コンストラクターの使用: タイムスタンプを日付に変換する方法の 1 つは、java.util.Date クラスのコンストラクターを使用して行われます。このコンストラクターの構文を見てみましょう:
Date ( long l)
  • Date Reference の使用: Date クラスを拡張するタイムスタンプ クラスがあり、タイムスタンプ クラスのインスタンスが Date に割り当てられます。したがって、この説明の構文は次のようになります:
Timestamp t = new Timestamp();
Date d = t;
  • カレンダー クラスの使用: カレンダー クラスにも存在するタイムスタンプから日付を取得すると、日付も提供されます。したがって、これは構文で次のように示すことができます:
Timestamp t = new Timestamp();
Calendar c = Calendar.getInstance();
Calendar.getTime();

Java でタイムスタンプを日付に変換するには?

Java では、タイムスタンプは、タイムスタンプ クラスを使用して表示される時刻と日付の形式として定義されます。タイムスタンプ クラスは、日付とカレンダー クラスを使用するために java.util クラスを使用して実行されますが、Java にはタイムスタンプ クラスを使用するための java.sql パッケージがあります。 Java プログラムにタイムスタンプを実装します。タイムスタンプを日付に変換すると、日付と時刻の両方がミリ秒単位で表示され、通常は人間が判読できる形式になります。したがって、タイムスタンプから日付への変換の正確な作業は 3 つの異なる方法を使用して行われ、java.util または java.sql パッケージを使用するタイムスタンプ クラスを使用して行われます。

1.コンストラクター

の使用

Java には、java.util パッケージによって提供される java.util.Date クラスがあり、このクラスのコンストラクターを使用してタイムスタンプを日付に変換します。この変換メソッドの動作は、まず Date クラス コンストラクターからパラメータとして Long 値である値を取得し、getTime() メソッド

を使用して Long 値を変換するタイムスタンプ オブジェクトを生成します。
import java.sql.Timestamp;
import java.util.Date;
public class TimestampToDate
{
public static void main(String args[])
{
Timestamp t = new Timestamp(System.currentTimeMillis());
Date d = new Date(t.getTime());
System.out.println(" Demonstration  of timestamp to date conversion using date constructor : ");
System.out.println(" \n ");
System.out.println( d );
}
}

出力:

Java での日付までのタイムスタンプ

上記の例では、日付コンストラクターを使用するには Date クラスが必要で、タイムスタンプを使用するには Timestamp クラスを使用するために、最初に java.sql、Timestamp パッケージ、および java.util.Date パッケージをインポートしたことがわかります。したがって、最初に、Timestamp クラスに属する変数「t」を定義して宣言し、「new」を使用してそれを定義し、currentTimeMills() 関数を渡して、後で日付に変換されるタイムスタンプを表示します。次に、「new」を使用して定義された Date クラスの Date 変数「d」を宣言し、getTime() 関数を渡して上記のタイムスタンプを日付形式に変換します。次に、println を使用して、取得したタイムスタンプから変換された日付を表示します。上記のプログラムの出力は、上のスクリーンショットに示されているとおりです。

2.日付参照の使用

これは、java.util.date パッケージの date クラスを再度使用して、Java でタイムスタンプを日付に変換する別の方法です。このメソッドでは、タイムスタンプ インスタンスが直接日付に割り当てられる日付クラスを拡張するだけであり、したがって、タイムスタンプより前の日付形式で出力される日付オブジェクトも拡張されます。 Java でこのようなメソッドを示す例を以下に示します。

import java.sql.Timestamp;
import java.util.Date;
public class TimestampToDate
{
public static void main(String args[])
{
Timestamp t = new Timestamp(System.currentTimeMillis());
Date d = t;
System.out.println(" Demonstration  of timestamp to date conversion using date reference : ");
System.out.println(" \n ");
System.out.println( d );
}
}

出力:

Java での日付までのタイムスタンプ

上の例では、前の例と似ていますが、上のプログラムとこのプログラムの唯一の違いは、ここで日付クラスを定義する代わりに、タイムスタンプ インスタンス「t」を日付オブジェクトに直接割り当てることです。 「d」。ここでは、日付オブジェクトをタイムスタンプ インスタンスに直接割り当てているため、getTime() 関数を使用しません。そのため、出力はタイムスタンプと似ていますが、ミリ秒も含む時刻を含む日付形式です。これは上のスクリーンショットで確認できます。

3. Using Calendar Class

Another method of converting timestamp to date in java is using calendar class which is rarely used by any developers. We can obtain calendar class by the java.util.Calendar package where we use this calendar class along with which we have to use getInstance() function to get timestamp instance and getTime() to get the date along with time which will be converted from the timestamp. Let us see an example below:

Example
import java.sql.Timestamp;
import java.util.Date;
import java.util.Calendar;
public class TimestampToDate
{
public static void main(String args[])
{
Timestamp t = new Timestamp(System.currentTimeMillis());
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(t.getTime());
System.out.println(" Demonstration  of timestamp to date conversion using Calendar class : ");
System.out.println(" \n ");
System.out.println(calendar.getTime());
}
}

Output:

Java での日付までのタイムスタンプ

In the above example, we can see we have imported Calendar class also from java.util.Calendar package and this example are similar to the first example where we used date class. In this example, we define the calendar instance by using the getInstance() function which will be in the timestamp format later by using getTime() the timestamp instance is converted to a date object which results as shown in the output in the above screenshot.

Conclusion

In this article, we conclude that the timestamp to date conversion in java is very simple. In Java, there is java.util package which provides different classes for supporting this conversion in various ways with different classes. It depends on the user on which method to use according to the user’s requirement and time accuracy which displays the result accurately as required.

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

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