commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Serge Knystautas" <>
Subject Re: [lang] status of CalendarUtils proposal
Date Tue, 03 Sep 2002 04:52:32 GMT
Steven et al,

Here is my much detailed CalendarUtils code... I finally got the time to
spend the past several hours writing up some JUnit tests (and caught a few
bugs)... the testing isn't so much fun to write since the logic is often
based on the current time or there is a bunch of ugly logic figuring out
which day is Monday, etc... (which is why I wrote the dang class in the
first place).

Anyway, 2 java files are attached... one is and one is  I've packaged them per the other lang utils and put
the license and everything set (you might want to repackage it... I forget
whether you wanted this class in lang or utils or wherever).  But I've been
having the two classes in the lang directory and building (with the slight
change to LangTestSuite to include the new test class), and everything's
working nicely.

Here's the highlights of what this class does:

- trunc(Date val, int unit) and trunc(Calendar val, int unit)
These two do the same thing... the unit is something like Calendar.DATE or
Calendar.MINUTE.  trunc(new Date("February 18, 2002", Calendar.MONTH) gives
you February 1, 2002.

- round(Date val, int unit) and round(Calendar val, int unit)
Same as trunc, except it rounds instead of truncates.  trunc(new
Date("February 18, 2002", Calendar.MONTH) gives you March 1, 2002.

- CalendarUtils.SEMI_MONTH
This was particularly useful for me with the trunc/round functions since we
have to know when the mid-month is.  So I can do trunc(new Date("February
18, 2002"), CalendarUtils.SEMI_MONTH) and get February 16, 2002.  This was
useful since we have semi monthly pay periods.

- parse(String)
This was built with the intention of handling all possible date values that
CVS can handle.  This includes:
  last Sunday
  last week
  last February
  two minutes ago
  two weeks ago
  4:13 pm
  January 4, 2002 3:23 am
  8/25/1972 6:23:00 EST
Still could use some work, but it's getting there.  There's untested support
for multiple locales so you can use non-american month and week names.

- getCalendarIterator(Calendar focus, int range) and
getCalendarIterator(Date focus, int range)
I wrote this to make it easier to build week and month calendar views.  You
pass it a date (as a Calendar or Date) and then a range like
RANGE_WEEK_SUNDAY or RANGE_MONTH_SUNDAY, and you get back an Iterator of
Calendar objects.  The week-based ranges will give you the 7 days
surrounding the focus date... RANGE_WEEK_SUNDAY would start on the Sunday
and iterate over the days until you get to Saturday.  RANGE_MONTH_SUNDAY
would find the Sunday before or on the first of the month and iterate
through all days in that month until you get to the Saturday on or after the
last day of the month.

Let me know if you have any questions...

Serge Knystautas
Loki Technologies - Unstoppable Websites

----- Original Message -----
From: "Steven Caswell" <>
To: "Jakarta Commons Developers List" <>
Sent: Sunday, September 01, 2002 6:07 PM
Subject: [lang] status of CalendarUtils proposal

> A couple of questions about the proposed CalendarUtils from early last
> month:
> - Did the proposed CalendarUtils get committed into cvs? I couldn't find
> it anywhere. I've got a version of CalendarUtils and also a DateUtils to
> submit, and I want to compare to Serge's version of CalendarUtils and
> propose an API for both.
> - Did we decide where something like CalendarUtils should go? Same
> question for a DateUtils class. Are we too close to a 1.0 release to add
> new stuff into lang at this point?
> Steven Caswell

View raw message