1

I want to insert the current timestamp as datetime data type into sql server using Scala.

So far I have tried this:

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

var currentTimeStamp = LocalDateTime.now()
val datetimeFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm a")
var insertTimestamp = currentTimeStamp.format(datetimeFormat)

myInsertStatement.setInt(1, insertTimestamp)

This gives me insertTimestamp in the format "MM/dd/yyyy HH:mm a" but this is a string. Not datetime data type in sql.

How can I do this?

activelearner
  • 7,055
  • 20
  • 53
  • 94
  • Simple answer: You can't `TIMESTAMP / ROWVERSION` is a hexadecimal representation of a consecutive 8 byte integer, and has nothing to do with `DATETIME`. Also `DATETIME` doesn't have a format, it's just a binary value. – Ilyes Jun 27 '19 at 22:03

1 Answers1

0

Provided that you are using JDBC 4.2 or higher (which I believe that most of us are):

myInsertStatement.setObject(1, currentTimeStamp)

LocalDateTime is a poor choice for a timestamp, though, because it doesn’t “know” its time zone and therefore is ambiguous as a point in time. While I don’t know SQL Server, for most database engines it would be better to use timestamp with timezone on the database side and OffsetDateTime on the Scala side.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161