Advertisement
remote87

Check Days of month in a given year

Nov 24th, 2020
736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner sc = new Scanner(System.in);
  9.         int month = Integer.parseInt(sc.nextLine());
  10.         int year = Integer.parseInt(sc.nextLine());
  11.  
  12.         //check if year is between 1 and 9999
  13.         if(year <= 0 || year > 9999){
  14.             System.out.println("Wrong year!");
  15.         //check if month is between 1 and 12
  16.         }else if(month <= 0 || month > 12){
  17.             System.out.println("Wrong month!");
  18.         }else{
  19.             System.out.println(daysInMonth(month, year));
  20.         }
  21.     }
  22.  
  23.     public static int daysInMonth(int month, int year){
  24.  
  25.         boolean dividedByFour = year % 4 == 0;
  26.         boolean dividedByOneHundred = year % 100 != 0;
  27.         boolean dividedByFourHundred = year % 400 == 0;
  28.  
  29.         int[] month31Days = {1, 3, 5, 7, 8, 10, 12};
  30.  
  31.         if(dividedByFour && dividedByOneHundred){
  32.             System.out.println("Leap year");
  33.             for(int i = 0; i < month31Days.length; i++){
  34.                 if(month == month31Days[i]) return 31;
  35.                 else if (month == 2) return 29;
  36.                 else return 30;
  37.             }
  38.         }else if(dividedByFour && dividedByFourHundred){
  39.             System.out.println("Leap year");
  40.             for(int i = 0; i < month31Days.length; i++){
  41.                 if(month == month31Days[i]) return 31;
  42.                 else if (month == 2) return 29;
  43.                 else return 30;
  44.             }
  45.         }
  46.  
  47.         System.out.println("Not a leap year");
  48.         for(int i = 0; i < month31Days.length; i++){
  49.             if(month == month31Days[i]) return 31;
  50.             else if (month == 2) return 28;
  51.         }
  52.         return 30;
  53.     }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement