如果前后1分钟或2分钟,如何舍入到最接近的5分钟间隔? [英] How to round off to the closest 5 minute interval if it is 1 or 2 minutes ahead or behind?

查看:87
本文介绍了如果前后1分钟或2分钟,如何舍入到最接近的5分钟间隔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Java中将Instant/LocalDateTime四舍五入到最接近的5分钟间隔.

I want to round off an Instant / LocalDateTime to its closest 5 minutes interval in Java.

示例:假设时间是: 2021-02-08T19: 02 :49.594预期结果: 2021-02-08T19: 00 :00.000

Examples: Suppose the time is: 2021-02-08T19:02:49.594 Expected result: 2021-02-08T19:00:00.000

假设时间为: 2021-02-08T19: 03 :49.594预期结果: 2021-02-08T19: 05 :00.000

Suppose the time is: 2021-02-08T19:03:49.594 Expected result: 2021-02-08T19:05:00.000

同样,如果时间是: 2021-02-08T19: 56 :49.594预期结果: 2021-02-08T19: 55 :00.000

Similarly, if the time is: 2021-02-08T19:56:49.594 Expected result: 2021-02-08T19:55:00.000

同样,如果时间是: 2021-02-08T19: 58 :49.594预期结果: 2021-02-08T20: 00 :00.000

Similarly, if the time is: 2021-02-08T19:58:49.594 Expected result: 2021-02-08T20:00:00.000

但是如果时间是2021-02-08T19: 55 :00.000或2021-02-08T19: 05 :00.000或2021-02-08T19: 00 :00.000然后执行什么都没有.

But if the time is 2021-02-08T19:55:00.000 or 2021-02-08T19:05:00.000 or 2021-02-08T19:00:00.000 then do nothing.

推荐答案

这是一种通用解决方案(不仅仅是5分钟):

Here is a general-purpose solution (not just 5 minutes):

public static Instant toNearest(Duration interval, Instant instant) {
    long intervalMillis = interval.toMillis();
    long adjustedInstantMillis = (instant.toEpochMilli() + (intervalMillis / 2)) / intervalMillis * intervalMillis;
    return Instant.ofEpochMilli(adjustedInstantMillis);
}

public static LocalDateTime toNearest(Duration interval, LocalDateTime dateTime, ZoneId zoneId) {
    ZoneRules zoneRules = zoneId.getRules();
    Instant instant = toNearest(interval, dateTime.toInstant(zoneRules.getOffset(dateTime)));
    return LocalDateTime.ofInstant(instant, zoneRules.getOffset(instant));
}

public static LocalDateTime toNearest(Duration interval, LocalDateTime dateTime) {
    return toNearest(interval, dateTime, ZoneId.systemDefault());
}

@Test
public void toNearestRoundsCorrectly() {
    assertThat(toNearest(Duration.ofMinutes(5), LocalDateTime.of(2021, 2, 8, 19, 0, 0)))
            .isEqualTo(LocalDateTime.of(2021, 2, 8, 19, 0, 0));
    assertThat(toNearest(Duration.ofMinutes(5), LocalDateTime.of(2021, 2, 8, 19, 2, 29, 999999999)))
            .isEqualTo(LocalDateTime.of(2021, 2, 8, 19, 0, 0));
    assertThat(toNearest(Duration.ofMinutes(5), LocalDateTime.of(2021, 2, 8, 19, 2, 30)))
            .isEqualTo(LocalDateTime.of(2021, 2, 8, 19, 5, 0));
    assertThat(toNearest(Duration.ofMinutes(5), LocalDateTime.of(2021, 2, 8, 19, 5, 0)))
            .isEqualTo(LocalDateTime.of(2021, 2, 8, 19, 5, 0));
}

@Test
public void toNearestTreatsDaylightSavingChangesCorrectly() {
    assertThat(toNearest(Duration.ofMinutes(5), LocalDateTime.of(2021,3,27, 23,57,30), ZoneId.of("Europe/London")))
            .isEqualTo(LocalDateTime.of(2021,3,28, 0,0,0));
    assertThat(toNearest(Duration.ofMinutes(5), LocalDateTime.of(2021,3,28, 0,57,29,999999999), ZoneId.of("Europe/London")))
            .isEqualTo(LocalDateTime.of(2021,3,28, 0,55,0));
    assertThat(toNearest(Duration.ofMinutes(5), LocalDateTime.of(2021,3,28, 0,57,30), ZoneId.of("Europe/London")))
            .isEqualTo(LocalDateTime.of(2021,3,28, 2,0,0));
    assertThat(toNearest(Duration.ofMinutes(5), LocalDateTime.of(2021,3,28, 2,5,0), ZoneId.of("Europe/London")))
            .isEqualTo(LocalDateTime.of(2021,3,28, 2,5,0));
}

这篇关于如果前后1分钟或2分钟,如何舍入到最接近的5分钟间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆