You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Pascal Schumacher (JIRA)" <ji...@apache.org> on 2017/04/22 12:20:04 UTC
[jira] [Closed] (LANG-347) DateUtils' new addWeekdays feature
[ https://issues.apache.org/jira/browse/LANG-347?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Pascal Schumacher closed LANG-347.
----------------------------------
Resolution: Won't Fix
As discussed on the mailing list, we won't add new features related to java.util.Date. See https://mail-archives.apache.org/mod_mbox/commons-dev/201704.mbox/%3Cac296ba2-c9ef-2d6f-271e-d4699690581d%40gmx.net%3E for details.
> DateUtils' new addWeekdays feature
> ----------------------------------
>
> Key: LANG-347
> URL: https://issues.apache.org/jira/browse/LANG-347
> Project: Commons Lang
> Issue Type: New Feature
> Components: lang.time.*
> Reporter: Vasily Ivanov
>
> New method to add signed number of weekdays (skipping weekends):
> /**
> * Adds a number of weekdays (skipping weekends) to a date returning a new Date object.
> * The original date object is unchanged.
> * <p>
> * If the original Date itself is on a weekend, calculation will be started from the
> * next Monday morning (0:00:00.000) if an amount is positive or from the last Friday night
> * (23:59:59.999) otherwise.
> *
> * @param date the date, not null
> * @param amount the amount to add, may be negative
> * @return the new Date object with the amount added
> */
> public static Date addWeekdays(Date date,
> int amount)
> {
> if (date == null) {
> throw new IllegalArgumentException("The date must not be null");
> }
> Calendar c = Calendar.getInstance();
> c.setTime(date);
> if (amount != 0) {
> if (isWeekend(c)) {
> // see comments above
> final boolean isSat = getDayOfWeek(c) == Calendar.SATURDAY;
> int numToJump = 0;
> if (amount > 0) {
> // this will jump date to the closest Monday
> numToJump = isSat ? 2 : 1;
> } else {
> // this will jump date to the closest Saturday
> numToJump = isSat ? 0 : -1;
> }
> c.add(Calendar.DAY_OF_MONTH, numToJump);
> // this will jump to the start of the day (Monday or Saturday)
> modify(c, Calendar.DAY_OF_MONTH, false);
> if (amount < 0) {
> // this will go back to the end of prev Friday
> c.add(Calendar.MILLISECOND, -1);
> }
> }
> int count = 0;
> final int absAmount = Math.abs(amount);
> final int offset = amount > 0 ? 1 : -1;
> while (count < absAmount) {
> c.add(Calendar.DAY_OF_MONTH, offset);
> if (!isWeekend(c)) {
> count++;
> }
> }
> }
> return c.getTime();
> }
> public static int getDayOfWeek(Calendar c)
> {
> return c.get(Calendar.DAY_OF_WEEK);
> }
> public static boolean isWeekend(Calendar c)
> {
> final int dayOfWeek = getDayOfWeek(c);
> return dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY;
> }
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)