开发者

Using roundMinutes in Java

I am trying to get the hour and minute and then round the minutes to the nearest 5. I found the java Calendar class with the roundMinutes function here: enter link description here

How would I use this in code? I have tried creating a Calendar object, a Date object, but nothing shows me this method to use. Providing an example would b开发者_如何学JAVAe great!


Make sure you are using this Calendar. There is no roundMinutes in java's default calendar.

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. 

See ROUND_DOWN


The following code rounds a given joda DateTime to 5 minutes and up

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);


Be sure you are using the right Calendar class: org.rakeshv.utils.Calendar, assuming you are using the custom Util jar that Rakesh created.


I'm not sure about the nearest 5 minutes but the following code will round to the next minute:

Date now = new Date();
Date nearestMinute = DateUtils.round(now, Calendar.MINUTE);

Perhaps you can proceed form there :D

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜