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