英文:
Round by next quarter minutes always
问题
我们有一个在Java代码中按照下一个季度分钟取整的要求,例如:
-
如果当前日期时间为2020-05-28T10:01:00
则将其四舍五入到下一个季度,变为2020-05-28T10:15:00 -
如果当前日期时间为2020-05-28T10:15:01
则将其四舍五入到下一个季度,变为2020-05-28T10:30:00 -
如果当前日期时间为2020-05-28T10:46:15
则将其四舍五入到下一个季度,变为2020-05-28T11:00:00 -
如果当前日期时间为2020-12-31T23:47:00
则将其四舍五入到下一个季度,变为2021-01-01T00:00:00
有人能提供一下实现这个需求的Java代码吗?非常感谢!
尝试了下面的代码,但无法获得我期望的输出:
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
int round = calendar.get(Calendar.MINUTE) % 15;
calendar.add(Calendar.MINUTE, round < 8 ? -round : (15-round));
calendar.set(Calendar.SECOND, 0);
System.out.println(calendar.getTime());
}
}
英文:
We have a requirement to Round by next quarter minutes in Java code, for example:
-
if current date time is 2020-05-28T10:01:00
then, round up to next quarter to make it 2020-05-28T10:15:00 -
if current date time is 2020-05-28T10:15:01
then, round up to next quarter to make it 2020-05-28T10:30:00 -
if current date time is 2020-05-28T10:46:15
then, round up to next quarter to make it 2020-05-28T11:00:00 -
if current date time is 2020-12-31T23:47:00
then, round up to next quarter to make it 2021-01-01T00:00:00
Can someone please provide Java code to achieve this. Any help is appreciated.
Tried below code but unable to get the output which I'm looking for:
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
int round = calendar.get(Calendar.MINUTE) % 15;
calendar.add(Calendar.MINUTE, round < 8 ? -round : (15-round));
calendar.set( Calendar.SECOND, 0 );
System.out.println(calendar.getTime());
}
}
答案1
得分: 3
这可以通过 java.time
包中的 LocalDateTime
类轻松实现,无论如何您都应该使用这个类<sup>1</sup>。
public static LocalDateTime roundUpToQuarter(LocalDateTime datetime) {
int minutesToAdd = 15 - (datetime.getMinute() % 15);
return datetime
.plusMinutes(minutesToAdd)
.truncatedTo(ChronoUnit.MINUTES);
}
这个涉及取模运算的计算确保我们得到要添加的分钟数,以到达下一个完整的一刻钟。truncatedTo(ChronoUnit.MINUTES)
调用进一步确保比 'minutes' 小的所有字段都被设为零。
更新:我不确定您的确切用例,但正如 Basil 指出的,如果您想要表示 时间线上的一个时刻,那么您还需要一个时区或偏移量。这可以通过在上述方法中将 LocalDateTime
实例替换为 ZonedDateTime
来实现。
<sup>1</sup> Date
、Calendar
和 SimpleDateFormat
类已过时。它们在简单情况下可能工作,但在更复杂的情况下一定会引起问题。请参阅 https://stackoverflow.com/questions/1969442/whats-wrong-with-java-date-time-api。
英文:
This can be easily done using the LocalDateTime
class from the java.time
package, which you should be using anyway<sup>1</sup>.
public static LocalDateTime roundUpToQuarter(LocalDateTime datetime) {
int minutesToAdd = 15 - (datetime.getMinute() % 15);
return datetime
.plusMinutes(minutesToAdd)
.truncatedTo(ChronoUnit.MINUTES);
}
This calculation involving the modulus operator makes sure that we get the number of minutes to add to go to the next full quarter. The truncatedTo(ChronoUnit.MINUTES)
call makes in turn sure that all fields smaller than 'minutes' are set to zero.
Update: I'm not certain of your exact use case, but, as Basil pointed out, if you want to represent a moment on the timeline, then you also need a timezone or offset. This can be done by replacing LocalDateTime
instances with ZonedDateTime
in the abovementioned method.
<sup>1</sup> The Date
, Calendar
and SimpleDateFormat
classes are obsolete. They may work in simple cases, but they will cause trouble in more complex cases. See https://stackoverflow.com/questions/1969442/whats-wrong-with-java-date-time-api.
答案2
得分: 0
你可以尝试以下代码。
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(1609438620000l));
int fraction = calendar.get(Calendar.MINUTE) / 15;
switch (fraction) {
case 0: {
calendar.set(Calendar.MINUTE, 0);
break;
}
case 1: {
calendar.set(Calendar.MINUTE, 15);
break;
}
case 2: {
calendar.set(Calendar.MINUTE, 30);
break;
}
default: {
calendar.set(Calendar.MINUTE, 45);
break;
}
}
calendar.add(Calendar.MINUTE, 15);
calendar.set(Calendar.SECOND, 0);
System.out.println(calendar.getTime());
英文:
You can try below code.
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(1609438620000l));
int fraction = calendar.get(Calendar.MINUTE) / 15;
switch (fraction) {
case 0: {
calendar.set(Calendar.MINUTE, 0);
break;
}
case 1: {
calendar.set(Calendar.MINUTE, 15);
break;
}
case 2: {
calendar.set(Calendar.MINUTE, 30);
break;
}
default: {
calendar.set(Calendar.MINUTE, 45);
break;
}
}
calendar.add(Calendar.MINUTE, 15);
calendar.set(Calendar.SECOND, 0);
System.out.println(calendar.getTime());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论