Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Timestamp constructor

I want to use the default constructor for the Timestamp class in Java but Eclipse indicates that it is deprecated. This is the constructor:

Timestamp myDate = new Timestamp(2014, 3, 24, 0, 0, 0 ,0);

Eclipse recommends using Timestamp(long time) default constructor but I don't know how to use it.

like image 542
Roby Sottini Avatar asked Oct 27 '25 09:10

Roby Sottini


1 Answers

What about the next method?

int myYear = 2014;
int myMonth = 3;
int myDay = 24;
Timestamp ts = Timestamp.valueOf(String.format("%04d-%02d-%02d 00:00:00", 
                                                myYear, myMonth, myDay));

Using JSR 310: Date and Time API (introduced in the Java SE 8 release):

java.sql.Timestamp ts = java.sql.Timestamp.valueOf(
        java.time.LocalDate.of(myYear, myMonth, myDay).atStartOfDay()
);
like image 121
Paul Vargas Avatar answered Oct 29 '25 23:10

Paul Vargas