commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henri Yandell (JIRA)" <j...@apache.org>
Subject [jira] Updated: (LANG-347) DateUtils' new addWeekdays feature
Date Fri, 03 Jul 2009 21:04:47 GMT

     [ 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.


Mime
View raw message