Q:

Java Convert Timestamp to Date

belongs to collection: Java Conversion Programs

0

We can convert Timestamp to Date in java using constructor of java.util.Date class.

The constructor of Date class receives long value as an argument. So, you need to convert Timestamp object into long value using getTime() method of java.sql.Timestamp class.

Let's see the constructor of Date class and signature of getTime() method.

Constructor of Date class:

 

Date(long l)  

getTime() method of Timestamp class:

 

public long getTime()  

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Java Timestamp to Date Example

Let's see the simple example to convert Timestamp to Date in java.

import java.sql.Timestamp;    
import java.util.Date;    
 public class TimestampToDateExample1 {    
       public static void main(String args[]){    
                Timestamp ts=new Timestamp(System.currentTimeMillis());  
                Date date=new Date(ts.getTime());  
                System.out.println(date);                     
        }    
}    

 

Output:

Thu Nov 02 02:29:07 IST 2017

The Timestamp class extends Date class. So, you can directly assign instance of Timestamp class into Date. In such case, output of Date object will be like Timestamp. But, it is not suggested by Java Doc because you may loose the milliseconds or nanoseconds of data.

import java.sql.Timestamp;    
import java.util.Date;    
 public class TimestampToDateExample2 {    
       public static void main(String args[]){    
                Timestamp ts=new Timestamp(System.currentTimeMillis());  
                Date date=ts;  
                System.out.println(date);                     
        }    
} 

 

Output:

2017-11-02 02:36:57.204

   

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Convert Binary to Decimal... >>
<< Java Convert Date to Timestamp...