📜  Java SQL Timestamp before()函数及示例

📅  最后修改于: 2022-05-13 01:55:08.627000             🧑  作者: Mango

Java SQL Timestamp before()函数及示例

before()函数是Java SQL 的 Timestamp 类的一部分。该函数返回一个布尔值,表示 Timestamp 对象是否出现在给定的 Timestamp 对象之前。

函数签名:

public boolean before(Timestamp t)

句法:

ts1.before(ts2);

参数:该函数接受 Timestamp 对象作为要检查的参数。

返回值:函数返回布尔数据类型,表示 Timestamp 对象是否出现在给定 Timestamp 对象之前。

异常:函数不抛出任何异常

下面的例子说明了 before()函数的使用

示例 1:创建两个不相等的时间戳,并检查第二个时间戳是否在第一个时间戳之前。

// Java program to demonstrate the
// use of before() function
  
import java.sql.*;
  
public class solution {
    public static void main(String args[])
    {
  
        // Create two timestamp objects
        Timestamp ts1 = new Timestamp(10003);
        Timestamp ts2 = new Timestamp(10001);
  
        boolean b = ts2.before(ts1);
  
        // Check if the second timestamp occurs
        // before first timestamp
        if (b) {
  
            // If true print that the Second Timestamp
            // occurs before the first timestamp
            System.out.println("Second Timestamp occurs"
                               + " before first timestamp");
        }
  
        else {
  
            // If false print that the Second Timestamp
            // does not occur before the first timestamp
            System.out.println("Second Timestamp does not occur"
                               + " before first timestamp");
        }
    }
}
输出:
Second Timestamp occurs before first timestamp

示例2:创建两个相等的时间戳,并检查第二个时间戳是否在第一个时间戳之前

// Java program to demonstrate the
// use of before() function
  
import java.sql.*;
  
public class solution {
    public static void main(String args[])
    {
  
        // Create two timestamp objects
        Timestamp ts1 = new Timestamp(10000);
        Timestamp ts2 = new Timestamp(10000);
  
        boolean b = ts2.before(ts1);
  
        // Check if the second timestamp occurs
        // before first timestamp
        if (b) {
  
            // If true print that the Second Timestamp
            // occurs before the first timestamp
            System.out.println("Second Timestamp occurs"
                               + " before first timestamp");
        }
  
        else {
  
            // If false print that the Second Timestamp
            // does not occur before the first timestamp
            System.out.println("Second Timestamp does not"
                               + " occur before first timestamp");
        }
    }
}
输出:
Second Timestamp does not occur before first timestamp

参考: https: Java/sql/Timestamp.html