0

I am trying to create a method that counts the number of each full months in an array. my array contains the dates in MM/dd/yyyy format. The array can contain dates starting from any date and ending at any date. The dates are in order from oldest to newest and only contain weekdays.

For example, if the array has dates from 6/15/2010 to 1/15/2012, it would return an array of: jan = 1, feb = 1, mar = 1, apr = 1, may = 1, jun = 1, jul = 2, aug = 2, sep = 2, oct = 2, nov = 2, dec = 2

I was hoping someone could give me some hints, here is what i have so far:

double[] months = new double[12];

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
SimpleDateFormat yyyy = new SimpleDateFormat("yyyy");

for(int ii = 0 ; ii < array.length-length ; ii++){

        date = (Date)formatter.parse(array[ii][0]);
        String currentDateMY = sdf.format(date);
        String currentDateYYYY = yyyy.format(date);

        date = (Date)formatter.parse(array[ii+1][0]);
        String nextDateMY = sdf.format(date);

            if (!currentDateMY.equals(nextDateMY)){

                date = (Date)formatter.parse(array[ii][0]);
                sdf = new SimpleDateFormat("MM");
                String currentDateM = sdf.format(date);

                if (currentDateM.equals("01")) months[0] = jan++;
                if (currentDateM.equals("02")) months[1] = feb++;
                if (currentDateM.equals("03")) months[2] = mar++;
                if (currentDateM.equals("04")) months[3] = apr++;
                if (currentDateM.equals("05")) months[4] = may++;
                if (currentDateM.equals("06")) months[5] = jun++;
                if (currentDateM.equals("07")) months[6] = jul++;
                if (currentDateM.equals("08")) months[7] = aug++;
                if (currentDateM.equals("09")) months[8] = sep++;
                if (currentDateM.equals("10")) months[9] = oct++;
                if (currentDateM.equals("11")) months[10] = nov++;
                if (currentDateM.equals("12")) months[11] = dec++;
            }
}
Mike
  • 2,299
  • 13
  • 49
  • 71

1 Answers1

1

Brain-dead way of doing it:

  1. Create a GregorianCalendar with the start date and end dates. (put in 1 for the starting day_of_month, and GregorianCalendar.getActualMaximum(DAY_OF_MONTH) for the starting day_of_month for the end date)
  2. Keep adding a month to the start date (GregorianCalendar.add(MONTH, 1)) and check to see if it's after() the end date. If not, increment that month's counter.
  3. Add 1 for each the start and end, if the actual start date's day_of_month component is 1, and if the actual end date's day_of_month is equal to GregorianCalendar.getActualMaximum(DAY_OF_MONTH).

If the start day is a Monday, and you want to include the preceding weekend, you can put in checks for that (similarly for the end date being a Friday).

A smarter way of doing it:

Get the difference in years between the dates. Increment each month by the difference-2, if indeed it is greater. Then check if the starting month is before or after the ending month, and increment the ones in between accordingly. Check if the starting and ending months themselves are included as above.

  • yeah, you should always look for already-made classes so you don't have to re-invent the wheel. Especially for a beginner. – Jimmt Jan 27 '12 at 04:53