×

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.

MethodsDescription
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

Timestamp Operation in Java

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

Timestamp Operation in Java

Related Topics

Java Boolean booleanValue() method

The booleanValue() method of Java Boolean class returns a Boolean value for the specified Boolean argument. Syntax public Boolean booleanValue() Parameters NA Return Value This method returns the primitive value of specified Boolean object. Example 1 public class...

2 minutes read.

Advanced Java Viva Questions

One of the more difficult languages available now is Java. Currently, 10 thousand developers worldwide use the programming language, which is rising daily. So, if you're a Java developer, an aspiring...

9 minutes read.

Zygodromes in Java

Zygodrome is a positive number created by the same digits running non-trivially. A number is called a zygodrome if identical digits constantly occur together (in pairs). The Greek word "zyg"...

4 minutes read.

Java time local date

Java: Java is one of the programming language which is object oriented. It consists of many features such as robust, simple, architecture neutral, dynamic, distributed, multi threaded, portable etc. The main feature...

3 minutes read.

Generics in Java

Generics in Java Parameterizedtypes mean generic. Generics allow types (Character, Integer, String, …, etc., as well as user-defined types) to act as parameters to interfaces, classes, and methods. Generics in Java...

9 minutes read.

Swing Program in Java

Java Swing is part of Java Foundation Classes (JFC). The swing toolkit is used to generate Graphical User Interface (GUI) for programs written in Java. The Java swing API is...

4 minutes read.

How to Convert Decimal to Hexadecimal in Java

How to Convert Decimal to Hexadecimal in Java The hexadecimal number uses 16 values to represent a number. Numbers from 0 to 9 represented by digits and the numbers from 10...

3 minutes read.

Convert list to array Java

One of the popular collection interfaces for storing an ordered collection is the List. The List interface may contain repeating groups and preserves the insertion order of entries. This article will...

4 minutes read.

Abstract Class Program in Java

Abstract Class Program in Java Abstraction is a technique by which a developer hides the implementation details from the user and shows only the functionality.It is not only confined to the...

6 minutes read.

Java Byte Code

Java byte code is really a powerful mechanism which makes Java a portable and platform-independent programming language. There are two software components which go along and make this byte code...

3 minutes read.

Upcasting and Downcasting in Java

Type casting in Java is an important and very interesting topic to deal with. But here upcasting and downcasting is somewhat related to typecasting. In normal typecasting, we convert from...

6 minutes read.

Java Final Keyword

In Java, the last keyword is used to limit the user. The applications of the java final keyword have large range of usage in program development. Last can be: variablemethodclass A final...

3 minutes read.

How to Convert String to float in Java

How to Convert String to Float in java It is used if you want to perform mathematical operations on the string that contains float number. You can convert String to float...

3 minutes read.

Java Integer toBinaryString() method

The toBinaryString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 2. Syntax public static String toBinaryString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

What is the ambiguity problem in Java?

The ambiguity problem in Java occurs when a method or constructor is overloaded with two or more methods with the same name but different parameters. This can confuse when multiple...

6 minutes read.

Beautiful Array in Java

In this section, we will be acknowledged about the beautiful array in Java, its characteristics, how it is achieved. What are the types of approaches and what are the tools...

8 minutes read.

Fall through in Java

In this article, you will be acknowledged about the fall through condition in Java along with an example program. Fall through It is a situation where there isn't a break statement in...

3 minutes read.

Program to find the duplicate characters in a string

Problem statement You have given with a string and your task is to find out the repeated characters from the string and print them. If no character is repeated, then you...

2 minutes read.

Centered Square Numbers in Java

In this tutorial, we will understand how to check the number is a centered square number. It is one of the prevalent interview questions of IT companies. Firstly, we will...

3 minutes read.

Java Math getExponent() Method

The getExponent() method of Math class returns the unbiased exponent of the argument. Syntax: public static int getExponent (double d) Parameters: The parameter ‘d’ represents the double value. Return Value: The getExponent () method returns the...

1 minute read.