根据当前时间获取特定的 15 分钟时间范围
Get the specific 15 minutes timeframe based on current time
我想获得基于当前时间的 15 分钟时间范围。
例如:
时间表适用于 every 15 minutes
。
即12比1分为四个时间段[ 12:00 to 12:15, 12:16 to 12:30, 1:31 to 12:45, 12:46 to 1:00]
如果当前时间是12:34
意味着,我需要return时间范围为12:31 to 12:45
我们可以使用 Java 8 日期和时间 API 轻松完成吗?
您可以创建一个 TemporalAdjuster
来计算当前 15 分钟周期的结束时间,并通过删除 14 分钟来计算周期的开始时间。
可能看起来像这样:
public static void main(String[] args) {
LocalTime t = LocalTime.of(12, 34);
LocalTime next15 = t.with(next15Minute());
System.out.println(next15.minusMinutes(14) + " - " + next15);
}
public static TemporalAdjuster next15Minute() {
return (temporal) -> {
int minute = temporal.get(ChronoField.MINUTE_OF_DAY);
int next15 = (minute / 15 + 1) * 15;
return temporal.with(ChronoField.NANO_OF_DAY, 0).plus(next15, ChronoUnit.MINUTES);
};
}
输出 12:31 - 12-45
.
注意:我不确定它在 DST 更改时的表现如何 - 待测试。
您可以简单地使用日期 API 并计算间隔。
除以您的间隔(15 分钟)并再次乘以。这将去除分钟并四舍五入到较低的 15 分钟间隔。现在您必须增加十五分钟才能获得所需的输出。见以下代码:
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
long ms = new Date().getTime();
System.out.println("Current time: " + new Date().toString());
long fifteen = 15 * 60 * 1000;
long newMs = (ms / fifteen) * fifteen + fifteen;
System.out.println("Calculated time: " + new Date(newMs));
}
}
编辑:
Running example 与 LocalDate
import java.util.*;
import java.lang.*;
import java.io.*;
import java.time.*;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
LocalTime now = LocalTime.now();
System.out.println(now);
LocalTime next = now.with((temp) -> {
int currentMinute = temp.get(ChronoField.MINUTE_OF_DAY);
int interval = (currentMinute / 15) * 15 + 15;
temp = temp.with(ChronoField.SECOND_OF_MINUTE, 0);
temp = temp.with(ChronoField.MILLI_OF_SECOND, 0);
return temp.with(ChronoField.MINUTE_OF_DAY, interval);
});
System.out.println(next);
}
}
这里有一个使用 LocalTime 的更简单的方法
public static void main(String[] args) {
int interval = 15; //Can be customized to any value
LocalTime now = LocalTime.now();
int minuteOfDay = now.toSecondOfDay() / 60;
int mod = (minuteOfDay / interval + 1) * interval;
System.out.println("lower bound" + LocalTime.ofSecondOfDay((mod - interval) * 60));
System.out.println("Upper bound" + LocalTime.ofSecondOfDay(mod * 60));
}
我想获得基于当前时间的 15 分钟时间范围。
例如:
时间表适用于 every 15 minutes
。
即12比1分为四个时间段[ 12:00 to 12:15, 12:16 to 12:30, 1:31 to 12:45, 12:46 to 1:00]
如果当前时间是12:34
意味着,我需要return时间范围为12:31 to 12:45
我们可以使用 Java 8 日期和时间 API 轻松完成吗?
您可以创建一个 TemporalAdjuster
来计算当前 15 分钟周期的结束时间,并通过删除 14 分钟来计算周期的开始时间。
可能看起来像这样:
public static void main(String[] args) {
LocalTime t = LocalTime.of(12, 34);
LocalTime next15 = t.with(next15Minute());
System.out.println(next15.minusMinutes(14) + " - " + next15);
}
public static TemporalAdjuster next15Minute() {
return (temporal) -> {
int minute = temporal.get(ChronoField.MINUTE_OF_DAY);
int next15 = (minute / 15 + 1) * 15;
return temporal.with(ChronoField.NANO_OF_DAY, 0).plus(next15, ChronoUnit.MINUTES);
};
}
输出 12:31 - 12-45
.
注意:我不确定它在 DST 更改时的表现如何 - 待测试。
您可以简单地使用日期 API 并计算间隔。 除以您的间隔(15 分钟)并再次乘以。这将去除分钟并四舍五入到较低的 15 分钟间隔。现在您必须增加十五分钟才能获得所需的输出。见以下代码:
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
long ms = new Date().getTime();
System.out.println("Current time: " + new Date().toString());
long fifteen = 15 * 60 * 1000;
long newMs = (ms / fifteen) * fifteen + fifteen;
System.out.println("Calculated time: " + new Date(newMs));
}
}
编辑:
Running example 与 LocalDate
import java.util.*;
import java.lang.*;
import java.io.*;
import java.time.*;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
LocalTime now = LocalTime.now();
System.out.println(now);
LocalTime next = now.with((temp) -> {
int currentMinute = temp.get(ChronoField.MINUTE_OF_DAY);
int interval = (currentMinute / 15) * 15 + 15;
temp = temp.with(ChronoField.SECOND_OF_MINUTE, 0);
temp = temp.with(ChronoField.MILLI_OF_SECOND, 0);
return temp.with(ChronoField.MINUTE_OF_DAY, interval);
});
System.out.println(next);
}
}
这里有一个使用 LocalTime 的更简单的方法
public static void main(String[] args) {
int interval = 15; //Can be customized to any value
LocalTime now = LocalTime.now();
int minuteOfDay = now.toSecondOfDay() / 60;
int mod = (minuteOfDay / interval + 1) * interval;
System.out.println("lower bound" + LocalTime.ofSecondOfDay((mod - interval) * 60));
System.out.println("Upper bound" + LocalTime.ofSecondOfDay(mod * 60));
}