You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Henri Yandell (JIRA)" <ji...@apache.org> on 2009/07/03 23:04:47 UTC

[jira] Updated: (LANG-347) DateUtils' new addWeekdays feature

     [ https://issues.apache.org/jira/browse/LANG-347?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated LANG-347:
-------------------------------

    Affects Version/s:     (was: 2.3)
        Fix Version/s: Commons Time?

Possible feature for a library on top of Joda time/Java 7 if this is reimagined as a DateSequence concept. 

> DateUtils' new addWeekdays feature
> ----------------------------------
>
>                 Key: LANG-347
>                 URL: https://issues.apache.org/jira/browse/LANG-347
>             Project: Commons Lang
>          Issue Type: New Feature
>            Reporter: Vasily Ivanov
>             Fix For: Commons Time?
>
>
> 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 is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.