我有一个有时间的字符串。我需要把它四舍五入到最近的小时和最近的一分钟。我怎么在java中做到这一点? 例如:字符串时间=“12:58:15”; 我需要把它四舍五入到1:00:00和12:58:00
答案 0 :(得分:20)
对于日历操作,您必须使用Calendar类。在你的情况下,你会做这样的事情:
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();
}
}
结果:
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)
一种方法是首先使用SimpleDateFormat及其解析方法将其转换为日期(javadoc将解释您需要的格式)。然后你可以查看秒数并确定你应该上升一分钟还是下降(如果它上升,将秒数设置为0并添加一分钟)。同样地,对于小时,只要看几分钟,如果它小于30,则向下舍入,如果大于30,则设置为零并增加小时。
答案 2 :(得分:1)
在Java 8中,您可以使用java.time.LocalDateTime或ZonedDateTime(时区)。可以四舍五入到最接近的小时数:
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);
例如:"2019-05-21 13:41:50"
将被打印为2019-05-21 14:00:00
答案 3 :(得分:0)
使用SimpleDateFormat创建日期。 从日期创建GregorianCalendar。 将分钟或秒数滚动30。 然后将分钟或秒设置为0.
答案 4 :(得分:0)
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();
}
}
答案 5 :(得分:0)
嗯,最好的答案是最简单的答案
/**
* 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)));
}