Skip to content

Commit

Permalink
Merge pull request #93 from barspi/master
Browse files Browse the repository at this point in the history
minigl: add Util.nextFloor(Date, int)
  • Loading branch information
ar authored Jan 22, 2019
2 parents 14079dc + ad584be commit 0bb4376
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions modules/minigl/src/main/java/org/jpos/gl/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -209,6 +210,44 @@ 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 <code>Date d</code> represents "Mon Jan 21 20:34:46 UYT 2019",
* then, <code>Util.nextFloor(d, Calendar.HOUR_OF_DAY)</code> will advance to the beginning
* 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)
* @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
Expand All @@ -226,4 +265,3 @@ public static Date tomorrow (Date d) {
return cal.getTime();
}
}

0 comments on commit 0bb4376

Please # to comment.