Round time by seconds

你离开我真会死。 提交于 2019-12-10 12:15:18

问题


In my Java project I want round Date time seconds which is divided by 5.

I have got   -   I want 
 12:00:01    -  12:00:00
 12:00:04    -  12:00:05
 12:00:06    -  12:00:05
 12:00:07    -  12:00:05
 12:00:08    -  12:00:10
 ...
 12:00:58    -  12:01:00

Date object contain date for example: Fri May 12 12:00:03 CEST 2017 and I want round seconds to modulo 5. I want achieve Date object with rounded seconds.

How can I do this using simple math or Joda ?


回答1:


Here’s a suggestion:

    LocalTime time = LocalTime.now(ZoneId.systemDefault()).truncatedTo(ChronoUnit.SECONDS);
    System.out.println("Before rounding: " + time);
    int secondsSinceLastWhole5 = time.getSecond() % 5;
    if (secondsSinceLastWhole5 >= 3) { // round up
        time = time.plusSeconds(5 - secondsSinceLastWhole5);
    } else { // round down
        time = time.minusSeconds(secondsSinceLastWhole5);
    }
    System.out.println("After rounding: " + time);

Output examples:

Before rounding: 14:46:33
After rounding: 14:46:35

Before rounding: 14:47:37
After rounding: 14:47:35

The % 5 (modulo 5) operation will give us the number of seconds since the last whole 5 seconds on the clock, as a number in the interval 0 through 4. Using that we know which way to round.

I am using java.time.LocalTime. It’s the recommended class for times without an explicit time zone today.




回答2:


Just as supplement to the correct answer of Ole V.V:

As far as I know (but other might correct me), Joda-Time offers rounding features but not of the type the OP wants, namely in configurable step widths (here: 5 seconds). So I suspect that a Joda solution would be very similar to that given by @Ole which is based on Java-8.

My time library Time4J has some more rounding features without any need to think so much about the rounding math as following code demonstrates:

import net.time4j.ClockUnit;
import net.time4j.PlainTime;

import net.time4j.format.expert.Iso8601Format;

import java.text.ParseException;
import java.time.LocalTime;

import static net.time4j.PlainTime.*;

public class RoundingOfTime {

    public static void main(String... args) throws ParseException {
        PlainTime t1 = Iso8601Format.EXTENDED_WALL_TIME.parse("12:59:57");
        PlainTime t2 = Iso8601Format.EXTENDED_WALL_TIME.parse("12:59:58");

        System.out.println(t1.with(SECOND_OF_MINUTE.roundedHalf(5))); // T12:59:55
        System.out.println(t2.with(SECOND_OF_MINUTE.roundedHalf(5))); // T13

        LocalTime rounded =
            PlainTime.nowInSystemTime()
            .with(PRECISION, ClockUnit.SECONDS) // truncating subseconds
            .with(SECOND_OF_MINUTE.roundedHalf(5)) // rounding
            .toTemporalAccessor(); // conversion to java-time
        System.out.println(rounded); // 15:57:05
    }
}

The method roundedHalf(int) is applicable on most time elements defined in the class PlainTime. I welcome further enhancement proposals, maybe even finding a way to define such methods as kind of TemporalAdjuster.



来源:https://stackoverflow.com/questions/43937466/round-time-by-seconds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!