static public List < DateTimePeriod > createLastYearMonths ( DateTimeZone zone ) { ArrayList < DateTimePeriod > periods = new ArrayList < DateTimePeriod > ( ) ; // get today's date DateTime now = new DateTime ( zone ) ; // start with today's current month and 11 others (last 12 months) for ( int i = 0 ; i < 12 ; i ++ ) { // create a new period DateTimePeriod period = createMonth ( now . getYear ( ) , now . getMonthOfYear ( ) , zone ) ; periods . add ( period ) ; // subtract 1 month now = now . minusMonths ( 1 ) ; } return periods ; }
Create a list of DateTimePeriods that represent the last year of YearMonth periods . For example if its currently January 2009 this would return periods representing January 2009 December 2008 ... February 2008