以秒为单位的循环时间
Round time by seconds
在我的 Java 项目中,我想要将日期时间秒数除以 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
日期对象包含日期,例如:Fri May 12 12:00:03 CEST 2017
我想要圆秒取模 5。我想用圆秒实现 Date 对象。
我如何使用简单的数学运算或 Joda
来做到这一点?
这里有一个建议:
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);
输出示例:
Before rounding: 14:46:33
After rounding: 14:46:35
Before rounding: 14:47:37
After rounding: 14:47:35
% 5
(模数 5)运算将为我们提供自时钟上最后一个完整 5 秒以来的秒数,作为介于 0 到 4 之间的数字。使用它我们知道哪种方式圆.
我正在使用 java.time.LocalTime
。对于今天没有明确时区的时间,建议 class。
作为Ole正确答案的补充V.V:
据我所知(但其他人可能会纠正我),Joda-Time 提供 rounding features 但不是 OP 想要的类型,即在可配置的步骤中宽度(此处:5 秒)。所以我怀疑 Joda 解决方案与@Ole 基于 Java-8.
给出的解决方案非常相似
我的时间库 Time4J 有一些更多的舍入功能,不需要考虑太多舍入数学,如下代码所示:
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
}
}
方法roundedHalf(int)适用于classPlainTime
中定义的大多数时间元素。我欢迎进一步的增强建议,甚至可能找到一种方法来定义这样的方法 TemporalAdjuster
.
在我的 Java 项目中,我想要将日期时间秒数除以 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
日期对象包含日期,例如:Fri May 12 12:00:03 CEST 2017
我想要圆秒取模 5。我想用圆秒实现 Date 对象。
我如何使用简单的数学运算或 Joda
来做到这一点?
这里有一个建议:
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);
输出示例:
Before rounding: 14:46:33
After rounding: 14:46:35
Before rounding: 14:47:37
After rounding: 14:47:35
% 5
(模数 5)运算将为我们提供自时钟上最后一个完整 5 秒以来的秒数,作为介于 0 到 4 之间的数字。使用它我们知道哪种方式圆.
我正在使用 java.time.LocalTime
。对于今天没有明确时区的时间,建议 class。
作为Ole正确答案的补充V.V:
据我所知(但其他人可能会纠正我),Joda-Time 提供 rounding features 但不是 OP 想要的类型,即在可配置的步骤中宽度(此处:5 秒)。所以我怀疑 Joda 解决方案与@Ole 基于 Java-8.
给出的解决方案非常相似我的时间库 Time4J 有一些更多的舍入功能,不需要考虑太多舍入数学,如下代码所示:
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
}
}
方法roundedHalf(int)适用于classPlainTime
中定义的大多数时间元素。我欢迎进一步的增强建议,甚至可能找到一种方法来定义这样的方法 TemporalAdjuster
.