根据kotlin中的时间戳过滤json响应



嘿,我有json响应来自retrofit. 我想要过滤器根据sendat财产。我想比较最近的时间并据此移动block。现在在我的json中有4个条目/block但它可能会更多。有人知道如何有效地过滤和修改列表吗?我还需要转换sentAttime和格式化并存储在其他变量中。今天的日期格式为time如果date是旧的,则需要在中输入date格式。

<Json响应/strong>

{
"conversations": [
{
"id": "789",
"title": "Conversation Title 3",
"lastMessage": {
"id": "4677",
"text": "Text 3",
"sentAt": "2021-09-28T10:39:10.0492422+01:00"
}
},
{
"id": "456",
"title": "Conversation Title 2",
"lastMessage": {
"id": "3c7e",
"text": "Text2",
"sentAt": "2021-09-26T12:39:10.0493518+01:00"
}
},
{
"id": "101",
"title": "Conversation Title 4",
"lastMessage": {
"id": "f983",
"text": "Text 4",
"sentAt": "2021-09-30T13:39:10.0493537+01:00"
}
},
{
"id": "123",
"title": "Conversation Title 1",
"lastMessage": {
"id": "f983",
"text": "Text 1",
"sentAt": "2021-09-26T12:38:00.0493537+01:00"
}
}......
]
}

预期结果

{
"conversations": [
{
"id": "101",
"title": "Conversation Title 4",
"lastMessage": {
"id": "f983",
"text": "Text 4",
"sentAt": "2021-09-30T13:39:10.0493537+01:00"
}
},
{
"id": "789",
"title": "Conversation Title 3",
"lastMessage": {
"id": "4677",
"text": "Text 3",
"sentAt": "2021-09-28T10:39:10.0492422+01:00"
}
},
{
"id": "456",
"title": "Conversation Title 2",
"lastMessage": {
"id": "3c7e",
"text": "Text2",
"sentAt": "2021-09-26T12:39:10.0493518+01:00"
}
},
{
"id": "123",
"title": "Conversation Title 1",
"lastMessage": {
"id": "f983",
"text": "Text 1",
"sentAt": "2021-09-26T12:38:00.0493537+01:00"
}
}......
]
}

例如将值存储在变量

id : 101
time : 13:39
id : 789
time : 28/09/2021

新增数据类

ConversationsResponse

data class ConversationsResponse(
val conversations: List<Conversations>? = null
)

data class Conversations(
val id: String? = null,
val title: String? = null,
val lastMessage: LastMessage? = null
)

LastMessage

data class LastMessage(
val id: String? = null,
val text: String? = null,
val sentAt: String? = null
)

下面是解析输入时间并分别处理日期和时间部分的示例,为此我从JSON示例中选取了一个sentAts:

import java.time.LocalDate
import java.time.LocalTime
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter
fun main() {
// example input from JSON
val sentAt = "2021-09-28T10:39:10.0492422+01:00"
// parse it directly, it is in ISO standard format
val odt: OffsetDateTime = OffsetDateTime.parse(sentAt)
// extract the date only
val date: LocalDate = odt.toLocalDate()
// extract the time of day
val time: LocalTime = odt.toLocalTime()
// format the date using a standard formatter
val dateString = date.format(DateTimeFormatter.ISO_LOCAL_DATE)
// and do the same for the time
val timeString = time.format(DateTimeFormatter.ISO_LOCAL_TIME)
// then print the formatted date and time separately
println("date: $dateString and time: $timeString")
}

这个的输出是

date: 2021-09-28 and time: 10:39:10.0492422

您可以直接在data class LastMessage中使用它,也可以创建一些实用程序类来转换LastMessages中的日期时间Strings。

可以改变输出使用不同DateTimeFormatter年代。有几个内置的格式化程序,您可以使用DateTimeFormatterBuilderDateTimeFormatter.ofPattern(String pattern)创建您的自定义格式化程序。如果你想控制命名单位的语言,如月、日、星期,你可以把Locale传递给DateTimeFormatter