如何在java中将时间缩短到最近的四分之一小时?

鉴于今天的时间,例如下午2点24分,我怎样才能到达下午2点30分?

同样如果时间是下午2点17分,我怎样才能到下午2点15分?

四舍五入

您将需要使用模来截断四分之一小时:

Date whateverDateYouWant = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(whateverDateYouWant); int unroundedMinutes = calendar.get(Calendar.MINUTE); int mod = unroundedMinutes % 15; calendar.add(Calendar.MINUTE, mod < 8 ? -mod : (15-mod)); 

正如EJP指出的那样,这也是可以的(更换最后一行,只有在日历是宽松的情况下才有效):

 calendar.set(Calendar.MINUTE, unroundedMinutes + mod); 

改进

如果你想确切地说,你也将不得不截断较小的领域:

 calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); 

您也可以使用Apache Commons / Lang中的 DateUtils.truncate()来执行此操作:

 calendar = DateUtils.truncate(calendar, Calendar.MINUTE); 

很简单,find自1970年以来的四分之一,四分之一,乘以十五分钟:

 long timeMs = System.System.currentTimeMillis(); long roundedtimeMs = Math.round( (double)( (double)timeMs/(double)(15*60*1000) ) ) * (15*60*1000) ); 

用你的date或日历对象。

有了上面的答案,您将得到所有有趣的代码来处理溢出到几小时,几天等。

自从这个时代以来,我会用毫秒的时间。

加7.5分或7.5×60×1000 = 450000

并截断为900000的倍数

 new Date(900000 * ((date.getTime() + 450000) / 900000)) 

这是有效的,因为ms时间开始的时间恰好是00:00:00。 而且由于世界上的所有时区都以15分钟的步幅变化,所以这并不影响四舍五入。

(哎呀,我太多了,忘记了一些重要的括号:现在还为时过早)

Java 8的一个注释实现。接受任意舍入单位和增量:

  public static ZonedDateTime round(ZonedDateTime input, TemporalField roundTo, int roundIncrement) { /* Extract the field being rounded. */ int field = input.get(roundTo); /* Distance from previous floor. */ int r = field % roundIncrement; /* Find floor and ceiling. Truncate values to base unit of field. */ ZonedDateTime ceiling = input.plus(roundIncrement - r, roundTo.getBaseUnit()) .truncatedTo(roundTo.getBaseUnit()); ZonedDateTime floor = input.plus(-r, roundTo.getBaseUnit()) .truncatedTo(roundTo.getBaseUnit()); /* * Do a half-up rounding. * * If (input - floor) < (ceiling - input) * (ie floor is closer to input than ceiling) * then return floor, otherwise return ceiling. */ return Duration.between(floor, input).compareTo(Duration.between(input, ceiling)) < 0 ? floor : ceiling; } 

来源:我自己

精彩的post,非常感谢你们! 这正是我需要的:)

这是我的基于工作的代码。

我的用例是“现在是上午11点47分,我想设置两个date象征当前的5分钟框架:上午11:45和上午11:50”

  Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); int modulo = calendar.get(Calendar.MINUTE) % 5; if(modulo > 0) { calendar.add(Calendar.MINUTE, -modulo); } myObject.setStartDate(calendar.getTime()); calendar.add(Calendar.MINUTE, 5); myObject.setDueDate(calendar.getTime()); 

如果你只是想要取整,这是一个更可读的版本使用Java时间API

 LocalDateTime time = LocalDateTime.now(); LocalDateTime lastQuarter = time.truncatedTo(ChronoUnit.HOURS) .plusMinutes(15 * (time.getMinute() / 15)); 

输出:

2016-11-04T10:58:10.228

2016-11-04T10:45:00

也许你可以使用一个实用程序库来操作date,例如这里有一个圆的方法可以帮助你:

http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/time/DateUtils.html#round%28java.util.Calendar,%20int%29

这里是代码示例:

  FastDateFormat formatter = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT; Date now = new Date(); System.out.println("now = " + formatter.format(now)); // Get nearest second Date nearestSecond = DateUtils.round(now, Calendar.SECOND); System.out.println("nearestSecond = " + formatter.format(nearestSecond)); // Get nearest minute Date nearestMinute = DateUtils.round(now, Calendar.MINUTE); System.out.println("nearestMinute = " + formatter.format(nearestMinute)); // Get nearest hour Date nearestHour = DateUtils.round(now, Calendar.HOUR); System.out.println("nearestHour = " + formatter.format(nearestHour)); 

你可以使用这个简单的代码…

 int mode = min % 15; if (mode > 15 / 2) { min = 15 - mode; } else { min = 0 - mode; } cal.add(Calendar.MINUTE, min); 

如果你有会议logging,可以用下面的函数把它们四舍五入:
int minutes = i % 15 < 8 ? i / 15 * 15 : (i / 15 + 1) * 15;

 minutes = (int) (Math.round(minutes / 15.0) * 15.0); 

使用我在Stackoverflow上find的一些代码,我创build了下面的代码。 它会输出四分之一分钟将被四舍五入。

 import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; DateTimeFormatter Datum_Format = DateTimeFormatter.ofPattern("HH:mm"); LocalDateTime time = LocalDateTime.now(); for(int i=0; i<=59; i++) { time = time.withMinute(i); int Minute = time.getMinute(); int Quarter = 15 * (int) Math.round(Minute / 15); if (Quarter == 60) { Time2 = time.plusHours(1); Time2 = Time2.withMinute(0); LOG.info (Datum_Format.format(time) + "," + Datum_Format.format(Time2)); } else { Time2 = time; Time2 = Time2.withMinute(Quarter); LOG.info (Datum_Format.format(time) + "," + Datum_Format.format(Time2)); } } 

当我将代码输出到一个控制台时,你将不得不用类似System.out.println的东西replaceLOG.info。

结果:

2016-08-16 15:14:31信息15:05,15:00
2016-08-16 15:14:31信息15:06,15:00
2016-08-16 15:14:31信息15:07,15:00
2016-08-16 15:14:31信息15:08,15:15
2016-08-16 15:14:31信息15:09,15:15
2016-08-16 15:14:31信息15:10,15:15

 public static Date getCurrentDateTimeWithQuarterRounding() { final Calendar calendar = new GregorianCalendar(); calendar.setTime(new Date()); calendar.set(Calendar.MILLISECOND, 0); calendar.set(Calendar.SECOND, 0); final int minutes = calendar.get(Calendar.MINUTE); if (minutes < 15) { calendar.set(Calendar.MINUTE, 0); } else if (minutes >= 45) { calendar.set(Calendar.MINUTE, 45); } else if (minutes < 30) { calendar.set(Calendar.MINUTE, 15); } else { calendar.set(Calendar.MINUTE, 30); } return calendar.getTime(); }