Date and Time

How to Convert Date into Timestamp format in Java

Table of Contents

Program

import java.sql.Timestamp;
import java.time.Instant;

public class TimeStamp {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	  
	       Timestamp instant= Timestamp.from(Instant.now());  
	        System.out.println("1. from() method will return "+instant);  
	        
	        String str="2018-09-01 09:01:15";  
	        Timestamp timestamp= Timestamp.valueOf(str);  
	        System.out.println("2. value of Timestamp : "+timestamp);  
	       
	        Integer val=timestamp.getNanos();  
	        System.out.println("3. Fractional seconds component : "+val);  
	        Timestamp ts2 = Timestamp.valueOf("2018-09-01 09:01:16");  
	       
		System.out.println("4. Boolean value returned : "+timestamp.before(ts2));  
	}

}

Output

	1. from() method will return 2021-04-08 15:37:58.0525668
	2. value of Timestamp : 2018-09-01 09:01:15.0
	3. Fractional seconds component : 0
	4. Boolean value returned : true

Description

public BeanContext getPropagatedFrom()

Gets the BeanContext from which this event was propagated.

Returns:

the BeanContext from which this event was propagated

public static String valueOf​(int i)

Returns the string representation of the int argument.
The representation is exactly the one returned by the Integer.toString method of one argument.

Parameters:

i – an int.

Returns:

a string representation of the int argument.

See Also:

Integer.toString(int, int)

public int getNanos()

Gets this Timestamp object’s nanos value.

Returns:

this Timestamp object’s fractional seconds component

See Also:

setNanos(int)

public boolean before​(Date when)

Tests if this date is before the specified date.

Parameters:

when – a date.

Returns:

true if and only if the instant of time represented by this
Date object is strictly earlier than the instant represented by when; false otherwise.

Throws:

NullPointerException – if when is null