Search code examples
javatimestampdeprecateddefault-constructor

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.


Solution

  • 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()
    );