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));
			}
}

i made my own version for calendar use zongrens congruence theorem for days and it works

import java.util.*;
public class Calendar {
	public static void main(String args[]){
		int startingDay = 0,date,month,century,k,year,sub1,sub2,sub3;
		String monthS = "";
		Scanner key = new Scanner(System.in);
		System.out.println("enter year: (eg 2008)");
		year = key.nextInt();
		System.out.println("enter month 1-12");
		month = key.nextInt();
		date =1;
		switch(month){
		case 1: monthS = "January"; break;
		case 2: monthS = "February"; break;
		case 3: monthS = "March"; break;
		case 4: monthS = "April"; break;
		case 5: monthS = "May"; break;
		case 6: monthS = "June"; break;
		case 7: monthS = "July"; break;
		case 8: monthS = "August"; break;
		case 9: monthS = "September"; break;
		case 10: monthS = "October"; break;
		case 11: monthS = "November"; break;
		case 12: monthS = "December"; break;
		}

		int numMonthDays;
		boolean leapYear = false;
		k = year %100;
		century= (year/100);
		if(month==1)
			month = 13;
		else if (month==2)
			month =14;




		sub1 = (26*(month+1))/10;
		sub2 = k/4;
		sub3 = century/4;
		startingDay = (date + sub1+k+sub2+sub3+5*century)%7;


		if (year% 400==0 || year%4==0 && year%100!=0)
			leapYear = true;


		int numberOfDays = 0;

		for(int i = 1800; i < year; i++){
			if(leapYear)
				numberOfDays+=366;
			else
				numberOfDays+=365;
		}
		if(month==13 || month==3 || month ==5 || month==7||month==8||month==10||month==12)
			numMonthDays= 31;
		else if(month==4||month==6||month==9||month==11)
			numMonthDays= 30;
		else if (month==14 && leapYear) 
			numMonthDays = 29;
		else
			numMonthDays = 28;

		int startDay1;
		if (startingDay==0)
			startDay1 = startingDay;
		else if (startingDay==1)
			startDay1 = startingDay;
		else if (startingDay==2)
			startDay1=startingDay;
		else if (startingDay==3)
			startDay1=startingDay;
		else if (startingDay==4)
			startDay1=startingDay;
		else if (startingDay==5)
			startDay1=startingDay;
		else  
			startDay1=6;
		int a = 0;
		for (a = 0; a <=startDay1; a++ ){
			System.out.println("           "+ monthS);

			System.out.println("  sun mon tue wed thu fri sat");
			System.out.println("-----------------------------");
			if (startDay1 ==0)
				System.out.print("                        ");
			else if(startDay1 ==1)
				System.out.print("");
			else if(startDay1 ==2)
				System.out.print("    ");
			else if(startDay1 ==3)
				System.out.print("        ");
			else if(startDay1 ==4)
				System.out.print("            ");
			else if (startDay1 ==5)
				System.out.print("                ");
			else if (startDay1==6)
				System.out.print("                    ");

			for(a=startDay1; a< numMonthDays + startDay1; a++){
				System.out.printf("%4d", a - startDay1 + 1);
				if ((a)%7==0)
					System.out.println();
			}
			System.out.println();
		}
		System.out.println("-----------------------------");
	}
}

Not sure ... I think I'm missing a brace some where but I cant find where it is missing one.

ok i indented your thing properly follow the braces

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));
	}
}

omg i figured out your problem

public static Calender cal;

since class calendar is imported you have to do this Calendar cal = new Calendar();

omg i figured out your problem

public static Calender cal;

since class calendar is imported you have to do this Calendar cal = new Calendar();

Not sure what you are referring too... I added the new method has you've mentioned but it didn't resolve the problem...

Edited 5 Years Ago by DaniwebOS: n/a

This article has been dead for over six months. Start a new discussion instead.