Tuesday, February 22, 2011

BlueJ Program On Displaying Date In Specific Format



In this program we will take a specific date from user in dd.mm.yyyy format and number of days after that date. Out program will display the date after that specified number of days in dd.mm.yyyy format.

Sample input and output of the program

Enter the date in dd.mm.yyyy format: 21 .2.2011
Enter the number of days after: 10
Output: 3.3.2011
Enter the date in dd.mm.yyyy format: 22.2.2011
Enter the number of days after: 43
Output: 6.4.2011

Codes of the program

import java.io.*;
class Pat
{
 int day[]={31,28,31,30,31,30,31,31,30,31,30,31};
 String str1,str2;
 boolean bool;
 int i,j;
 int no1,no2,month1,year1;
 BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
 public void take()throws Exception
 {
 while(true)
 {
  System.out.println("\nEnter the date in dd.mm.yyyy format:");
  str1=br.readLine().trim();
  bool=validateFormat(str1);
  if(bool==false)
  continue;
  bool=validateDate(str1);
  if(bool==false)
  continue;
  else
  break;
 }
 i=str1.indexOf(".");
 no1=Integer.parseInt(str1.substring(0,i));
str1=str1.substring(i+1);
 i=str1.indexOf(".");
 month1=Integer.parseInt(str1.substring(0,i));
 year1=Integer.parseInt(str1.substring(i+1));
 System.out.println("\nEnter the number of days after:");
  no2=Integer.parseInt(br.readLine().trim());
  i=month1-1;
  for(j=0;j< i;j++)
  {
  no1=no1+day[j];
  if(leap(year1)==true && i==1)
  no1++;
 
 }

 no2=no2+no1;
 no1=0;
 i=0;
 while(true)
 {
  no1=no1+day[i];
  if(leap(year1)==true && i==1)
  no1++;
  if(no1 >=no2)
  {
  no1=no1-day[i];
  break;
  }
  i++;
  if(i==12)
  {
  i=0;
  year1++;
  }
  }
   no1=no2-no1;
  System.out.println("Output:"+no1+":"+(i+1)+":"+year1);
}
private boolean validateFormat(String s)
{
 int counter=0;
 int index;
 while(true)
 {
  index=s.indexOf(".");
  if(index<=0)
  break;
  else
  {
   counter++;
   s=s.substring(index+1);
  }
  }
   if (counter==2 && s.length()==4)
   return true;
   else
   return false;
  }
private boolean validateDate(String s)
{
 int counter=0;
 int li=0,d,m,y;
 int index;
  index=s.indexOf(".");
 d=Integer.parseInt(s.substring(0,index));
 s=s.substring(index+1);
  index=s.indexOf(".");
  m=Integer.parseInt(s.substring(0,index));
  s=s.substring(index+1);
  y=Integer.parseInt(s);
  if(leap(y)==true)
  li=1;
 if(m==2 && d<=day[m-1]+1)
 counter=1;
 else if(m!=2 && d<=day[m-1])
 counter=1;
 if(m>0 && m<=12)
 counter=2;
 if(counter==2)
 return true;
 else
 return false;
 }
private boolean leap(int x)
{
 if(x%100==0 && x%400==0)
 return true;
 else if(x%100!=0 && x%4==0)
 return true;
 else
 return false;
 }
 public static void main(String args[])throws Exception
 {
  Pat ob=new Pat();
  ob.take();
  }
  }

Technical analysis of the program

Number of days of each month is taken in an array in this program which will help us to count the number days. Specific date is taken from user and the number of days after that date is also taken. Function 'private boolean validateFormat(String s)' is defined in this class to check whether the date entered is in proper format, otherwise the date is again taken from user. After confirming the proper format of entry next job is to check the validity of date and month number. This job is performed using another function 'private boolean validateDate (String s)' defined in this program. When both the checking points in this programare satisfied, the date, month number and year entered are separated and the total number of days from start of the year up to that date is counter. We have taken the number of days after that date from user. Both are added and again using infinite loop days are counter from the start of the year. In this process year is also incremented when required. As soon as the counted number of days from the start of the year crosses the total number of days, the total days of the specific month is reduced from the counted days and the loop is terminated using break statement.

At the end counted number of days is deducted from total number of days and which is the date and our program displays the date. While counting number of days a function ‘private boolean leap(int x)’ is used to check for leap year as we have to count 29 days for February month in a leap year.

2 comments:

Subscribe via email

Enter your email address:

Delivered by FeedBurner