Scala conversion long to datetime

后端 未结 2 1271
故里飘歌
故里飘歌 2021-01-15 05:42

I am using nscala-time (wrapper for Joda Time) and slick for a project. I\'m trying to use this clause to write a line to the database:

Article.insert(0,\"ti         


        
相关标签:
2条回答
  • 2021-01-15 06:05

    You misunderstand what asInstanceOf does: asInstanceOf doesn't convert anything. What it does is lie to the compiler, telling it to believe something instead of going with the knowledge it has.

    So, you had a Long, and then you got a Long, but pretended it was a Timestamp, which obviously doesn't work.

    I have a simple recommendation regarding asInstanceOf: never use it.

    0 讨论(0)
  • 2021-01-15 06:15

    There's no magic about it. Your first statement:

    DateTime.now.getMillis
    

    is a Long. A Long is not a Timestamp, so it makes sense that you can't convert it to one by using asInstanceOf.

    The second statement:

    new Timestamp(dateTime.getMillis())
    

    is using the Timestamp constructor to create a new Timestamp instance based on the dateTime.getMillis.

    0 讨论(0)
提交回复
热议问题