Showing posts with label LeapYear. Show all posts
Showing posts with label LeapYear. Show all posts

Friday, 17 June 2016

Program to find the Previous date of an Entered Date.

Explanation :
1)If input = 12/12/1995 then output will be 11/12/1995.
2)If input = 1/1/1995 then output will be 31/12/1994.
3)If input = 31/11/1995 then output will be Invalid as November has only 30 days.
4)If input = 29/02/1995 then output will be Invalid as 1995 is not a Leap year.
5)If input = 1/03/1996 then output will be 29/02/1996 as 1996 is a Leap year.

Code:

import java.util.*;
public class Previousday
{
   public static void display(int d,int m,int y)
   {
       System.out.print("Previous day of Entered date is:"+d+ "-"+m+"-"+y);
   }
 
   public static void main(String args[])
   {
      System.out.println("Enter the date in DD-MM-YYYY Format:");
      Scanner sc=new Scanner(System.in);
      String s=sc.next();
      sc.close();
      StringTokenizer st=new StringTokenizer(s,"-/ ");
      int d=Integer.parseInt(st.nextToken());
      int m=Integer.parseInt(st.nextToken());
      int y=Integer.parseInt(st.nextToken());
      if(d==1)
      {
          if(m==1)
          {
              d=31;m=12;y=y-1;display(d,m,y);
          }
          else if(m==3)
          {
              if((y % 400 == 0) || ((y % 4 == 0) && (y % 100 != 0)))
              {
                  d=29;m-=1;display(d,m,y);
              }
              else
              {
                  d=28;m-=1;display(d,m,y);
              }
          }
          else if(m==2 || m==4 ||m==6 ||m==8 ||m==9 || m==11)
          {
              d=31;m=m-1;display(d,m,y);
          }
          else if(m==5||m==7||m==10||m==12)
          {
              d=30;m=m-1;display(d,m,y);
          }
          else System.out.print("Invalid date Format");
      }
      else if(d>1 && d<=31)
      {
          if(m==2 && d<=29)
          {
              if(d==29)
              {
                 if((y % 400 == 0) || ((y % 4 == 0) && (y % 100 != 0)))
                 {
                   d=d-1;display(d,m,y);
                 }
                 else System.out.print("Invalid date Format");
              }
              else
              {
                  d=d-1;display(d,m,y);
              }
          }
          else if((m==1 ||m==3||m==5||m==7||m==8||m==10||m==12) && d<=31)
          {
              d=d-1;display(d,m,y);
          }
          else if((m==4||m==6||m==9||m==11) && d<=30)
          {
              d=d-1;display(d,m,y);
          }
          else System.out.print("Invalid date Format");
      }
      else System.out.print("Invalid date Format");
   }
}

Program to know whether an Year is a Leap year or not.

Code:

public class Leapyear
{
        public static void main(String[] args)  
       {               
               Scanner sc=new Scanner(System.in);
                int year = sc.nextInt();
                if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
                        System.out.println("Year " + year + " is a leap year");
                else
                        System.out.println("Year " + year + " is not a leap year");
        }
}