Date Book Program in Java
Posted by rjndhl1
Last Updated: January 23, 2015

Date Book Program in java

// Date Book Program

import java.util.Scanner;

public class DateBook {
	public static boolean isLeapYear(int Year){
	   boolean leapYear = false;
	   if ( Year % 4 ==0) {
		  leapYear = true; 
		  if (Year % 100 ==0){
			  if (Year % 400 == 0)
				  leapYear = true;
			  else 
				  leapYear = false;
		  }	  
	   }	  
	 
		if (leapYear)
		   return true;
	   else 
		   return false;
    }
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter Date in the formt yyyy mm dd :");
		String date = input.next();

		String yearStr = date.substring(0, 4);
		String monthStr = date.substring(5, 7);
		String dayStr = date.substring(8, 10);

		int year = Integer.parseInt(yearStr);
		int month = Integer.parseInt(monthStr);
		int day = Integer.parseInt(dayStr);

		System.out.println("year is " + year);
		System.out.println("month is " + month);
		System.out.println("day is " + day);

		System.out.print(" Enter the number of days");
		int numDays = input.nextInt();
		input.close();

		
	for( int i=1;i<=numDays;i++){
		
	
		day++;
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:

			if (day > 31) {
				day = 1;
				month++;
			}
			break;
		case 2:
			if (isLeapYear(year)) {
			    if (day > 29) {
				   day = 1;
				   month++;
			    }
			}    
		    else {
			   	if (day > 28) {
			   	   day = 1;
				   month++;
			    }
			    	
			    }
			    
			    
			
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			if (day > 30) {
				day = 1;
				month++;
			}
			break;
	
		}
		if (month > 12) {
			month = 1;
			year++; 
				
		}
		
	}
			

		System.out.printf(" The next day is: %d-%d-%d", year, month, day);
		 
	}
	
	
}


import java.util.Scanner;

 

public class DateBook {

public static boolean isLeapYear(int Year){

boolean leapYear = false;

if ( Year % 4 ==0) {

leapYear = true;

if (Year % 100 ==0){

if (Year % 400 == 0)

leapYear = true;

else

leapYear = false;

}

}

 

if (leapYear)

return true;

else

return false;

}

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter Date in the formt yyyy mm dd :");

String date = input.next();

 

String yearStr = date.substring(0, 4);

String monthStr = date.substring(5, 7);

String dayStr = date.substring(8, 10);

 

int year = Integer.parseInt(yearStr);

int month = Integer.parseInt(monthStr);

int day = Integer.parseInt(dayStr);

 

System.out.println("year is " + year);

System.out.println("month is " + month);

System.out.println("day is " + day);

 

System.out.print(" Enter the number of days");

int numDays = input.nextInt();

input.close();

 

 

for( int i=1;i<=numDays;i++){

 

 

day++;

switch (month) {

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

 

if (day > 31) {

day = 1;

month++;

}

break;

case 2:

if (isLeapYear(year)) {

if (day > 29) {

day = 1;

month++;

}

}

else {

if (day > 28) {

day = 1;

month++;

}

 

}

 

 

 

break;

case 4:

case 6:

case 9:

case 11:

if (day > 30) {

day = 1;

month++;

}

break;

 

}

if (month > 12) {

month = 1;

year++;

 

}

 

}

 

 

System.out.printf(" The next day is: %d-%d-%d", year, month, day);

 

}

 

 

}

 

Related Content