Friday, 17 July 2015

Java Programming

Q). Return the number of days in a month, where month and year are given as input.

  1. package Set1;
  2. import java.util.*;
  3. public class ClassSet44 {
  4.       public static void main(String[] args){
  5.             String s1="02/2011";
  6.             System.out.println(noOfDaysInAmonth(s1));
  7.       }
  8.       public static int noOfDaysInAmonth(String s1){
  9.             int n=0;
  10.             StringTokenizer r=new StringTokenizer(s1,"/");
  11.             int n1=Integer.parseInt(r.nextToken());
  12.             int n2=Integer.parseInt(r.nextToken());
  13.             if(n1==1 || n1==3 || n1==5 ||n1==7 || n1==8 || n1==10 || n1==12)
  14.                   n=31;
  15.             else if(n1==4 || n1==6 || n1==9 || n1==11)
  16.                   n=30;
  17.             else if(n1==2){
  18.                   if(n2%4==0)
  19.                         n=29;
  20.                   else
  21.                         n=28; }
  22.             return n;
  23.       }
  24. }