Timestamp Operation in Java
JDBC escape syntax is supported by Timestamp's formatting and parsing functions. Additionally, it adds support for fractional seconds values for SQL TIMESTAMP.java.util.Date is wrapped in a lightweight wrapper that enables the JDBC API to recognise this as a SQL TIMESTAMP value.
Methods | Description |
after ( ) | If this Timestamp object is later than the given Timestamp object, it returns the Boolean value true or else false. |
before ( ) | If this Timestamp object is earlier than the given Timestamp object, then this function returns the Boolean value true or else false |
to ( ) | Timestamp object is compared to the supplied timestamp object or the given date object using the to ( ) function |
Equal ( ) | If this Timestamp object is equal to the given Timestamp object or to the specified object, it returns the Boolean value true or else false. |
From ( ) | Provides a Timestamp instance from an Instant object. |
getNanos ( ) | Retrieves the nanos value of the Timestamp object. |
getTime ( ) | The amount of milliseconds that have passed since January 1, 1970, 00:00:00 GMT. |
hashCode ( ) | For this object set, returns a hash code result. |
Nanos ( ) | The function nanos ( ) creates a nanos value for the given integer value. |
setTime ( ) | Sets the object of this class to a millisecond of time (after January 1, 1970 00:00:00 GMT). |
toInstant ( ) | the Instant that results from the conversion of the Timespan object to toInstant ( ) represents the same time point as this timestamp. |
toLocalDateTime ( ) | This will return the same date-time value as this Timestamp, converts this Timespan object into a LocalDateTime. |
toString ( ) | Timespan object is transformed using JDBC timestamp escape format via function toString ( ) |
valueOf ( ) | function valueOf ( ) creates a Timestamp value from a string object or extracts a Timestamp instance from a LocalDateTime object. |
TimeStamp.java
import java . sql . Timestamp ;
import java . time . Instant ;
// public class
public class TimeStamp {
public static void main ( String [ ] args ) {
// Gets a Timestamp instance from an Instant object by from () method
Timestamp in= Timestamp . from ( Instant . now ( ) ) ;
System . out . println ( " 1. from() method will return "+ in ) ;
// function valueOf ( ) creates a Timestamp value from a string object or extracts a Timestamp instance from a LocalDateTime object.
String s1 ="2018-09-01 09:01:15";
Timestamp tp = Timestamp . valueOf ( s1 ) ;
System . out . println ( " 2 . value of Timestamp : "+ tp ) ;
// getNanos() Retrieves the nanos value of the Timestamp object.
Integer v= tp . getNanos ( ) ;
System . out . println ( " 3 . Fractional seconds component : "+ v ) ;
Timestamp ts = Timestamp . valueOf ( " 2018-09-01 09:01:16 " ) ;
System . out . println ( "4. Boolean value returned : " + tp . before ( ts ) ) ;
}
}
Output

TimeStamp2.java
import java . sql . * ;
import java . time . Instant ;
public class Time Stamp2 {
public static void main ( String [ ] args ) {
Timestamp t1 = Timestamp . valueOf ( "2018-09-01 09:01:15");
System . out . println("Timestamp : "+ t1 ) ;
// getTime ( ) The amount of milliseconds that have passed since January 1, 1970, 00:00:00 GMT.
Long val= t1 . getTime ( ) ;
System.out.println("1. Milliseconds : "+val);
// hashCode() For this object set, returns a hash code result.
Integer v1=t1 . hashCode ( ) ;
System . out . println ( " 2. Hash code : "+ v1 ) ;
// setNanos()
t1 . setNanos ( 54646 ) ;
System . out . println ( "3 . Timestamp after setting nanos : " + t1 ) ;
// the Instant that results from the conversion of the Timespan object to toInstant ( ) represents the same time point as this timestamp.
Instant in = t1.toInstant ( ) ;
System . out . println ( " 4. Instant Timespan : " + in ) ;
}
}
Output
