Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minigl: Util.floor and Util.ceil with precision param #91

Merged
merged 1 commit into from
Dec 21, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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