You could use something like this,
Main class:
public class AppMain {
public static void main(String[] args) {
MyDate initDate = new MyDate(14, 1, 2012);
printMyDate(initDate);
MyDate newDate = initDate;
do{
newDate = DateIncrementator.increaseDate(newDate, 30);
printMyDate(newDate);
}while(newDate.getYear() == initDate.getYear());
}
private static void printMyDate(MyDate date){
System.out.println("[Day: "+ date.getDay() + "][" + "[Mont: "+ date.getMonth() + "][" + "[Year: "+ date.getYear() + "]");
}
}
DateIncrementator class:
public class DateIncrementator {
private static final int[] MONTH_DAYS_NOP_LEAP_YEAR = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public static MyDate increaseDate(MyDate date, int numberOfDays){
if(numberOfDays < 1 || numberOfDays > 30){
throw new IllegalArgumentException("numberOfDays must have a value between 1 and 30");
}
int numberDaysCurrentMonth = MONTH_DAYS_NOP_LEAP_YEAR[date.getMonth() - 1];
if(date.getMonth() == 2 && isLeapYear(date.getYear())){
numberDaysCurrentMonth++;
}
int newDay = date.getDay();
int newMonth = date.getMonth();
int newYear = date.getYear();
newDay += numberOfDays;
if(newDay > numberDaysCurrentMonth){
newDay = newDay % numberDaysCurrentMonth;
newMonth++;
}
if(newMonth > 12){
newMonth = 1;
newYear++;
}
return new MyDate(newDay, newMonth, newYear);
}
private static boolean isLeapYear(int year){
if(year % 4 != 0){
return false;
}
if(year % 100 != 100){
return true;
}
if(year % 400 == 0){
return true;
}else{
return false;
}
}
}
MyDate class:
public class MyDate {
private int day; // 1 to 31
private int month; // 1 to 12
private int year; // 0 to infinite
public MyDate(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public int getDay() {
return day;
}
public int getMonth() {
return month;
}
public int getYear() {
return year;
}
}