在Java中使用roundMinutes
我试图计算小时和分钟,然后将分钟四舍五入到最近的5。我在这里找到了带有roundMinutes函数的java Calendar类:在Java中使用roundMinutes,java,Java,我试图计算小时和分钟,然后将分钟四舍五入到最近的5。我在这里找到了带有roundMinutes函数的java Calendar类: 我将如何在代码中使用它?我已经尝试过创建一个日历对象,一个日期对象,但是没有显示这个方法可以使用。提供一个例子将是伟大的 确保您使用的是正确的日历类:org.rakeshv.utils.Calendar,假设您正在使用。确保您使用的是正确的日历类:org.rakeshv.utils.Calendar,假设您正在使用。确保您正在使用日历。java的默认日历中没有rou
我将如何在代码中使用它?我已经尝试过创建一个日历对象,一个日期对象,但是没有显示这个方法可以使用。提供一个例子将是伟大的 确保您使用的是正确的
日历
类:org.rakeshv.utils.Calendar
,假设您正在使用。确保您使用的是正确的日历
类:org.rakeshv.utils.Calendar
,假设您正在使用。确保您正在使用日历
。java的默认日历中没有roundMinutes
calendar.roundMinutes(5, Calendar.ROUND_UP) if you want 5:41 to become 5:45
calendar.roundMinutes(5, Calendar.ROUND_DOWN) if you want 5:41 to become 5:40
calendar.roundMinutes(5, Calendar.ROUND_AUTO) if you want 5:41 to become whatever the library thinks is normal.
请参见确保您正在使用日历
。java的默认日历中没有roundMinutes
calendar.roundMinutes(5, Calendar.ROUND_UP) if you want 5:41 to become 5:45
calendar.roundMinutes(5, Calendar.ROUND_DOWN) if you want 5:41 to become 5:40
calendar.roundMinutes(5, Calendar.ROUND_AUTO) if you want 5:41 to become whatever the library thinks is normal.
请参见我不确定最近的5分钟,但以下代码将循环到下一分钟:
Date now = new Date();
Date nearestMinute = DateUtils.round(now, Calendar.MINUTE);
也许您可以从那里继续:D我不确定最近的5分钟,但以下代码将转到下一分钟:
Date now = new Date();
Date nearestMinute = DateUtils.round(now, Calendar.MINUTE);
也许您可以从那里继续:D下面的代码将给定的joda DateTime四舍五入到5分钟以上
DateTime dt = timeEnded.minuteOfDay().roundCeilingCopy();
int minutes = dt.getMinuteOfHour();
int mod = minutes % 5;
int add = 5 - mod; //add this to our datetime
DateTime roundedTo5 = dt.plusMinutes(add);
以下代码将给定的joda DateTime四舍五入到5分钟及以上
DateTime dt = timeEnded.minuteOfDay().roundCeilingCopy();
int minutes = dt.getMinuteOfHour();
int mod = minutes % 5;
int add = 5 - mod; //add this to our datetime
DateTime roundedTo5 = dt.plusMinutes(add);
我不确定那是什么API,但它不适用于标准Java日历类。以下是用于日历的Java7文档。虽然它不能直接回答您的问题,但您可能希望在处理日期时使用Joda Time,而不是内置类。我之前曾回答过一个类似的问题,OP想要四舍五入到最近的四分之一。在这里查看:@BartvanHeukelom仅供参考,该项目现在已经开始,团队建议迁移到类中。我不确定那是什么API,但它不适用于标准的JavaCalendar类。以下是用于日历的Java7文档。虽然它不能直接回答您的问题,但您可能希望在处理日期时使用Joda Time,而不是内置类。我之前曾回答过一个类似的问题,OP想要四舍五入到最近的四分之一。在这里查看:@BartvanHeukelom仅供参考,该项目现在已经开始,团队建议迁移到类中。看见