gpt4 book ai didi

java - 比较日期列表并找到最接近分钟的日期,然后选择它并将其四舍五入到最接近的分钟

转载 作者:行者123 更新时间:2023-12-02 09:21:52 25 4
gpt4 key购买 nike

我有一个对象类型列表 - TrackingMessage,其中包含日期。该列表可以包含 5 到数千个条目。

我需要遍历该列表来比较每个日期,以找到最接近分钟的日期。该列表在一分钟内可能包含 3 或 4 个条目,因此需要找到最接近分钟的条目,然后将其四舍五入到最接近的分钟。

即 12:45:22、12:45:35、12:45:15

从上面的小示例中,它将选择 12:45:15 作为最接近分钟的值,然后将其四舍五入到最接近的分钟 - 12:45:00>.

我目前的代码如下:

返回根据时间排序和舍入的消息列表的方法

private List<TrackingMessage> findTrackingMessageDatesToNearestMinute(List<TrackingMessage> filteredTrackingMessages) {
return filteredTrackingMessages.stream()
.sorted(orderByClosestToMinute)
.filter(JourneyServiceUtil.distinctByKey(x -> roundToClosestMinute(x)))
.sorted(comparing(TrackingMessage::getEventTime))
.collect(Collectors.toList());
}

传入流的比较器

  public Comparator<TrackingMessage> orderByClosestToMinute = (tm1, tm2) -> {
LocalDateTime roundedTrackingMessage1 = roundToClosestMinute(tm1);
LocalDateTime roundedTrackingMessage2 = roundToClosestMinute(tm2);
Duration tm1Duration = Duration.between(DateTimeUtils.toLocalDateTime(tm1.getEventTime()), roundedTrackingMessage1).abs();
Duration tm2Duration = Duration.between(DateTimeUtils.toLocalDateTime(tm2.getEventTime()), roundedTrackingMessage2).abs();
return tm1Duration.compareTo(tm2Duration);
};

将时间四舍五入到最接近的分钟的函数

  private LocalDateTime roundToClosestMinute(TrackingMessage trackingMessage) {
return DateTimeUtils.toLocalDateTime(DateUtils.round(trackingMessage.getEventTime(), Calendar.MINUTE));
}

去重函数

  public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Set<Object> seen = ConcurrentHashMap.newKeySet();
return t -> seen.add(keyExtractor.apply(t));
}

现在这段代码可以工作了,但是这段代码返回的响应如下:

{
"status": "success",
"serviceId": "d8f2092b-01de-424f-9068-32c4efdcfd45",
"driverId": "e1275ef6-f885-4724-9255-2e03868df5ee",
"eventList": [
{
"eventTime": "2019-07-18T13:09:01",
"eventId": 15,
"lat": 50.4130166,
"lon": -5.0739198
},
{
"eventTime": "2019-07-18T13:10:04",
"eventId": 21,
"lat": 50.412991299999995,
"lon": -5.073924
},
{
"eventTime": "2019-07-18T13:11:04",
"eventId": 21,
"lat": 50.413074599999995,
"lon": -5.074010299999999
},
{
"eventTime": "2019-07-18T13:12:04",
"eventId": 21,
"lat": 50.4131295,
"lon": -5.0739676
},
{
"eventTime": "2019-07-18T13:13:04",
"eventId": 21,
"lat": 50.4133475,
"lon": -5.073917499999999
},
{
"eventTime": "2019-07-18T13:14:04",
"eventId": 21,
"lat": 50.4133618,
"lon": -5.0739218
},
{
"eventTime": "2019-07-18T13:15:04",
"eventId": 21,
"lat": 50.4133333,
"lon": -5.073961
},
{
"eventTime": "2019-07-18T13:16:04",
"eventId": 21,
"lat": 50.4132803,
"lon": -5.0739765
},
{
"eventTime": "2019-07-18T13:17:04",
"eventId": 21,
"lat": 50.4133433,
"lon": -5.0739588
},
{
"eventTime": "2019-07-18T13:18:04",
"eventId": 21,
"lat": 50.413306999999996,
"lon": -5.0739355999999995
},
{
"eventTime": "2019-07-18T13:19:04",
"eventId": 21,
"lat": 50.4133013,
"lon": -5.073953599999999
},
{
"eventTime": "2019-07-18T13:20:04",
"eventId": 21,
"lat": 50.413309299999995,
"lon": -5.073988099999999
},
{
"eventTime": "2019-07-18T13:21:04",
"eventId": 21,
"lat": 50.4133146,
"lon": -5.0739618
},
{
"eventTime": "2019-07-18T13:22:04",
"eventId": 21,
"lat": 50.413287999999994,
"lon": -5.0739141
},
{
"eventTime": "2019-07-18T13:23:04",
"eventId": 21,
"lat": 50.4132981,
"lon": -5.0739008
},
{
"eventTime": "2019-07-18T13:24:04",
"eventId": 21,
"lat": 50.413283,
"lon": -5.07386
},
{
"eventTime": "2019-07-18T13:25:03",
"eventId": 52,
"lat": 50.413303,
"lon": -5.0738673
},
{
"eventTime": "2019-07-18T13:26:01",
"eventId": 60,
"lat": 50.4135111,
"lon": -5.0737745
},
{
"eventTime": "2019-07-18T13:27:02",
"eventId": 130,
"lat": 50.415140099999995,
"lon": -5.073355299999999
},
{
"eventTime": "2019-07-18T13:28:00",
"eventId": 125,
"lat": 50.4139743,
"lon": -5.0697981
},
{
"eventTime": "2019-07-18T13:29:01",
"eventId": 21,
"lat": 50.4186421,
"lon": -5.0698473
},
{
"eventTime": "2019-07-18T13:30:09",
"eventId": 153,
"lat": 50.418105999999995,
"lon": -5.0624391
},
{
"eventTime": "2019-07-18T13:30:50",
"eventId": 21,
"lat": 50.4185011,
"lon": -5.0581641
},
{
"eventTime": "2019-07-18T13:31:59",
"eventId": 21,
"lat": 50.417915099999995,
"lon": -5.0517363
},
{
"eventTime": "2019-07-18T13:33:02",
"eventId": 131,
"lat": 50.4194111,
"lon": -5.0496058
},
{
"eventTime": "2019-07-18T13:35:34",
"eventId": 51,
"lat": 50.4194153,
"lon": -5.049543
}
]
}

正如您所看到的(向底部),我们在 13:30 内有两次 {2019-07-18T13:30:50、2019-07-18T13:30:09}

从技术上讲,我希望选择 2019-07-18T13:30:09,因为它最接近分钟(相差 9 秒,而另一个相差 10 秒)

此外,如果您查看响应,时间会以秒为单位,并且不会向下舍入。

如果不清楚,请随时提出任何进一步的问题。

最佳答案

您似乎想要计算自一分钟开始以来经过的秒数。因此,在您的示例中,13:30:09 表示该分钟后 9 秒,13:30:50 表示该分钟后 50 秒。

要计算您可以使用此方法:

public static int distanceToMinute(LocalDateTime t) {
LocalDateTime minute = t.truncatedTo(ChronoUnit.MINUTES);
return (int) ChronoUnit.SECONDS.between(minute, t);
}

对于您给出的两个示例,它将按预期返回 9 和 50。

下一步是按分钟对时间进行分组,例如:

List<LocalDateTime> times = ...
Map<LocalDateTime, List<LocalDateTime>> timesByMinute = times.stream()
.collect(Collectors.groupingBy(t -> t.truncatedTo(ChronoUnit.MINUTES)));

然后将每个列表减少到最接近分钟的元素:

public static LocalDateTime bestTime(List<LocalDateTime> times) {
return times.stream()
.sorted((t1, t2) -> Integer.compare(distanceToMinute(t1), distanceToMinute(t2)))
.findFirst().get();
}

关于java - 比较日期列表并找到最接近分钟的日期,然后选择它并将其四舍五入到最接近的分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58639207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com