如何使用Kotlin处理Postgres时间戳?

时间:2019-01-05 01:43:15

标签: postgresql kotlin kotlin-exposed

我正在尝试通过建立实践博客来与Postgres和Kotlin保持同步。现在,我只是在处理虚拟内容,但是在接下来的几周内,我将开始一个更大的项目。我在示例应用程序中生成的时间戳似乎有些疯狂。我在这里做什么错了?

我得到了一个巨大的日期对象,我真的只想要像psql sh这样的时间戳记:

 SELECT * FROM posts;
 id |   title   |       content       |   type    | status |        datecreated         |        datemodified
----+-----------+---------------------+-----------+--------+----------------------------+----------------------------
  1 | Hey there | This is the content | Hey there | draft  | 2019-01-04 20:28:05.978762 | 2019-01-04 20:28:05.978762

控制器:

class PostController {
    fun index(): ArrayList<Post> {
        val posts: ArrayList<Post> = arrayListOf()
        transaction {
            Posts.selectAll().map { it ->
                posts.add(Post(
                        id = it[Posts.id], content = it[Posts.content],
                        title = it[Posts.title], type = it[Posts.type],
                        status = it[Posts.status], dateCreated = it[Posts.dateCreated],
                        dateModified = it[Posts.dateModified]
                ))
            }
        }
        return posts
    }
}

数据类:

import org.joda.time.DateTime

data class Post(
        val id: Int,
        val title: String,
        val content: String,
        val type: String,
        val status: String,
        val dateCreated: DateTime?,
        val dateModified: DateTime?
)

我正在使用Jetbrains暴露库。它在文档中没有提到这样的内容:

导入org.jetbrains.exposed.sql.Table

object Posts : Table() {
    val id = integer("id").primaryKey().autoIncrement()
    val title = text("title")
    val content = text("content")
    val type = text("type")
    val status = text("status")
    val dateCreated = datetime("datecreated")
    val dateModified = datetime("datemodified")
}

响应:

{
  "id": 1,
  "title": "Hey there",
  "content": "This is the content",
  "type": "Hey there",
  "status": "draft",
  "dateCreated": {
    "iMillis": 1546651744314,
    "iChronology": {
    "iBase": {
"iBase": {
"iBase": {
"iMinDaysInFirstWeek": 4
}
},
"iParam": {
"iZone": {
"iTransitions": [
-9223372036854776000,
...... goes on, seemingly forever

1 个答案:

答案 0 :(得分:0)

因此,我在“公开的”源代码中查看了DateColumnType.kt。显然,这一直是一个选择:

data class Post(
        val id: Int,
        val title: String,
        val content: String,
        val type: String,
        val status: String,
        val dateCreated: String?,
        val dateModified: String?
)