Given today’s time e.g. 2:24PM, how do I get it to round to 2:30PM?
Similarly if the time was 2:17PM, how do I get it to round to 2:15PM?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Rounding
You will need to use modulo to truncate the quarter hour:
As pointed out by EJP, this is also OK (replacement for the last line, only valid if the calendar is lenient):
Improvements
If you want to be exact, you will also have to truncate the smaller fields:
You can also use DateUtils.truncate() from Apache Commons / Lang to do this: