Java Timestamp

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

The Java timestamp is used to provide parsing operations and formatting to support the escape syntax of JDBC. it also adds up the ability to hold the SQL TIMESTAMP fractional second value.

Introduction to Java Timestamp

The timestamp of java belongs to the thin wrapper class of java. util. Date it is allowed to be recognized as a SQL TIMESTAMP type of value by the API JDBC. the precision of this TIMESTAMP of java is known for:

  • The total number of characters in the `yyyy-mm-dd hh:mm format is 19
  • The precision is 20+ s which is again a total number of characters in the yyyy-mm-dd hh:mm `format, where s is the scale of the timestamp given, which is a fractional second precision.

The timestamp belongs to the typed composite of java.util.Date that has a separate value of nanosecond. The values are stored in java.util.Date components are of integral seconds. The Timestamp.equals(Object) method does not return true in the output when an object is passed in the argument of the method and which does not belong to java.sql.Timestamp instance.

This is due to the fact that the component Nanos is unknown to that date. This factor leads to asymmetry of the Timestamp. equals(Object) method as compared to the java.util.Date.equals(Object) method and the underlying implementation of the method java. util. The date will be used by the method hashcode thereby not including Nanos in the computation.

Declaration

The syntax for declaring a timestamp in java is as follows:

The syntax given constructs a timestamp value by using a millisecond time value.

Constructors of Java Timestamp

constructordescription
Timestamp(int year, int month, int date, int hour, int minute, int second, int nano)Deprecated, instead we can use Timestamp(long millis) constructor
Timestamp(long time)This method constructs a time stamp object by using time value milliseconds

Constructors Detail

Timestamp constructor detail

Deprecated, instead we can the use Timestamp(long millis) constructor Parameters:

This method Throws: IllegalArgumentException - if the nano argument is out of bounds

Timestamp

Constructs an object, Timestamp by using a milliseconds time value. In the underlying date value, the integral seconds are stored; the fractional seconds are stored in the field, N os of the Timestamp object.

Parameters: time - milliseconds since January 1, 1970, 00:00:00 GMT. A negative number is a number of milliseconds before January 1, 1970, 00:00:00 GMT.

Methods of Java Timestamp

The method timestamp can be used in a lot of methods, some of which are shown below

MethodSyntaxDescription
afterpublic boolean after(Timestamp TM)This method shows if the present object of a timestamp is after the given object. Here, tm is the value of the timestamp object to be compared with another. The function returns true if the timestamp value is after the given object, else false.
beforepublic boolean before(Timestamp TM)This method shows if the present object of a timestamp is before the given object. Here, tm is the value of the timestamp object to be compared with another. The function returns true if the timestamp value is before the given object, else false.
compareTopublic int compareTo(Timestamp tm)This is a comparison function that compares the given object of timestamp with others. The function return 0 when both the objects to be compared are equal whereas if the value to be compared comes before the other object then the value is less than 0 else it is greater than 0.
`equalspublic boolean equals(Timestamp tm)This method is used to check whether two timestamps are equal. Here, tm is the value of the timestamp object to be compared with another. The function returns true if the timestamp values are equal, else false.
getTimepublic long getTime()This number is used for getting the total number of milliseconds that starts from the default value of the data which is Jan 1, 1970, 00:00:00 GMT, indicated by the timestamp object.
getNanospublic int getNanos()This function is used for fetching the object of timestamp whose values are in Nanos. The function returns the fractional second parameter of the object.
toInstantpublic Instant toInstant()It is used to change the timestamp of the object to an instant that indicates a point on the line which is the same as the timestamp’s object. This function also overrides the method constant of the class Date.
public void setTime(long time)public void setTime(long time)This function sets the object off timestamp to indicate a time in milliseconds that is after Jan 1, 1970, 00:00:00 GMT.
getTimepublic long getTime()This function is generally used for getting the time in milliseconds.
valueOfpublic static Timestamp valueOf(String str)This method is used for converting the string object to the timestamp value that belongs to the JDBC timestamp escape format.

Methods Detail

setTime

This function sets the object off timestamp to indicate a time in milliseconds that is after Jan 1, 1970, 00:00:00 GMT.

Overrides: setTime in class Date

Parameters: time - the number of `milliseconds```.

getTime

This function is generally used for getting the time in milliseconds.

Overrides: getTime in class Date

Returns: This date represents the number of milliseconds since January 1, 1970, 00:00:00 GMT.

valueOf

This method is used for converting the string object to the timestamp value that belongs to the JDBC timestamp escape format.

Parameters: s - timestamp in format yyyy-[m]m-[d]d hh:mm.f.... The fractional seconds may be omitted. The leading zero for mm and dd may also be omitted.

Returns: corresponding Timestamp value

Throws: IllegalArgumentException - if the given argument does not have the format yyyy-[m]m-[d]d hh:mm: ss[.f...]

toString

Formats a timestamp in JDBC timestamp escape format. yyyy-mm-dd hh:mm: ss. fffffffff, where ffffffffff indicates nanoseconds.

Overrides: toString in class Date

Returns: a String object in yyyy-mm-dd hh:mm: ss. fffffffff format

getNanos

This function is used for fetching the object of timestamp whose values are in Nanos. The function returns the fractional second parameter of the object.

Returns: this Timestamp object's fractional seconds component

Sets this Timestamp object's Nanos field to the given value.

Parameters: n - the new fractional seconds component

Throws: IllegalArgumentException - if the given argument is greater than 999999999 or less than 0

equals

This method is used for checking if two timestamps are equal or not. Here, tm is the value of the timestamp object to be compared with another. The function returns true if the timestamp values are equal, else false.

Parameters: ts - the Timestamp value to compare with

Returns: true if the given Timestamp object is equal to this Timestamp object; false otherwise

before

This method shows if the present object of a timestamp is before the given object. Here, tm is the value of the timestamp object to be compared with another. The function returns true if the timestamp value is before the given object, else false.

Parameters: ts - the Timestamp value to compare with

Returns: true if this Timestamp object is earlier; false otherwise

after

This method shows if the present object of a timestamp is after the given object. Here, tm is the value of the timestamp object to be compared with another. The function returns true if the timestamp value is after the given object, else false.

Parameters: ts - the Timestamp value to compare with

Returns: true if this Timestamp object is later; false otherwise

compareTo

This is a comparison function that compares the given object of timestamp with others. The function return 0 when both the objects to be compared are equal whereas if the value to be compared comes before the other object then the value is less than 0 else it is greater than 0.

Parameters: ts - the Timestamp object to be compared to this Timestamp object

Returns: the value 0 if the two Timestamp objects are equal; a value less than 0 if this Timestamp object is before the given argument; and a value greater than 0 if this Timestamp object is after the given argument.

Implementation of Java Timestamp

Below is the implementation of Timestamp in Java

Output:

Example

Java Program to Demonstrate the Functionality of getTime() Function

Output: demonstrate the functionality of getTime() function

Explanation: In this example, we will first create an object of timestamp. At the output, we will print it by using two different functions, toString, and getTime functions.

More Examples of Java Timestamp

Java Program to Get Current Timestamp Value

Output: Java Program to Get Current Timestamp Value

Explanation: In this example, we will first create two timestamp objects and use the getTime function to fetch the timestamp object time and initialize it to the before the default value of time which is Jan 1, 1970, which the negative value shows.

Convert Instant to/from Timestamp

To convert Instant to/from Timestamp, look at the example given below

We can also use Timestamp.toInstant() method to convert Timestamps to Instants

Insert Timestamp into a Table

Output

Advantages of Using Java Timestamp

The following are the advantages of using the timestamp in java:

  • By the use of ZonedDateTime, it covers the Timezone
  • With the help of Duration.toDays(), It also covers the conversion of units
  • When compared with j ava. Util, the object Timestamp has a better set of functions for calculating and manipulating the logic.
  • Clear separation of timestamp object’s between two different time duration, due to methods like LocalDate, and LocalTime.

Conclusion

  • The Java timestamp is used to provide parsing operations and formatting in order to support the escape syntax of JDBC. it also adds up the ability to hold the SQL TIMESTAMP fractional second value.
  • The timestamp of java belongs to the thin wrapper class of java. UTI .D ate it is allowed to be recognized as a SQL TIMESTAMP type of value by the API JDBC.
  • The syntax for declaring a timestamp in java is as follows:

The syntax given constructs a timestamp value by using a millisecond time value.