How to calculate the day from the given date in java

import java.io.*;
import java.util.*;
class Main
  {
   public static void main(String args[]) throws IOException
     {
      System.out.println("enter date as 'dd/mm/yyyy':\n");
      DataInputStream din=new DataInputStream(System.in);
      String date=din.readLine();
      StringTokenizer st=new StringTokenizer(date,"/");
      int dd=Integer.parseInt(st.nextToken());
      int mm=Integer.parseInt(st.nextToken());
      int yyyy=Integer.parseInt(st.nextToken());
         System.out.println(" "+dd+"/"+mm+"/"+yyyy);
         Total tot=new Total();
         int total=tot.findtotal(dd,mm,yyyy);
         String day=tot.getday(total%7);
         System.out.println("the given date is '"+day+"'");
     }
  }
class Total
  {
   
    int findtotal(int date,int month,int year)
      {
       
int tot=(date+(monthcode(month))+((year%100)/4)+(year%100)+(yearcode(year/100)));
        if((((year%100)%4)==0)&&(month==01||month==02))
          {
          return(tot-1);
          }
        else
          {return(tot);
          }
       }
    int monthcode(int mon)
      {
       switch(mon)
        { case 1:return(1);
          case 10: return(1);
          case 2: return(4);
          case 3: return(4);          
          case 4: return(0);
          case 5: return(2);
          case 6: return(5);
          case 7:return(0);      
          case 8: return(3);
          case 11:return(4);  
          case 9: return(6);
          default:return(6);
         }        
        
      }
    int yearcode(int yy)
      {
       switch(yy%4)
        {
          case 0:return(6);
          case 1: return(4);
          case 2: return(2);
          case 3: return(0);
          default:return(0);
        }
      }
    String getday(int n)
      {
        switch(n)
          {
           case 1:return("SUNDAY");
          case 2: return("MONDAY");
          case 3: return("TUESDAY");
          case 4: return("WEDNESDAY");          
          case 5: return("THURSDAY");
          case 6: return("FRIDAY");
          case 0: return("SATURDAY");
          default:return("INVALID DATE");
          }
      }
}

Output of the above program is:
enter date as 'dd/mm/yyyy':
12/11/1993
 12/11/1993
the given date is 'FRIDAY'


Lets try this frenzz.............
Share on Google Plus
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment