通过在 kotlin 中减去分钟来过滤数组列表
Filter the arraylist by subtracting with minutes in kotlin
我有一个对象列表,在那个对象中我有一个值时间戳,它是“时间戳”:“2021-12-16T07:30:13.950774575Z”,这种格式。我有一个要求,当我点击一个名为 60 分钟的文本视图时,我应该从结束时间获取 60 分钟的数据,也就是说,如果我在下午 4 点到晚上 7 点之间有数据,点击我需要从下午 6 点到晚上 7 点的数据。这需要减去 1 小时并过滤数据,我已经尝试过但我没有得到适当的解决方案,任何人都可以帮助我如何实现它。请帮助我,因为我是初学者。
将所有数据放在一个列表中,然后过滤列表中最近 60 分钟内的元素。
var now = currentTS();
var l = listOf(parseTS("2021-12-16T07:30:13.950774575Z"), parseTS(...),...);
var result = l.filter { now.inMinutes() - it.inMinutes() < 60 }
我不知道您使用的是哪种时间戳 class,因此请根据您的需要调整 currentTS
、parseTS
、inMinutes
。
我会使用 java.time
和 …
- 解析从按钮点击收到的
String
(当然是嘲笑)
- 从值中精确减去一小时
- 使用结果从示例列表中过滤
在这里,全部在一个fun main
,请看评论:
fun main(args: Array<String>) {
// your example String received on button click
val timestamp = "2021-12-16T07:30:13.950774575Z"
// example values to be filtered
val someObjects = listOf(
SomeObject(0, "2021-12-12T07:30:13.950774575Z"),
SomeObject(1, "2021-12-13T07:30:13.950774575Z"),
SomeObject(2, "2021-12-13T07:30:13.950774575Z"),
SomeObject(3, "2021-12-14T07:30:13.950774575Z"),
SomeObject(4, "2021-12-15T07:30:13.950774575Z"),
// here's the border
SomeObject(5, "2021-12-16T07:30:13.850774575Z"),
SomeObject(6, "2021-12-16T07:30:13.960774575Z"),
SomeObject(7, "2021-12-17T07:30:13.950774575Z"),
SomeObject(8, "2021-12-18T07:30:13.950774575Z")
)
// parse the timestamp captured on button click and subtract an hour
val filterByOdt = OffsetDateTime.parse(timestamp).minusHours(1)
// filter the list using the OffsetDateTime
val filteredObjects = someObjects.filter {
OffsetDateTime.parse(it.timestamp)
.isBefore(filterByOdt)
}
// print the ids of the results
println(filteredObjects.joinToString(", ") { "${it.id}" })
}
我在示例中使用了以下虚拟对象 class。
data class SomeObject(val id: Long, val timestamp: String)
根据需要,筛选后的列表仅包含时间戳在timestamp
之前的5 SomeObject
:
0, 1, 2, 3, 4
我有一个对象列表,在那个对象中我有一个值时间戳,它是“时间戳”:“2021-12-16T07:30:13.950774575Z”,这种格式。我有一个要求,当我点击一个名为 60 分钟的文本视图时,我应该从结束时间获取 60 分钟的数据,也就是说,如果我在下午 4 点到晚上 7 点之间有数据,点击我需要从下午 6 点到晚上 7 点的数据。这需要减去 1 小时并过滤数据,我已经尝试过但我没有得到适当的解决方案,任何人都可以帮助我如何实现它。请帮助我,因为我是初学者。
将所有数据放在一个列表中,然后过滤列表中最近 60 分钟内的元素。
var now = currentTS();
var l = listOf(parseTS("2021-12-16T07:30:13.950774575Z"), parseTS(...),...);
var result = l.filter { now.inMinutes() - it.inMinutes() < 60 }
我不知道您使用的是哪种时间戳 class,因此请根据您的需要调整 currentTS
、parseTS
、inMinutes
。
我会使用 java.time
和 …
- 解析从按钮点击收到的
String
(当然是嘲笑) - 从值中精确减去一小时
- 使用结果从示例列表中过滤
在这里,全部在一个fun main
,请看评论:
fun main(args: Array<String>) {
// your example String received on button click
val timestamp = "2021-12-16T07:30:13.950774575Z"
// example values to be filtered
val someObjects = listOf(
SomeObject(0, "2021-12-12T07:30:13.950774575Z"),
SomeObject(1, "2021-12-13T07:30:13.950774575Z"),
SomeObject(2, "2021-12-13T07:30:13.950774575Z"),
SomeObject(3, "2021-12-14T07:30:13.950774575Z"),
SomeObject(4, "2021-12-15T07:30:13.950774575Z"),
// here's the border
SomeObject(5, "2021-12-16T07:30:13.850774575Z"),
SomeObject(6, "2021-12-16T07:30:13.960774575Z"),
SomeObject(7, "2021-12-17T07:30:13.950774575Z"),
SomeObject(8, "2021-12-18T07:30:13.950774575Z")
)
// parse the timestamp captured on button click and subtract an hour
val filterByOdt = OffsetDateTime.parse(timestamp).minusHours(1)
// filter the list using the OffsetDateTime
val filteredObjects = someObjects.filter {
OffsetDateTime.parse(it.timestamp)
.isBefore(filterByOdt)
}
// print the ids of the results
println(filteredObjects.joinToString(", ") { "${it.id}" })
}
我在示例中使用了以下虚拟对象 class。
data class SomeObject(val id: Long, val timestamp: String)
根据需要,筛选后的列表仅包含时间戳在timestamp
之前的5 SomeObject
:
0, 1, 2, 3, 4