From 2cf22682e0ba08ec85d8ac4dbcd9363f7bba8989 Mon Sep 17 00:00:00 2001 From: Barzilai Spinak Date: Mon, 21 Jan 2019 22:17:00 -0300 Subject: [PATCH 1/2] minigl: add Util.nextFloor(Date, int) --- .../src/main/java/org/jpos/gl/Util.java | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/modules/minigl/src/main/java/org/jpos/gl/Util.java b/modules/minigl/src/main/java/org/jpos/gl/Util.java index 26360825b5..59ff536137 100644 --- a/modules/minigl/src/main/java/org/jpos/gl/Util.java +++ b/modules/minigl/src/main/java/org/jpos/gl/Util.java @@ -192,9 +192,10 @@ public static Date floor (Date d, int precision) { cal.set (Calendar.YEAR, 1970); } + // keep in sync with nextFloor(Date, int)! switch (precision) { case Calendar.DATE: - cal.set (Calendar.HOUR, 0); // nobreak + cal.set (Calendar.HOUR_OF_DAY, 0); // nobreak case Calendar.HOUR: case Calendar.HOUR_OF_DAY: cal.set (Calendar.MINUTE, 0); // nobreak @@ -209,6 +210,42 @@ public static Date floor (Date d, int precision) { return cal.getTime(); } + /** + * Increment the given precision unit, and set the next lower unit to ZERO. + * + * For example, if the Date d represents "Mon Jan 21 20:34:46 UYT 2019", + * then, Util.nextFloor(d, Calendar.HOUR_OF_DAY) will advance to the beginning + * of next minute, i.e. "Mon Jan 21 21:00:00 UYT 2019". + * @param d date + * @param precision is one of the Calendar constants: DATE, HOUR, HOUR_OF_DAY, MINUTE, SECOND + * (behavior for other values is undefined) + * @return converted date + * @throws NullPointerException if d is null + */ + public static Date nextFloor (Date d, int precision) { + Calendar cal = Calendar.getInstance(); + cal.setTime (d); + cal.add (precision, 1); + + // floor it! (basically what floor(Date, int) does, keep in sync! + switch (precision) { + case Calendar.DATE: + cal.set (Calendar.HOUR_OF_DAY, 0); // nobreak + case Calendar.HOUR: + case Calendar.HOUR_OF_DAY: + cal.set (Calendar.MINUTE, 0); // nobreak + case Calendar.MINUTE: + cal.set (Calendar.SECOND, 0); // nobreak + case Calendar.SECOND: + cal.set (Calendar.MILLISECOND, 0); // nobreak + // case Calendar.MILLISECOND: ??? + default: + // nothing? throw error? + } + return cal.getTime(); + } + + /** * Force date to tomorrow at 00:00:00.000 * @param d date @@ -226,4 +263,3 @@ public static Date tomorrow (Date d) { return cal.getTime(); } } - From ad584bee0329c02338f65c34ab0eeb436db2bbc9 Mon Sep 17 00:00:00 2001 From: Barzilai Spinak Date: Tue, 22 Jan 2019 00:39:24 -0300 Subject: [PATCH 2/2] fix wrong javadoc [ci skip] --- modules/minigl/src/main/java/org/jpos/gl/Util.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/minigl/src/main/java/org/jpos/gl/Util.java b/modules/minigl/src/main/java/org/jpos/gl/Util.java index 59ff536137..1bb1bc1c28 100644 --- a/modules/minigl/src/main/java/org/jpos/gl/Util.java +++ b/modules/minigl/src/main/java/org/jpos/gl/Util.java @@ -215,7 +215,9 @@ public static Date floor (Date d, int precision) { * * For example, if the Date d represents "Mon Jan 21 20:34:46 UYT 2019", * then, Util.nextFloor(d, Calendar.HOUR_OF_DAY) will advance to the beginning - * of next minute, i.e. "Mon Jan 21 21:00:00 UYT 2019". + * of next hour, i.e. "Mon Jan 21 21:00:00 UYT 2019". (notice that the next hour could very well be + * on the next day, year, etc...) + * * @param d date * @param precision is one of the Calendar constants: DATE, HOUR, HOUR_OF_DAY, MINUTE, SECOND * (behavior for other values is undefined)