Java SQL Timestamp before() function with examples
Last Updated :
06 Mar, 2019
Improve
The before() function is a part of Timestamp class of Java SQL.The function returns a boolean value representing whether the Timestamp object occurs before the given Timestamp object.
Function Signature:
Java
Java
public boolean before(Timestamp t)Syntax:
ts1.before(ts2);Parameters: The function accepts Timestamp object as parameter which is to be checked. Return value: The function returns boolean data type representing whether the Timestamp object occurs before the given Timestamp object. Exception: The function does not throw any exceptions Below examples illustrate the use of before() function Example 1: Create two non equal timestamps and check whether the second timestamp occurs before first timestamp or not.
// 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");
}
}
}
// 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");
}
}
}
Output:
Example 2: Create two equal timestamps and check whether the second timestamp occurs before the first timestamp or not
Second Timestamp occurs before first timestamp
// 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");
}
}
}
// 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");
}
}
}
Output:
Reference: https:// docs.oracle.com/javase/7/docs/api/java/sql/Timestamp.html
Second Timestamp does not occur before first timestamp