Java Date rounding
If you use Apache commons-lang, you can use DateUtils
to round your dates:
Date now = new Date();
Date nearestMinute = DateUtils.round(now, Calendar.MINUTE);
The way to do it without 3rd-party libraries (may be not so elegant and not so flexible, though): add a half of a field (for a rounding by minutes - 30 seconds) and set this field and lower ones to zero.
Calendar calendar = ... // assume you already have it with a specified Date value
// 'add' cause changing larger fields if necessary
calendar.add( Calendar.SECOND, 30 );
calendar.set( Calendar.SECOND, 0 );
calendar.set( Calendar.MILLISECOND, 0 );
If a current value is less than 30 seconds, a minute value won't change on 'add'. Otherwise, it will be increased by 1. In any case, seconds and lower values are zeroed. So we have a rounding.