Generating List of Dates

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Generating List of Dates

Elam Daly
Hi All,

I have the need to have a user enter in a number of pay periods, along with a starting year, and I then need to generate the corresponding date ranges for display.  So, for instance, the user inputs 52 pay periods, ie every week, and I generate a beginning date, which is the first Monday of the week to the following Sunday.

Now the problem I've run into involves when the first Monday of the year is not in the starting year, as in 2008 for instance.  The first Monday is actually Dec 31, 2007.  I've accounted for that in my code, but I would appreciate if someone could tell me if this algorithm is sound as my Joda experience is limited.

Thanks,
- Elam Daly

   private void generateDates() {

        // 2008, 2007 etc.
        int startY = Integer.valueOf( startYear.getText() );
        int periods = Integer.valueOf( numOfPeriods.getText() ).intValue();
        int numOfWeeksPerPeriod = 52/periods;

         // Start at the first monday of the year.
       DateTime startDate = new DateTime( startY, DateTimeConstants.JANUARY, DateTimeConstants.MONDAY, 0, 0, 0, 0);

       // For years(ie 2008) that the first Monday falls on the previous year.
        startDate = startDate.withDayOfWeek(DateTimeConstants.MONDAY);

        // Create rows for JTable
        Vector<Vector> rows = new Vector();
        // Row data is Pay Period, Start Date, End Date.
        Vector row = new Vector( Arrays.asList(1,startDate,startDate.plusDays(numOfWeeksPerPeriod*7-1) );
        rows.add(row);
        for(int i=2;i<=periods;i++) {     
            startDate = startDate.plusWeeks(numOfWeeksPerPeriod);
            row = new Vector( Arrays.asList(i,startDate,startDate.plusDays(numOfWeeksPerPeriod*7-1) );
            rows.add(row);
        }
        ((DefaultTableModel)groupTable.getModel()).setDataVector(rows, columns);
    }

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generating List of Dates

jodastephen
The constructor:
  new DateTime( startY, DateTimeConstants.JANUARY,
DateTimeConstants.MONDAY, 0, 0, 0, 0);
does not take the dayOfWeek as the third argument. It takes the
dayOfMonth as the third argument.

You should probably use:
  new DateTime( startY, DateTimeConstants.JANUARY, 1, 0, 0, 0, 0);
to get the first day of the year, and then:
  startDate = startDate.withDayOfWeek(DateTimeConstants.MONDAY);
to adjust to the Monday of that week. This approach will result in 6/7
start of years being in the previous year.

An alternative is to use the ISO week year algorithm (google it, eg
wikipedia). This will ensure that at least 4 days of the first week is
in the new year.

Stephen


Elam Daly wrote:

> Hi All,
>
> I have the need to have a user enter in a number of pay periods, along
> with a starting year, and I then need to generate the corresponding date
> ranges for display.  So, for instance, the user inputs 52 pay periods,
> ie every week, and I generate a beginning date, which is the first
> Monday of the week to the following Sunday.
>
> Now the problem I've run into involves when the first Monday of the year
> is not in the starting year, as in 2008 for instance.  The first Monday
> is actually Dec 31, 2007.  I've accounted for that in my code, but I
> would appreciate if someone could tell me if this algorithm is sound as
> my Joda experience is limited.
>
> Thanks,
> - Elam Daly
>
>    private void generateDates() {
>
>         // 2008, 2007 etc.
>         int startY = Integer.valueOf( startYear.getText() );
>         int periods = Integer.valueOf( numOfPeriods.getText() ).intValue();
>         int numOfWeeksPerPeriod = 52/periods;
>
>          // Start at the first monday of the year.
>        DateTime startDate = new DateTime( startY,
> DateTimeConstants.JANUARY, DateTimeConstants.MONDAY, 0, 0, 0, 0);
>
>        // For years(ie 2008) that the first Monday falls on the previous
> year.
>         startDate = startDate.withDayOfWeek(DateTimeConstants.MONDAY);
>
>         // Create rows for JTable
>         Vector<Vector> rows = new Vector();
>         // Row data is Pay Period, Start Date, End Date.
>         Vector row = new Vector(
> Arrays.asList(1,startDate,startDate.plusDays(numOfWeeksPerPeriod*7-1) );
>         rows.add(row);
>         for(int i=2;i<=periods;i++) {    
>             startDate = startDate.plusWeeks(numOfWeeksPerPeriod);
>             row = new Vector(
> Arrays.asList(i,startDate,startDate.plusDays(numOfWeeksPerPeriod*7-1) );
>             rows.add(row);
>         }
>         ((DefaultTableModel)groupTable.getModel()).setDataVector(rows,
> columns);
>     }
>
>
> ------------------------------------------------------------------------
>
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
> Build the coolest Linux based applications with Moblin SDK & win great prizes
> Grand prize is a trip for two to an Open Source event anywhere in the world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Joda-interest mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/joda-interest

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Loading...