Get an instance of the Calendar class.
Calendar cal = GregorianCalendar.getInstance();
Declare a format to be used.
SimpleDateFormat df = new SimpleDateFormat("MMM yyyy");
Set the Calendar's current time with todays' Date.
cal.setTime(new Date());
Add a month to the Calendar's date
cal.add(Calendar.MONTH, 1);
Format the new date into a string.
String nextMonth = df.format(cal.getTime());
The nextMonth String will contain the next month's value.
The following technique allows us to show 12 consecutive months:
cal.setTime(new Date());
for(int i=0; i<12;i++){
cal.add(Calendar.MONTH, 1);
String nextMonth = df.format(cal.getTime());
System.out.println(nextMonth);
}
The Calendar class has a lot of other methods to set its date. For example, you can set the year and month as follows:
cal.set(Calendar.YEAR, 2007);
cal.set(Calendar.MONTH, 05);
or
cal.set(Calendar.MONTH, Calendar.JUNE);
This code sets the Calendar date to June 2007.
You can also set the date using year, month, and day arguments.
cal.set(2007, 05, 15);
This code sets the Calendar date to June 15, 2007.
To create an array of Dates as Strings you could use the following method:
public static String[] getDateStrings(SimpleDateFormat df, int timePeriod, int length ){
String[] arr = new String [length];
Calendar cal = GregorianCalendar.getInstance();
cal.setTime(new Date());
for(int i=0; i
cal.add(timePeriod, 1);
}
return arr;
}
The method accepts 3 arguments: SimpleDateFormat, a time period by which the Calendar object will advance, e.g. Calendar.MONTH or Calendar.DATE, and the length of the array to be returned. The array to be returned will start with today's date. Example:
SimpleDateFormat df = new SimpleDateFormat("MMM yyyy");
String[] arr = getDateStrings(df, Calendar.MONTH, 48);
The following method returns an array of calendar month names,e.g. January, February, March, etc.
public static String[] getCalendarMonthNames(){
String[] months = new String[12];
SimpleDateFormat df = new SimpleDateFormat("MMMM");
Calendar cal = GregorianCalendar.getInstance();
cal.set(Calendar.MONTH,0);
for(int i=0; i<12;i++){
months [i]= df.format(cal.getTime());
cal.add(Calendar.MONTH, 1);
}
return months;
}
If you want to return only month abbreviations, e.g.: Jan, Feb, Mar, etc. you need to change the SimpleDateFormat constructor argument:
SimpleDateFormat df = new SimpleDateFormat("MMM");
Create an ArrayList of Dates between Two Specified Dates
To create an ArrayList of Dates between 2 dates, you can use the following method:
public static ArrayList getDateStrings2(SimpleDateFormat df, int timePeriod, Calendar cal, Calendar endCal ){
ArrayList arrayList= new ArrayList();
while(cal.before(endCal)){
arrayList.add(df.format(cal.getTime()));
cal.add(timePeriod, 1);
}
//add the end date to the array
arrayList.add(df.format(endCal.getTime()));
return arrayList;
}
For this method you need to specify instances of the Calendar class that will represent start and end dates. For example:
Calendar calStart = GregorianCalendar.getInstance();
calStart.set(Calendar.YEAR, 2007);
calStart.set(Calendar.MONTH, 05);
Calendar calEnd= GregorianCalendar.getInstance();
calEnd.set(Calendar.YEAR, 2012);
calEnd.set(Calendar.MONTH, 11);
Then, you call the method as follows:
ArrayList arrayList = getDateStrings2(df, Calendar.MONTH, calStart, calEnd);