syv
syv

Reputation: 3608

Converting LocalDateTime to Timestamp format

Im converting date time string format to Timestamp (java.sql) from different format. I have converted it to LocalDateTime based on different format. Is there anyway to convert LocalDateTime object to Timestamp?

Upvotes: 3

Views: 8151

Answers (2)

Anonymous
Anonymous

Reputation: 86296

It’s more delicate than you think, so good you asked.

Since you can use java.time, the modern Java date and time API, you should not wish to apply the old-fashioned and poorly designed Timestamp class too.

I am assuming that you were asking for a Timestamp because you need something that you can store into an SQL database column of type timestamp (without time zone). Allow me to mention that for most purposes you don’t want such a column. The standard SQL datatype timestamp doesn’t live up its name. While the idea of a timestamp is that it should identify an unambiguous point in time when something happened, an SQL timestamp does not define such a point in time. It defines a date and time of day, and anyone is free to interpret it in any time zone, allowing for a variation of some 26 hours. Instead for a point in time you should use timestamp with timezone. It doesn’t live up to its name either in that it doesn’t let you choose the time zone. It stores times in UTC, so does identify a unique point in time.

On the Java side use an OffsetDateTime for storing into a timestamp with timezone column. Since you’ve got a LocalDateTime, you need to convert. And for the conversion you need to know the time zone intended behind your LocalDateTime. For example:

    final ZoneId zone = ZoneId.of("Asia/Tehran");
    LocalDateTime ldt = LocalDateTime.of(2019, 2, 25, 23, 45);
    OffsetDateTime odt = ldt.atZone(zone).toOffsetDateTime();

    System.out.println("As OffsetDateTime: " + odt);

As OffsetDateTime: 2019-02-25T23:45+03:30

Store into your database:

    PreparedStatement ps = yourDatabaseConnection.prepareStatement(
            "insert into your_table(your_ts_with_timezone) values (?)");
    ps.setObject(1, odt);
    ps.executeUpdate();

If either for some reason you do need a timestamp without time zone in your database or you cannot change it, you don’t need any conversion at all. Just store the LocalDateTime you had:

    PreparedStatement ps = yourDatabaseConnection.prepareStatement(
            "insert into your_table(your_ts_without_timezone) values (?)");
    ps.setObject(1, ldt);

If you are programming to a legacy API that requires an old-fashioned java.sql.Timestampobject, things are getting even delicater. A Timestamp does define a point in time. So you will now need to convert from a LocalDateTime that does not define a point in time, to a Timestamp that does define one, then pass the Timestamp to an API that may store it in a database column that again does not define an unambiguous point in time. You will need to be sure which time zones are used for the conversions, and still there may be corner cases that fail or (worse) give incorrect results. However, as the_storyteller mentioned in a comment, the conversion is straightforward enough:

    Timestamp ts = Timestamp.valueOf(ldt);
    System.out.println("As old-fashioned Timestamp: " + ts);

As old-fashioned Timestamp: 2019-02-25 23:45:00.0

The conversion uses your JVMs default time zone. This setting is also used by TImestamp.toString(), so the output is as expected. This is shaky, though, since any program running in the JVM may change the default time zone setting at any time, so generally you don’t know what you get. To exercise control over the time zone used for conversion:

    Instant i = ldt.atZone(zone).toInstant();
    Timestamp ts = Timestamp.from(i);

Links

Upvotes: 6

Saeed Alizadeh
Saeed Alizadeh

Reputation: 1467

try it out with java 8 built in classes

public class Test {
    public static void main(String[] args) {
        Timestamp timestamp = Timestamp.valueOf(LocalDateTime.now());
        // java timestamp class
        System.out.println(timestamp);
        // miliseconds 
        System.out.println(timestamp.getTime());
    }
}

Upvotes: 2

Related Questions