Hello everyone, I think I need a second eye on the code. I'm not sure where I went wrong but it was working at some point. Everything should run without a problem any idea what is going on? A second opinion would be highly appreciated...
import java.util.Calender;
import java.util.GregorianCalendar;
public class Date
{
public static Calender cal;
public static void main(String[] args)
{
try
{
//sets value
DisplayDate(11, 27, 1998);
//display values
System.out.println("Date object constructor for date" +print());
//increments the date
for (int i = 1; i < 41; i++)
{
nextDay();
System.out.println("Incremented Date: " +print());
}
}catch (Exception e) {
}
}
public static void DisplayDate(int month, int day, int year) throws Exception
{
cal = new GregorianCalender();
cal.set(Calender.MONTH, month - 1);
cal.set(Calender.DATE, day);
cal.set(Calender.YEAR, year);
if (cal.get(Calender.YEAR) != year || cal.get(Calender.MONTH) + 1 != month || cal.get(Calender.DATE) != day)
{
System.out.println("Invalid Date");
}
}
//calculates the next day
public static void nextDay()
{
int date = cal.get(Calender.DATE);
cal.add(Calender.DATE, 1);
if (date == 31 && cal.get(Calender.DATE) == 1)
{
System.out.println("Day 32 invalid. Set to day 1. ");
}
if (date == 30 && cal.get(Calender.DATE) == 1)
{
System.out.println("Day 31 invalid. Set to day 1. ");
}
if (date == 29 && cal.get(Calender.DATE) == 1)
{
System.out.println("Day 30 invalid. Set to day 1. ");
}
if (date == 28 && cal.get(Calender.DATE) == 1)
{
System.out.println("Day 29 invalid. Set to day 1. ");
}
}
//returning string data
public static String print()
{
return String.format("%d/%d/%d", cal.get(Calender.MONTH) + 1, cal.get(Calender.DATE), cal.get(Calender.YEAR));
}
}