You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Sean Schofield <se...@schof.com> on 2004/09/11 19:08:14 UTC

[lang] Enhancement proposal for DateUtils

I have several Date related utility methods that I've been using in a 
project I'm working on.  I thought I would offer them to the group because I 
think they are useful.  I plan on submitting a patch shortly after I finish 
up the unit tests.  If there are any objections to these new methods, please 
let me know before I go to the trouble of making the patch.

The first two methods I'd like to add are:

isWeekend(Date):boolean
adjustForWeekends(Date):Date

I'm currently working on a project for the government and they have certain 
deadlines that may or may not fall on the weekend.  If the deadlines fall on 
a weekend, the managers want to know and plan accordingly.  The 
adjustForWeekends method will move the original date backwards until it is 
no longer a weekend.

I think this would be a useful set of methods to add.  The only issue I can 
see is that it assumes a GregorianCalendar (which is documented in the 
javadoc for the method.)

Any thoughts on this?

sean 



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [lang] Enhancement proposal for DateUtils

Posted by Sean Schofield <se...@schof.com>.
OK,

I gave the weekend adjustment thing some thought.  What if we included the 
following methods?  (NOTE: javadoc, patches, and unit tests will be provided 
later if we go with this idea.)

Thoughts?
sean


    public static int[] SATURDAY_SUNDAY = {Calendar.SATURDAY, 
Calendar.SUNDAY};

    public static Date adjustUntilNot(Date originalDate, int dayOfWeek, int 
increment) {
        int[] dateArray = {dayOfWeek};
        return adjustUntilNot(originalDate, dateArray, increment);
    }

    public static Date adjustUntilNot(Date originalDate, int[] daysOfWeek, 
int increment) {
        Date adjustedDate = originalDate;
        Calendar calendar = new GregorianCalendar();

        while (fallsOn(adjustedDate, daysOfWeek)) {
            calendar.add(Calendar.DATE, increment);
            adjustedDate = calendar.getTime();
        }

        return adjustedDate;
    }

    public static boolean fallsOn(Date date, int dayToCheck) {
        int[] daysToCheck = {dayToCheck};
        return fallsOn(date, daysToCheck);
    }

    public static boolean fallsOn(Date date, int[] daysToCheck) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

        for (int i=0; i < daysToCheck.length; i++) {
            if (dayOfWeek == daysToCheck[i]) {
                return true;
            }
        }

        return false;
    }

----- Original Message ----- 
From: "Stephen Colebourne" <sc...@btopenworld.com>
To: "Jakarta Commons Developers List" <co...@jakarta.apache.org>
Sent: Sunday, September 12, 2004 10:06 AM
Subject: Re: [lang] Enhancement proposal for DateUtils


> Its a point for debate really. It might be OK to define SatSun weekends as 
> a
> default, but an API should probably be framed to allow the avoidance of 
> any
> day of week combination. And that can get to be a tricky API to define.
>
> Stephen
>



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


RE: [lang] Enhancement proposal for DateUtils

Posted by Rory Winston <rw...@eircom.net>.
This is a good point. I spent some time working on a project in Saudi
Arabia, and our "weekend" was Wednesday and Thursday. Which is not much fun
when the UK and US offices are working on a Sat/Sun weekend (which basically
means you end up working a seven-day week)... :)


Rory

-----Original Message-----
From: Stephen Colebourne [mailto:scolebourne@btopenworld.com]
Sent: 12 September 2004 15:06
To: Jakarta Commons Developers List
Subject: Re: [lang] Enhancement proposal for DateUtils


Its a point for debate really. It might be OK to define SatSun weekends as a
default, but an API should probably be framed to allow the avoidance of any
day of week combination. And that can get to be a tricky API to define.

Stephen

----- Original Message -----
From: "Sean Schofield" <se...@schof.com>
> I agree that this limits you to 'western' calendars.  Do non-western
> calendars have the concept of a weeekend?  I think there are enough people
> using the Gregorian calendar for these methods to be useful.  I guess it
> depends on the overall policy of whether things that go in commons lang
need
> to be country neutral.  Is that the policy/general practice?
>
> Don't some of the constants and methods in DateUtils implictly assume a
> Gregorian calendar?  Are you opposed to including these methods or are you
> just throwing the issue out there for debate?
>
> sean
>
>
> > The main problem is that the concept of weekday/weekend as Mon-Fri and
> > Sat-Sun is a 'western' one. There is a significant portion of the world
> > that
> > uses other measures.
> >
> > Stephen
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [lang] Enhancement proposal for DateUtils

Posted by Stephen Colebourne <sc...@btopenworld.com>.
Its a point for debate really. It might be OK to define SatSun weekends as a
default, but an API should probably be framed to allow the avoidance of any
day of week combination. And that can get to be a tricky API to define.

Stephen

----- Original Message -----
From: "Sean Schofield" <se...@schof.com>
> I agree that this limits you to 'western' calendars.  Do non-western
> calendars have the concept of a weeekend?  I think there are enough people
> using the Gregorian calendar for these methods to be useful.  I guess it
> depends on the overall policy of whether things that go in commons lang
need
> to be country neutral.  Is that the policy/general practice?
>
> Don't some of the constants and methods in DateUtils implictly assume a
> Gregorian calendar?  Are you opposed to including these methods or are you
> just throwing the issue out there for debate?
>
> sean
>
>
> > The main problem is that the concept of weekday/weekend as Mon-Fri and
> > Sat-Sun is a 'western' one. There is a significant portion of the world
> > that
> > uses other measures.
> >
> > Stephen
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [lang] Enhancement proposal for DateUtils

Posted by Sean Schofield <se...@schof.com>.
Stephen,

I agree that this limits you to 'western' calendars.  Do non-western 
calendars have the concept of a weeekend?  I think there are enough people 
using the Gregorian calendar for these methods to be useful.  I guess it 
depends on the overall policy of whether things that go in commons lang need 
to be country neutral.  Is that the policy/general practice?

Don't some of the constants and methods in DateUtils implictly assume a 
Gregorian calendar?  Are you opposed to including these methods or are you 
just throwing the issue out there for debate?

sean


> The main problem is that the concept of weekday/weekend as Mon-Fri and
> Sat-Sun is a 'western' one. There is a significant portion of the world 
> that
> uses other measures.
>
> Stephen



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [lang] Enhancement proposal for DateUtils

Posted by Stephen Colebourne <sc...@btopenworld.com>.
The main problem is that the concept of weekday/weekend as Mon-Fri and
Sat-Sun is a 'western' one. There is a significant portion of the world that
uses other measures.

Stephen

----- Original Message -----
From: "Sean Schofield" <se...@schof.com>
> Good point.  Adjusting backwards is really specific to my application's
> needs.  I can see other applications want to adjust to next weekday.
>
> ----- Original Message -----
> > I would think that, in general, you would want the ability to adjust
dates
> > either forward or backward for weekends.  I.e. "1st weekday of the
month"
> > or "last weekday of the month".  "15th of the month, adjusted" might go
> > either way.
> >
> > Probably two methods, "adjustForwardToWeekday()" and
> > "adjustBackwardToWeekday", although alternately "nextWeekday" and
> > "previousWeekday" could work, with
> >  Date adjustForwardToWeekday(Date d) {
> >    Date n = d.nextWeekday();
> >    return n.previousWeekday().equals(d) ? d : n;
> >  }
> > etc.
> > --scott
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [lang] Enhancement proposal for DateUtils

Posted by Sean Schofield <se...@schof.com>.
Good point.  Adjusting backwards is really specific to my application's 
needs.  I can see other applications want to adjust to next weekday.

----- Original Message ----- 
> I would think that, in general, you would want the ability to adjust dates
> either forward or backward for weekends.  I.e. "1st weekday of the month"
> or "last weekday of the month".  "15th of the month, adjusted" might go
> either way.
>
> Probably two methods, "adjustForwardToWeekday()" and
> "adjustBackwardToWeekday", although alternately "nextWeekday" and
> "previousWeekday" could work, with
>  Date adjustForwardToWeekday(Date d) {
>    Date n = d.nextWeekday();
>    return n.previousWeekday().equals(d) ? d : n;
>  }
> etc.
> --scott



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [lang] Enhancement proposal for DateUtils

Posted by "C. Scott Ananian" <cs...@cscott.net>.
On Sat, 11 Sep 2004, Sean Schofield wrote:

> adjustForWeekends(Date):Date

> a weekend, the managers want to know and plan accordingly.  The
> adjustForWeekends method will move the original date backwards until it is
> no longer a weekend.

I would think that, in general, you would want the ability to adjust dates
either forward or backward for weekends.  I.e. "1st weekday of the month"
or "last weekday of the month".  "15th of the month, adjusted" might go
either way.

Probably two methods, "adjustForwardToWeekday()" and
"adjustBackwardToWeekday", although alternately "nextWeekday" and
"previousWeekday" could work, with
  Date adjustForwardToWeekday(Date d) {
    Date n = d.nextWeekday();
    return n.previousWeekday().equals(d) ? d : n;
  }
etc.
 --scott

Panama Sugar Grove Yakima KUPALM supercomputer ODYOKE AEFOXTROT Mossad
   Register to vote!  http://www.yourvotematters.org/VerifiedVoting
                         ( http://cscott.net/ )

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org