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

标签 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

Controller :

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 Exposed 库。它在文档中没有提到类似的内容:

导入 org.jetbrains.expose.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

最佳答案

所以我查看了Exposed源代码中的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?
)

关于postgresql - 如何使用 Kotlin 处理 Postgres 时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54048245/

相关文章:

kotlin - 如何在 Kotlin Exposed 中获取插入的行?

kotlin - 如何从 Exposed 中的 QueryAlias 映射 count() 值

vb.net - 无法在 Visual Basic.net 中将 postgresql 查询显示为组合框

java - 在匿名类的接口(interface)中只实现一种方法

intellij-idea - 按运行时 Intellij Idea 无法构建

kotlin - 为什么不能使用名称相同但泛型不同的方法呢?

Kotlin Exposed - 如果不存在和冲突,如何插入?

ruby-on-rails - 加载错误 : cannot load such file --rails on 'rake db:setup'

php - Yii2 批量删除操作发生Internal Server Error

postgresql - Postgres IF 变量