In java, how do we end the time before the next hour and minute?

I have a line that has time. I need to round it to the nearest hour, as well as the next minute. How to do this in java? Example: String time = "12:58:15"; I need to round it to 1:00:00 as well as 12:58:00

+4
source share
5 answers

For calendar operations, you need to use the Calendar class. In your case, you will 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 
+20
source

One way is to first convert it to Date using SimpleDateFormat and its parsing method (javadoc will explain the format you need), then you can look at the seconds and determine whether you should go a minute or down (if it rises, set the seconds to 0 and add a minute). Similarly, for an hour, just look at the minutes, and if it is less than 30, round down, and if more than thirty, set to zero and increase the hour.

+1
source

In Java 8, you can use java.time.LocalDateTime or ZonedDateTime (time zone). This will help you start 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" will be printed as 2019-05-21 14:00:00

+1
source

Create a date using SimpleDateFormat. Create a GregorianCalendar from a date. Reset minutes or seconds to 30. Then set minutes or seconds to 0.

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

Source: https://habr.com/ru/post/1382570/


All Articles