I have a String which has the time. I need to round it off to the nearest hour and also the nearest minute. How do I do it in java? Ex: String time="12:58:15"; I need to round it off to 1:00:00 and also 12:58:00
6 Answers
For calendar operations you have to use Calendar class. In your case you would do something like this:
package test;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class TestDate {
public static void main(String[] args) {
Calendar c = new GregorianCalendar();
c.set(Calendar.HOUR_OF_DAY, 12);
c.set(Calendar.MINUTE, 58);
c.set(Calendar.SECOND, 15);
Date d = c.getTime();
System.out.println("Start point: " + d.toString());
System.out.println("Nearest whole minute: " + toNearestWholeMinute(d));
System.out.println("Nearest whole hour: " + toNearestWholeHour(d));
}
static Date toNearestWholeMinute(Date d) {
Calendar c = new GregorianCalendar();
c.setTime(d);
if (c.get(Calendar.SECOND) >= 30)
c.add(Calendar.MINUTE, 1);
c.set(Calendar.SECOND, 0);
return c.getTime();
}
static Date toNearestWholeHour(Date d) {
Calendar c = new GregorianCalendar();
c.setTime(d);
if (c.get(Calendar.MINUTE) >= 30)
c.add(Calendar.HOUR, 1);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
return c.getTime();
}
}
And the result:
Start point: Tue Nov 22 12:58:15 CET 2011
Nearest whole minute: Tue Nov 22 12:58:00 CET 2011
Nearest whole hour: Tue Nov 22 13:00:00 CET 2011

- 1
- 1

- 11,196
- 6
- 36
- 54
-
Thanks for the reply. I need to be using a String object as mentioned above as I would be getting it from a text file. – Srinivas Nov 23 '11 at 15:46
-
4when rounding, you should also set `Calendar.MILLISECOND` to `0`, not just seconds (and minutes). – oezi Jul 31 '13 at 12:41
-
1Um, you "have" to use the `Calendar` class? No. You can do this logic raw using the primative `long`. For example: `long now = System.currentTimeMillis(); long rounded = now - now % 60000;` Once you have a long you can do whatever you want. – Blake Jan 12 '18 at 16:23
With Java 8 you can use a java.time.LocalDateTime or ZonedDateTime (timezone). This would get you started by rounding to the nearest hour:
String mytime = "2019-05-21 13:41:50";
// Convert String to LocalDateTime
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.parse(mytime, dtf);
// If minutes equals 30 or more, add 1 hour
int minutes = localDateTime.getMinute();
if (minutes >= 30)
{
localDateTime = localDateTime.plusHours(1);
}
// Round down
localDateTime = localDateTime.truncatedTo(ChronoUnit.HOURS)
// Back to String (OP wants the end result to be of type String)
String roundedTime = dtf.format(localDateTime);
System.out.println(roundedTime);
For example: "2019-05-21 13:41:50"
would be printed as 2019-05-21 14:00:00

- 3,375
- 1
- 31
- 45
-
4For rounding down to whole hour I recommend `localDateTime.truncatedTo(ChronoUnit.HOURS)` (it will get rid of any nanoseconds too). – Ole V.V. May 23 '19 at 16:41
-
@OleV.V. Yes you are correct. In my answer above the DateTImeFormatter would "take care" of it as well, since the end result is a String. I will update my answer! – Baked Inhalf May 28 '19 at 05:32
One way is to first convert it into a Date using SimpleDateFormat and its parse method (The javadoc will explain the format you need). Then you can look at the seconds and determine whether you should go up a minute or down (if it goes up, set the seconds to 0 and add a minute). Likewise for the hour, just look at minutes and if it's less than 30, round down and if greater than thirty set to zero and increase the hour.

- 9,332
- 17
- 73
- 109
-
Thanks for the reply. I need to be using a String object as mentioned above as I would be getting it from a text file. – Srinivas Nov 23 '11 at 15:46
-
You can use the simple date format to convert to date, perform your work, then use it to convert back to String. Probably a lot more reliable than parsing all the possible permutations of the string. But, you can do it the hard way if you wish. – AHungerArtist Nov 23 '11 at 17:58
package com.xyz.util;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DateTimeUtils {
public static void main(String[] args) {
Date date = new Date();
date.setHours(23);
System.out.println("prev whole hour, millies: "+toWholeHour(date,-1)+", "+toWholeHour(date,-1).getTime());
System.out.println("curr whole hour, millies: "+toWholeHour(date,0)+", "+toWholeHour(date,0).getTime());
System.out.println("next whole hour, millies: "+toWholeHour(date,1)+", "+toWholeHour(date,1).getTime());
System.out.println("prev whole minute, millies: "+toWholeMinute(date,-1)+", "+toWholeMinute(date,-1).getTime());
System.out.println("curr whole minute, millies: "+toWholeMinute(date,0)+", "+toWholeMinute(date,0).getTime());
System.out.println("next whole minute, millies: "+toWholeMinute(date,1)+", "+toWholeMinute(date,1).getTime());
}
public static Date toWholeHour(Date d, int beforeOrAfter) {
Calendar c = new GregorianCalendar();
c.setTime(d);
c.add(Calendar.HOUR, beforeOrAfter);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();
}
public static Date toWholeMinute(Date d, int beforeOrAfter) {
Calendar c = new GregorianCalendar();
c.setTime(d);
c.add(Calendar.MINUTE, beforeOrAfter);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();
}
}

- 64
- 3
Well, the best answer is the simplest one
/**
* 2020-01-01 10:00:55 is transformed into 2020-01-01 10:00:00
* 2020-01-01 10:20:55 is transformed into 2020-01-01 10:00:00
* 2020-01-01 10:30:01 is transformed into 2020-01-01 11:00:00
* @return the Timestamp rounded to the nearest hour
*/
public static Timestamp roundTimestampToNearestHour(Timestamp input){
return Timestamp.from(Instant.ofEpochMilli(((input.getTime()+1800000)/(60000*60))*(60000*60)));
}

- 1,400
- 11
- 16
Create a Date using SimpleDateFormat. Create GregorianCalendar from the Date. Roll the minutes or seconds by 30. Then set minutes or seconds to 0.

- 2,649
- 3
- 22
- 32
-
Thanks for the reply. I need to be using a String object as mentioned above as I would be getting it from a text file. – Srinivas Nov 23 '11 at 15:45