Wednesday, 12 November 2014

checking validity of the date



//using bluej
class Valid_Date
{
public static void main(int dd,int mm,int yy)
{
int k=0,e=0;
int m[]={30,28,31,30,31,30,31,31,30,31,30,31};
k=(yy%400==0)?yy%100==0?1:0:yy%4==0?1:0;
m[k]=m[k]+k;
if(mm<1 || mm>12)
{
e=1;
System.out.println(mm+"is invalid month");
}
if(dd<0 || dd>m[mm-1])
{
e=1;
System.out.println(dd+" is invalid date");
}
if(yy<1)
{
e=1;
System.out.println(yy+" is invalid year");
}
if(e==0)
System.out.println(dd+"-"+mm+"-"+yy+" is valid date");
}
}

Sunday, 9 November 2014

To find days elapsed between two valid dates

//To find days elapsed between two valid dates
import java.util.* ;
class DateQ2_2000 {
static int daysOfMonth[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
static void validateInput(int d,int m,int y){ // validates the input date
if(y<1 || y>9999) {
System.out.println("INVALID YEAR");
System.exit(0);

}
if(m<1 || m>12) {
System.out.println("INVALID MONTH");
System.exit(0);
}
if(d<1 || d>daysOfMonth[m]) {
System.out.println("INVALID DAY");
System.exit(0);

}
}
static boolean isLeap(int y){
if((y%4==0 && y%100!=0)|| y%400==0) return true;
return false;
}
static int daysBefore(int d,int m,int y){
// finds days elapsed from start date till end of first year
int diff=0,i;
if(isLeap(y))
daysOfMonth[2]+=1; // check for days in February
diff=daysOfMonth[m]-d; // find remaining last days of start month
for(i=m+1;i<=12;i++)
diff+=daysOfMonth[i]; // find days till last month of first year
return diff;
}
static int daysBetween(int y1,int y2){
// finds days elapsed between start year and finish year
int diff=0,i;
for(i=y1+1;i<=y2-1;i++)
if(isLeap(i)) diff+=366;
else diff+=365;
return diff;
}
static int daysAfter(int d,int m,int y){
// finds days elapsed from beginning of finish year till finish date
int diff=0,i;
if(isLeap(y))
daysOfMonth[2]+=1; // check for days in February
for(i=1;i<m;i++)
diff+=daysOfMonth[i]; // find days till last month of first year
diff+=d;
return diff;
}
public static void main(String args []) throws Exception {
Scanner sc=new Scanner(System.in);
int dd1,mm1,yy1,dd2,mm2,yy2,i,days;
System.out.print("First Date:\t");
System.out.print("\tDay: ");
dd1=sc.nextInt();
System.out.print("\t\t\t\tMonth: ");
mm1=sc.nextInt();
System.out.print("\t\t\t\tYear: ");
yy1=sc.nextInt();
System.out.print("\nSecond Date:\t");
System.out.print("Day: ");
dd2=sc.nextInt();
System.out.print("\t\t\t\tMonth: ");
mm2=sc.nextInt();
System.out.print("\t\t\t\tYear: ");
yy2=sc.nextInt();
days=0;
validateInput(dd1,mm1,yy1); // validate first date
validateInput(dd2,mm2,yy2); // validate second date
if(yy1==yy2){ // if dates are of same year
if(isLeap(yy1))
daysOfMonth[2]+=1; // check for days in February
days=daysOfMonth[mm1]-dd1; // find remaining last days of start month
for(i=mm1+1;i<mm2;i++)
days+=daysOfMonth[i]; // find days between start and finish month
days+=dd2; // add beginning days of finish month
}
else { //  dates are of different years

int diff1=daysBefore(dd1,mm1,yy1);
int diff2=daysBetween(yy1,yy2);
int diff3=daysAfter(dd2,mm2,yy2);
days=diff1+diff2+diff3;
}
System.out.println("\nOUTPUT :");
System.out.print("\nDays elapsed: "+days);
} // end of main
} // end of class 

To accept day number, year, and N and display the corresponding date and future date in words

// To accept day number, year, and N and display the corresponding date and future date
// date in words (Q1-2009)
import java.util.* ;
class DateQ1_2009 {
static void validateInput(int d,int y,int n){
if(d<1 || d>366) {
System.out.println("INVALID DAY NUMBER");
System.exit(0);
}
if(y<1000 || y>9999) {
System.out.println("INVALID YEAR");
System.exit(0);
}
if(n<2 || n>100) {
System.out.println("INVALID N VALUE");
System.exit(0);
}
}
static int isLeap(int y){
if((y%4==0 && y%100!=0)|| y%400==0) return 1;
return 0;
}
static void printDate(int dd,int mm,int yy){
String monthOfYear[]={"","January","February","March","April","May","June","July","August",
"September","October","November","December"};
String suffix,date;
if(dd%10==1) suffix="st "; // for days that end with 1
else if(dd%10==2) suffix="nd "; // for days that end with 2
else if(dd%10==3) suffix="rd "; // for days that end with 3
else suffix="th "; // for other days
if(dd>10 && dd<14) suffix="th "; // for days 11, 12, and 13
date=dd+suffix+monthOfYear[mm]+" "+yy; // storing date in full form
System.out.println(date);
}
public static void main(String args []) throws Exception {
Scanner sc=new Scanner(System.in);
int daysOfMonth[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int daynum,dd,mm,yy,n;
System.out.print("DAY NUMBER\t: ");
daynum=sc.nextInt();
System.out.print("YEAR\t: ");
yy=sc.nextInt();
System.out.print("DAY AFTER\t: ");
n=sc.nextInt();
validateInput(daynum,yy,n); // validate input data
dd=daynum;
daysOfMonth[2]+=isLeap(yy); // check for days in February
// converts input day number into day, month, and year
for(mm=1;mm<=12 && dd>28;mm++)
dd-=daysOfMonth[mm]; // finds the day of the month
System.out.println("\nOUTPUT :");
printDate(dd,mm,yy); // display current date in words
// finds the date after n days
dd+=n; // add working days to the day number
while(dd>28){
dd-=daysOfMonth[mm]; // finds the day of the year
++mm; // update the month
if(mm>12){
yy++; // update year
mm=1; // start from the first month of new year
daysOfMonth[2]+=isLeap(yy);
}
}
System.out.print("\nDATE AFTER "+n+" DAYS\t: ");
printDate(dd,mm,yy); // display future date in words

} // end of main
} // end of class 

To accept date, day name on 1st January and display the day name for the given date in words

// To accept date, day name on 1st January and display the day name for the given date
// date in words
import java.util.* ;
class DateQ1_2007 {
static int daysOfMonth[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
static int dd,mm,yy, wkday;
static void validateInput(){ // validates the input date and week day
if(yy<1 || yy>9999) { // validates year
System.out.println("INVALID YEAR");
System.exit(0);

}
if(mm<1 || mm>12) { // validates month
System.out.println("INVALID MONTH");
System.exit(0);
}
if(dd<1 || dd>daysOfMonth[mm]) { // validates day
System.out.println("INVALID DAY");
System.exit(0);

}
if(wkday<1 || wkday>7) {
System.out.println("INVALID WEEK DAY");
System.exit(0);
}
}
static int isLeap(int y){
if((y%4==0 && y%100!=0)|| y%400==0) return 1;
return 0;
}
static void extractDate(String date) // separates day, month, and year from the entered date
{
StringTokenizer st=new StringTokenizer(date,"/");
for(int i=3;st.hasMoreTokens();i--) {
if(i>2) dd=Integer.parseInt(st.nextToken()); // store day
else if(i>1)mm=Integer.parseInt(st.nextToken()); // store month
else yy=Integer.parseInt(st.nextToken()); // store year
}
}
public static void main(String args []) throws Exception {
Scanner sc=new Scanner(System.in);
String daysOfWeek[]={"","SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY",
};
String date,firstday;
int days,i;
System.out.print("Date\t: ");
date=sc.next();
System.out.print("Day on 1st January\t: ");
firstday=sc.next();
extractDate(date); // separates day, month, and year
for(i=1;i<=7;i++)
if(firstday.equalsIgnoreCase(daysOfWeek[i])){
wkday=i; // store week day number(1-7)
break;
}
validateInput(); // validate input data
daysOfMonth[2]+=isLeap(yy); // check for days in February
// converts input day number into day, month, and year
days=0;
for(i=1;i<mm;i++)
days+=daysOfMonth[i]; // finds total days elapsed before the target month
days+=dd-1; // finds total days elapsed before the target day
days+=wkday;
wkday=days%7; // find the target week day number
System.out.println("\nOUTPUT :");
System.out.print("\nDay on "+date+"\t: "+daysOfWeek[wkday]);
} // end of main
} // end of class 

To print the date of a year in words given the day number and year

// To print the date of a year in words given the day number and year (ISC-1998)
import java.util.* ;
class DateQ1_1998 {
static int isLeap(int y){
if((y%4==0 && y%100!=0)|| y%400==0) return 1;
return 0;
}
public static void main(String args []) throws Exception {
Scanner sc=new Scanner(System.in);
String monthOfYear[]={"","January","February","March","April","May","June","July","August",
"September","October","November","December"};
String suffix,date;
int daysOfMonth[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int daynum,dd,mm,yy;
System.out.print("DAY = ");
daynum=sc.nextInt();
System.out.print("YEAR = ");
yy=sc.nextInt();
dd=daynum;
daysOfMonth[2]+=isLeap(yy);
for(mm=1;mm<=12 && dd>31;mm++)
dd-=daysOfMonth[mm]; // finds the day of the month
if(dd%10==1) suffix="st "; // for days that end with 1
else if(dd%10==2) suffix="nd "; // for days that end with 2
else if(dd%10==3) suffix="rd "; // for days that end with 3
else suffix="th "; // for other days
if(dd>10 && dd<14) suffix="th "; // for days 11, 12, and 13

date=dd+suffix+monthOfYear[mm]+" "+yy; // storing date in full form
System.out.println("\nOUTPUT :");
System.out.println(date);

} // end of main
} // end of class 

To find the completion date of a scheduled project given starting date and number of working days

// To find the completion date of a scheduled project given starting date and
// number of working days
import java.util.* ;
class DateQ1_1995 {
static int isLeap(int y){
if((y%4==0 && y%100!=0)|| y%400==0) return 1;
return 0;
}
public static void main(String args []) throws Exception {
Scanner sc=new Scanner(System.in);
int daysOfMonth[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i,dd,mm,yy,ww;
System.out.println("Project Start Date");
System.out.print("DAY:\t");
dd=sc.nextInt();
System.out.print("MONTH:\t");
mm=sc.nextInt();
System.out.print("YEAR:\t");
yy=sc.nextInt();
System.out.print("WORKING DAYS: ");
ww=sc.nextInt();
dd+=ww; // add working days to the day number
daysOfMonth[2]+=isLeap(yy); // check for days in February
while(dd>31){
dd-=daysOfMonth[mm]; // finds the day of the year
++mm; // update the month
if(mm>12){
yy++; // update year
mm=1; // start from the first month of new year
daysOfMonth[2]+=isLeap(yy);
}
}
System.out.println("\nOUTPUT :");
System.out.println("Project Completion Date");
System.out.println("DAY:\t"+dd);
System.out.println("MONTH:\t"+mm);
System.out.println("YEAR:\t"+yy);
} // end of main
} // end of class 

To accept date of birth and current date and find the age

// To accept date of birth and current date and find the age
import java.util.* ;
class DateQ1_2007 {
static int daysOfMonth[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
static void validateInput(int d,int m,int y){ // validates the input date
if(y<1 || y>9999) { // validates year
System.out.println("INVALID YEAR");
System.exit(0);

}
if(m<1 || m>12) { // validates month
System.out.println("INVALID MONTH");
System.exit(0);
}
if(d<1 || d>daysOfMonth[m]) { // validates day
System.out.println("INVALID DAY");
System.exit(0);

}
}
static int isLeap(int y){
if((y%4==0 && y%100!=0)|| y%400==0) return 1;
return 0;
}
public static void main(String args []) throws Exception {
Scanner sc=new Scanner(System.in);
String dob,currdate;
int n,d,dd1,dd2,m,mm1,mm2,y,yy1,yy2;
StringTokenizer st;
System.out.print("Date of birth: ");
dob="1/1/1971";//sc.next();
System.out.print("Current date\t: ");
currdate="20/1/1971";//sc.next();
dd1=dd2=mm1=mm2=yy1=yy2=0;

st=new StringTokenizer(dob,"/");
for(int i=3;st.hasMoreTokens();i--) { // separates day,month,and year
if(i>2) dd1=Integer.parseInt(st.nextToken()); // store day
else if(i>1)mm1=Integer.parseInt(st.nextToken()); // store month
else yy1=Integer.parseInt(st.nextToken()); // store year
}
st=new StringTokenizer(currdate,"/");
for(int i=3;st.hasMoreTokens();i--) { // separates day,month,and year
if(i>2) dd2=Integer.parseInt(st.nextToken()); // store day
else if(i>1)mm2=Integer.parseInt(st.nextToken()); // store month
else yy2=Integer.parseInt(st.nextToken()); // store year
}

validateInput(dd1,mm1,yy1); // validate input dob
validateInput(dd2,mm2,yy2); // validate input currdate
daysOfMonth[2]+=isLeap(yy1); // check for days in February

n=daysOfMonth[mm1];
d=n-dd1+(dd2-1);
m=0;
while (yy1!=yy2 || mm1!=mm2) { // finds total months elapsed before the target month
++mm1;
++m;
if(mm1>12){
mm1=1;
++yy1;
}
}
m+=d/n;
d%=n;
y=m/12; // converts months into years
if(y==0) --m; // age is less than 1 year
else m%=12; // age is more than 1 year
System.out.println("\nOUTPUT:");
System.out.println("\nAge is:");
System.out.println(y+(y==1?" year":" years"));
System.out.println(m+(m==1?" month":" months"));
System.out.println(d+(d==1?" day":" days"));
} // end of main
} // end of class