「java」afterメソッド、beforeメソッドでTimestampの比較サンプル

環境
JDK1.8
Eclipse2019-12

書式
1.public boolean after(Timestamp ts)
このTimestampオブジェクトが、指定されたTimestampオブジェクトより遅い時間かどうかを示します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
パラメータ:ts - 比較対象のTimestamp値
戻り値:このTimestampオブジェクトの方が遅い場合はtrue、そうでない場合はfalse
パラメータ:ts - 比較対象のTimestamp値 戻り値:このTimestampオブジェクトの方が遅い場合はtrue、そうでない場合はfalse
パラメータ:ts - 比較対象のTimestamp値
戻り値:このTimestampオブジェクトの方が遅い場合はtrue、そうでない場合はfalse

2.public boolean before(Timestamp ts)
このTimestampオブジェクトが、指定されたTimestampオブジェクトより早い時間かどうかを示します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
パラメータ:ts - 比較対象のTimestamp値
戻り値:このTimestampオブジェクトの方が早い場合はtrue、そうでない場合はfalse
パラメータ:ts - 比較対象のTimestamp値 戻り値:このTimestampオブジェクトの方が早い場合はtrue、そうでない場合はfalse
パラメータ:ts - 比較対象のTimestamp値
戻り値:このTimestampオブジェクトの方が早い場合はtrue、そうでない場合はfalse

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.stud;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class TimestampDemo {
//日付のフォーマット
public static final String PTN = "yyyy/MM/dd HH:mm:ss";
public static void main(String[] args) throws ParseException {
String strA = "2022/04/02 18:29:07";
SimpleDateFormat smpDF = new SimpleDateFormat(PTN);
// Timestampオブジェクト生成
Timestamp tmpA = new Timestamp(smpDF.parse(strA).getTime());
String strB = "2022/04/03 18:29:07";
SimpleDateFormat smDFt2 = new SimpleDateFormat(PTN);
// Timestampオブジェクト生成
Timestamp tmpB = new Timestamp(smDFt2.parse(strB).getTime());
System.out.println("afterまたはbeforeメソッドでTimestampを比較する");
// Timestampの比較(after/before)
System.out.println("afterでTimeStampの比較結果: " + tmpA.after(tmpB));
System.out.println("beforeでTimeStampの比較結果: " + tmpA.before(tmpB));
}
}
package com.arkgame.stud; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; public class TimestampDemo { //日付のフォーマット public static final String PTN = "yyyy/MM/dd HH:mm:ss"; public static void main(String[] args) throws ParseException { String strA = "2022/04/02 18:29:07"; SimpleDateFormat smpDF = new SimpleDateFormat(PTN); // Timestampオブジェクト生成 Timestamp tmpA = new Timestamp(smpDF.parse(strA).getTime()); String strB = "2022/04/03 18:29:07"; SimpleDateFormat smDFt2 = new SimpleDateFormat(PTN); // Timestampオブジェクト生成 Timestamp tmpB = new Timestamp(smDFt2.parse(strB).getTime()); System.out.println("afterまたはbeforeメソッドでTimestampを比較する"); // Timestampの比較(after/before) System.out.println("afterでTimeStampの比較結果: " + tmpA.after(tmpB)); System.out.println("beforeでTimeStampの比較結果: " + tmpA.before(tmpB)); } }
package com.arkgame.stud;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class TimestampDemo {
      //日付のフォーマット
      public static final String PTN = "yyyy/MM/dd HH:mm:ss";

      public static void main(String[] args) throws ParseException {
            String strA = "2022/04/02 18:29:07";
            SimpleDateFormat smpDF = new SimpleDateFormat(PTN);
            // Timestampオブジェクト生成
            Timestamp tmpA = new Timestamp(smpDF.parse(strA).getTime());

            String strB = "2022/04/03 18:29:07";
            SimpleDateFormat smDFt2 = new SimpleDateFormat(PTN);
            // Timestampオブジェクト生成
            Timestamp tmpB = new Timestamp(smDFt2.parse(strB).getTime());

            System.out.println("afterまたはbeforeメソッドでTimestampを比較する");
            // Timestampの比較(after/before)
            System.out.println("afterでTimeStampの比較結果: " + tmpA.after(tmpB));
            System.out.println("beforeでTimeStampの比較結果: " + tmpA.before(tmpB));

      }

}

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
afterまたはbeforeメソッドでTimestampを比較する
afterでTimeStampの比較結果: false
beforeでTimeStampの比較結果: true
afterまたはbeforeメソッドでTimestampを比較する afterでTimeStampの比較結果: false beforeでTimeStampの比較結果: true
afterまたはbeforeメソッドでTimestampを比較する
afterでTimeStampの比較結果: false
beforeでTimeStampの比較結果: true

 

Java

Posted by arkgame