From 071f999a3952641fd05c5c53521ee522238bb5eb Mon Sep 17 00:00:00 2001 From: Alexandre Grison <a.grison@gmail.com> Date: Thu, 4 May 2017 22:24:32 +0200 Subject: [PATCH] comment date format --- src/main/kotlin/io/realworld/model/Comment.kt | 2 +- src/main/kotlin/io/realworld/model/inout/Comment.kt | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/io/realworld/model/Comment.kt b/src/main/kotlin/io/realworld/model/Comment.kt index deb310f..e63e0c1 100644 --- a/src/main/kotlin/io/realworld/model/Comment.kt +++ b/src/main/kotlin/io/realworld/model/Comment.kt @@ -5,7 +5,7 @@ import javax.persistence.* @Entity data class Comment(var createdAt: OffsetDateTime = OffsetDateTime.now(), - var updatedAt: OffsetDateTime? = null, + var updatedAt: OffsetDateTime = OffsetDateTime.now(), var body: String = "", @ManyToOne var article: Article = Article(), diff --git a/src/main/kotlin/io/realworld/model/inout/Comment.kt b/src/main/kotlin/io/realworld/model/inout/Comment.kt index 0843398..cff433e 100644 --- a/src/main/kotlin/io/realworld/model/inout/Comment.kt +++ b/src/main/kotlin/io/realworld/model/inout/Comment.kt @@ -2,21 +2,27 @@ package io.realworld.model.inout import com.fasterxml.jackson.annotation.JsonRootName import io.realworld.model.User +import java.time.OffsetDateTime +import java.time.ZoneId import java.time.format.DateTimeFormatter @JsonRootName("comment") data class Comment(val createdAt: String, - val updatedAt: String?, + val updatedAt: String, val body: String, val author: Profile, val id: Long) { companion object { + fun dateFormat(date: OffsetDateTime): String { + return date.toZonedDateTime().withZoneSameInstant(ZoneId.of("Z")).format(DateTimeFormatter.ISO_ZONED_DATE_TIME) + } + fun fromModel(model: io.realworld.model.Comment, currentUser: User): Comment { return Comment( id = model.id, body = model.body, - createdAt = model.createdAt.format(DateTimeFormatter.ISO_DATE_TIME), - updatedAt = model.updatedAt?.format(DateTimeFormatter.ISO_DATE_TIME), + createdAt = dateFormat(model.createdAt), + updatedAt = dateFormat(model.updatedAt), author = Profile.fromUser(model.author, currentUser) ) } -- GitLab