×

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 HashSet

HashSet implements the set interface. It uses the hash table to make the collection to store different data types. The hash set is the unordered collection of different data types....

6 minutes read.

Java class class

Java Class class instances are an executing Java application's implementation of the classes and interfaces. As well as, every Array is indeed an object that is common for all Arrays...

6 minutes read.

Find next greater number with same set of digits in Java

It contains a number (num). Finding the smallest number that has the same number of elements as num that is also larger than num is the task at hand. If...

8 minutes read.

Minimum XOR value pair in Java

In this section, you will discuss about minimum XOR value pair in Java. The objective is to enforce a value that indicates the least XOR values of the two numbers from...

4 minutes read.

Star Program in Java

By solving the patterns, we can develop our coding skills and logical thinking. Mostly each pattern program uses two or more loops. Loops' number depends on the complexity of logic....

3 minutes read.

Java Logo

Java is a prominent and extensively used object-oriented programming language. In 1995, Sun Microsystems created it. Later in 2009, Oracle Corp takeover Java. History of Java Logo The name of the island...

3 minutes read.

PriorityQueue in Java

PriorityQueue in Java A PriorityQueue is a member of the Java Collection Framework and is used when the values are needed to be processed based on priority. Priority queue operates similar to...

8 minutes read.

Java Tutorial

What is Java? Java is an object-oriented, robust, secured and platform-independent programming language. With the help of Java Programming, we can develop console, window, web, enterprise and mobile applications. Java language was...

22 minutes read.

Java Socket Programming

Java Socket Programming Socket programming is used for the communication between the applications, i.e., client and server running on different JRE may be connection-oriented or connectionless. The client program can be designed using the...

8 minutes read.

Zigzag Traversal of Binary Tree in Java

In this article, you will be acknowledged about the zigzag traversal of binary tree in java and the approaches or ways in which the zigzag traversal can be done. Zigzag Traversal A...

10 minutes read.

Producer Consumer Problem in Java Using Synchronized Block

The producer-consumer dilemma is a well-known instance of a multi-process synchronization issue in computing. Two processes—the producer and the consumer—are described in the issue, and they share a single, fixed-size...

4 minutes read.

Java String concat() method:

Java String concat() method is used to add the given String to the end of the current String. Syntax: public String concat(String str) Parameter: Str: String to be concatenated at the end of current...

1 minute read.

Duodecimal in Java

Duodecimal is a notation style in which a number with a base of 12 is referred to be a duodecimal number. In Java, we can use to convert duodecimal integers...

2 minutes read.

How to handle NullPointerException in Java

Introduction: Throwable class is the super or root class of the java exception hierarchy which contains two sub-classes. The classes’ are- ExceptionError Exception: If you are doing any wrong things in a...

3 minutes read.

Java Constant

A constant is an unchangeable entity in coding, as its title implies. The value which cannot be altered, in other terms. We shall understand about Java constants and exactly how...

3 minutes read.

Java Hello World

Let’s start by writing a simple program that prints “Hello World” to the output window. Write the program into any text editor or IDE (Eclipse, Netbeans, etc.) and save the file with the...

2 minutes read.

Sorting Algorithms in Java

Sorting Algorithms in Java Sorting is the technique that puts the elements of an array or list either in descending or ascending order. For example, take an array A, whose elements...

3 minutes read.

Java Error Stack Trace

The stack trace in Java is an array of stacks.The stack trace reveals the console's location of an exception or error by gathering data from all program methods. The JVM...

3 minutes read.

Java vs DotNET

Before understanding the differences between DotNET and Java, one must know about Java and DotNET. Java Java is a general-purpose programming language that is class-based and object-oriented, with minimal implementation dependencies. Regardless of...

9 minutes read.

Convert IP to Binary in Java

Fundamental conversion, such as going from binary to decimal or vice versa, is a crucial activity in computers. Understanding IP addressing and subnetting is crucial for networking. The primary networking...

4 minutes read.