Skip to content

Commit

Permalink
Merge pull request #91 from barspi/master
Browse files Browse the repository at this point in the history
minigl: Util.floor and Util.ceil with precision param
  • Loading branch information
ar authored Dec 21, 2018
2 parents 83ba8ac + d4fd773 commit 0bf931f
Showing 1 changed file with 65 additions and 7 deletions.
72 changes: 65 additions & 7 deletions modules/minigl/src/main/java/org/jpos/gl/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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));
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 0bf931f

Please # to comment.