From d4fd773d8f8f2870f3b07322bea819739cd08d67 Mon Sep 17 00:00:00 2001 From: Barzilai Spinak Date: Fri, 21 Dec 2018 11:30:45 -0300 Subject: [PATCH] minigl: Util.floor and Util.ceil with precision param --- .../src/main/java/org/jpos/gl/Util.java | 72 +++++++++++++++++-- 1 file changed, 65 insertions(+), 7 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 a867f2090c..9faea254c4 100644 --- a/modules/minigl/src/main/java/org/jpos/gl/Util.java +++ b/modules/minigl/src/main/java/org/jpos/gl/Util.java @@ -37,13 +37,13 @@ public class Util { static { df_yyyyMMdd = (SimpleDateFormat) DateFormat.getDateTimeInstance(); df_yyyyMMdd.applyPattern("yyyyMMdd"); - df_yyyyMMddhhmmss = (SimpleDateFormat) + df_yyyyMMddhhmmss = (SimpleDateFormat) DateFormat.getDateTimeInstance(); df_yyyyMMddhhmmss.applyPattern("yyyyMMddHHmmss"); } - static SimpleDateFormat dfDate = (SimpleDateFormat) + static SimpleDateFormat dfDate = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, Locale.UK); - static SimpleDateFormat dfDateTime = (SimpleDateFormat) + static SimpleDateFormat dfDateTime = (SimpleDateFormat) DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.MEDIUM, Locale.UK); @@ -89,8 +89,8 @@ public static String dateTimeToString (Date d) { * @param attributeName attribute name * @param d date object */ - public static void setDateAttribute - (Element elem, String attributeName, Date d) + public static void setDateAttribute + (Element elem, String attributeName, Date d) { if (d != null) { elem.setAttribute (attributeName, dateToString (d)); @@ -102,13 +102,14 @@ public static String dateTimeToString (Date d) { * @param attributeName attribute name * @param d date object */ - public static void setDateTimeAttribute - (Element elem, String attributeName, Date d) + public static void setDateTimeAttribute + (Element elem, String attributeName, Date d) { if (d != null) { elem.setAttribute (attributeName, dateTimeToString (d)); } } + /** * Force the 'time' portion of a date up to 23:59:59.999 * @param d date @@ -130,6 +131,35 @@ public static Date ceil (Date d) { cal.set (Calendar.MILLISECOND, 999); return cal.getTime(); } + + public static Date ceil (Date d, int precision) { + Calendar cal = Calendar.getInstance(); + if (d != null) + cal.setTime (d); + else { + cal.set (Calendar.DAY_OF_MONTH, 31); + cal.set (Calendar.MONTH, 12); + cal.set (Calendar.YEAR, 2099); + } + + switch (precision) { + case Calendar.DATE: + cal.set (Calendar.HOUR_OF_DAY, 23); // nobreak + case Calendar.HOUR: + case Calendar.HOUR_OF_DAY: + cal.set (Calendar.MINUTE, 59); // nobreak + case Calendar.MINUTE: + cal.set (Calendar.SECOND, 59); // nobreak + case Calendar.SECOND: + case Calendar.MILLISECOND: + cal.set (Calendar.MILLISECOND, 999); // nobreak + + default: + // nothing? throw error? + } + return cal.getTime(); + } + /** * Force the 'time' portion of a date down to 00:00:00.000 * @param d date (if null, we default to 01/01/1970) @@ -151,6 +181,34 @@ public static Date floor (Date d) { cal.set (Calendar.MILLISECOND, 0); return cal.getTime(); } + + public static Date floor (Date d, int precision) { + Calendar cal = Calendar.getInstance(); + if (d != null) + cal.setTime (d); + else { + cal.set (Calendar.DAY_OF_MONTH, 1); + cal.set (Calendar.MONTH, 1); + cal.set (Calendar.YEAR, 1970); + } + + switch (precision) { + case Calendar.DATE: + cal.set (Calendar.HOUR, 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