It will display appropriate time (years, months, dates, etc) when time is increased or decreased. For example, it will show leap year, months (28, 29, 30, or 31 days).
*/
package timeside;
import java.util.Scanner;
class MyTime
{
Scanner input = new Scanner(System.in);
int year, month, day, hour, minute, second;
private String Month;
int time[][] = new int[3][6];
MyTime(int i)
{
year = i;
month = i;
day = i;
hour = i;
minute= i;
second= i;
}
String convertMonth(int mont)
{
switch (mont)
{
case 1:
Month = "January";
break;
case 2:
Month = "February";
break;
case 3:
Month = "March";
break;
case 4:
Month = "April";
break;
case 5:
Month = "May";
break;
case 6:
Month = "June";
break;
case 7:
Month = "July";
break;
case 8:
Month = "August";
break;
case 9:
Month = "September";
break;
case 10:
Month = "October";
break;
case 11:
Month = "November";
break;
case 12:
Month = "December";
break;
default:
System.out.println("Invalid month !");
}
return Month;
}
boolean chkLeapYr(int year)
{
if (year%4 != 0) // common year
return false;
else if (year%100 != 0) // leap year
return true;
else if (year%400 != 0) // common year
return false;
else
return true; // leap year
}
void enterDate()
{
int i;
for (i=0; i<3; i++)
{
switch (i)
{
case 0:
System.out.print("Enter Year: ");
time[0][0] = input.nextInt();
if (time[0][0]<1 | time[0][0]>9999)
{
System.out.print("Invalid year. It should be between 1 and 9999. Reenter years: ");
time[0][0] = input.nextInt();
}
break;
case 1:
System.out.print("Enter Month: ");
time[0][1] = input.nextInt();
if (time[0][1]<1 | time[0][1]>12)
{
System.out.print("Invalid month. It should be between 1 and 12. Reenter months: ");
time[0][1] = input.nextInt();
}
break;
case 2:
System.out.print("Enter Day: ");
time[0][2] = input.nextInt();
switch (time[0][1]) // month
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (time[0][2]<1 | time[0][2]>31)
{
System.out.print("Invalid day. It should be between 1 and 31. Reenter days: ");
time[0][2] = input.nextInt();
}
break;
case 2:
if (chkLeapYr(time[0][0]) == true) // leap year
{
if (time[0][2]<1 | time[0][2]>29)
{
System.out.print("Invalid day. It should be between 1 and 29. Reenter days: ");
time[0][2] = input.nextInt();
}
}
if (chkLeapYr(time[0][0]) == false) // common year
{
if (time[0][2]<0 | time[0][2]>28)
{
System.out.print("Invalid day. It should be between 1 and 28. Reenter days: ");
time[0][2] = input.nextInt();
}
}
break;
case 4:
case 6:
case 9:
case 11:
if (time[0][2]<1 | time[0][2]>30)
{
System.out.print("Invalid day. It should be between 1 and 30. Reenter days: ");
time[0][2] = input.nextInt();
}
break;
}
break;
default:
System.out.println("Invalid date");
}
}
for (i=0; i<3; i++) // copy time[0][0,1,2] to time[1][0,1,2] and time[2][0,1,2]
{
time[1][i] = time[0][i];
time[2][i] = time[0][i];
}
}
void enterTime()
{
int i, temp;
for (i=0; i<3; i++)
{
switch (i)
{
case 0:
System.out.print("Enter Hours: ");
time[0][3] = input.nextInt();
if (time[0][3]<0 | time[0][3]>23)
{
System.out.print("Invalid hours. It should be between 0 and 23. Reenter hours: ");
time[0][3] = input.nextInt();
}
break;
case 1:
System.out.print("Enter Minutes: ");
time[0][4] = input.nextInt();
if (time[0][4]<0 | time[0][4]>59)
{
System.out.print("Invalid minutes. It should be between 0 and 59. Reenter minutes: ");
time[0][4] = input.nextInt();
}
break;
case 2:
System.out.print("Enter Seconds: ");
time[0][5] = input.nextInt();
if (time[0][5]<0 | time[0][5]>59)
{
System.out.print("Invalid seconds. It should be between 0 and 59. Reenter seconds: ");
time[0][5] = input.nextInt();
}
break;
default:
System.out.println("Invalid time");
}
}
for (i=3; i<6; i++) // copy time[0][3,4,5] to time[1][3,4,5] and time[2][3,4,5]
{
time[1][i] = time[0][i];
time[2][i] = time[0][i];
}
}
void displayStdTime(int month, int day, int year, int hour, int minute, int second)
{
int hr=0;
String mon;
//System.out.println(month+"/"+day+"/"+year+" "+hour+":"+minute+":"+second);
//System.out.print(" Standard form: ");
mon = convertMonth(month);
switch (hour)
{
case 12:
hr = hour;
break;
case 13:
hr = hour-12;
break;
case 14:
hr = hour-12;
break;
case 15:
hr = hour-12;
break;
case 16:
hr = hour-12;
break;
case 17:
hr = hour-12;
break;
case 18:
hr = hour-12;
break;
case 19:
hr = hour-12;
break;
case 20:
hr = hour-12;
break;
case 21:
hr = hour-12;
break;
case 22:
hr = hour-12;
break;
case 23:
hr = hour-12;
break;
}
System.out.print(mon+" ");
if (day<=9)
System.out.print("0"+day+",");
else System.out.print(day+",");
if (year>999 & year<=9999)
System.out.print(" "+year);
else if (year>99 & year<=999)
System.out.print(" "+"0"+year);
else if (year>9 & year<=99)
System.out.print(" "+"00"+year);
else if (year>=1 & year<=9)
System.out.print(" "+"000"+year);
else System.out.print(" "+year);
if (hour<12)
{
if (hour<=9)
{
if (hour==0)
System.out.print(" "+"12");
else System.out.print(" "+"0"+hour);
}
else System.out.print(" "+hour);
}
else
{
hr = hour-12;
if (hr<=9)
{
if (hour==0)
System.out.print(" "+"12");
else System.out.print(" "+"0"+hr);
}
else System.out.print(" "+hr);
}
if (minute<=9)
System.out.print(":"+"0"+minute);
else System.out.print(":"+minute);
if (second<=9)
System.out.print(":"+"0"+second);
else System.out.print(":"+second);
if (hour<12)
System.out.print(" AM");
else System.out.print(" PM");
System.out.println("");
}
void displayMilTime(int month, int day, int year, int hour, int minute, int second)
{
if (month<=9)
System.out.print("0"+month);
else System.out.print(month);
if (day<=9)
System.out.print("/"+"0"+day);
else System.out.print("/"+day);
if (year>999 & year<=9999)
System.out.print("/"+year);
else if (year>99 & year<=999)
System.out.print("/"+"0"+year);
else if (year>9 & year<=99)
System.out.print("/"+"00"+year);
else if (year>=1 & year<=9)
System.out.print("/"+"000"+year);
else System.out.print("/"+year);
if (hour<=9)
System.out.print(" "+"0"+hour);
else System.out.print(" "+hour);
if (minute<=9)
System.out.print(":"+"0"+minute);
else System.out.print(":"+minute);
if (second<=9)
System.out.print(":"+"0"+second);
else System.out.print(":"+second);
System.out.println("");
}
////////////////////////////////////////////////////////////////////////////////
void increaseTime(String str)
{
int temp;
switch (str)
{
///
case "1s": // seconds
//System.out.print(" Increment 1 second.");
temp = 1; // By default 1s
time[1][5] = time[1][5]+temp;
if (time[1][5]>59) // number of seconds > 59
{
time[1][5] = time[1][5]-60;
time[1][4] = ++time[1][4]; // increment number of minutes by 1 mn
}
if (time[1][4]>59)
{
time[1][4] = time[1][4]-60;
time[1][3] = ++time[1][3]; // increment number of hrs by 1 hr
}
if (time[1][3]>23)
{
time[1][3] = time[1][3]-24;
switch (time[1][1])
{
case 1: //31 days
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
time[1][2] = ++time[1][2];
if (time[1][2]>31)
{
time[1][2] = time[1][2]-31;
time[1][1] = ++time[1][1];
}
break;
case 4: //30 days
case 6:
case 9:
case 11:
time[1][2] = ++time[1][2];
if (time[1][2]>30)
{
time[1][2] = time[1][2]-30;
time[1][1] = ++time[1][1];
}
break;
case 2: // 28 or 29
time[1][2] = ++time[1][2];
if (chkLeapYr(time[1][0])) // leap year
{
if (time[1][2]>29)
{
time[1][2] = time[1][2]-29; // days
time[1][1] = ++time[1][1]; // month+1
}
}
else // common year
if (time[1][2]>28)
{
time[1][2] = time[1][2]-28; // days
time[1][1] = ++time[1][1]; // month+1
}
break;
}
}
switch(time[1][1]) // month
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (time[1][2]>31)
{
time[1][2] = time[1][2]-31; // days
time[1][1] = ++time[1][1]; // month+1
}
break;
case 4:
case 6:
case 9:
case 11:
if (time[1][2]>30)
{
time[1][2] = time[1][2]-30; // days
time[1][1] = ++time[1][1]; // month+1
}
break;
case 2:
if (chkLeapYr(time[1][0])) // leap year
{
if (time[1][2]>29)
{
time[1][2] = time[1][2]-29; // days
time[1][1] = ++time[1][1]; // month+1
}
}
else // common year
if (time[1][2]>28)
{
time[1][2] = time[1][2]-28; // days
time[1][1] = ++time[1][1]; // month+1
}
break;
}
if (time[1][1]>12) // months
{
time[1][1] = time[1][1]-12; // months
time[1][0] = ++time[1][0];
}
if (time[1][0]>9999)
System.out.println("Invalid year !");
break;
///
case "s": // seconds
System.out.print(" Increase seconds by ");
temp = input.nextInt();
if (temp<0)
{
System.out.print("Invalid seconds. It should be more than 0. Reenter seconds: ");
temp = input.nextInt();
time[1][5] = time[1][5]+temp;
} else time[1][5] = time[1][5]+temp;
if (time[1][5]>59) // number of seconds > 59
{
time[1][4] = time[1][4]+time[1][5]/60; // increment number of minutes by 1 mn
time[1][5] = time[1][5]%60; // Remainder = seconds
}
if (time[1][4]>59)
{
time[1][3] = time[1][3]+time[1][4]/60; // increase hours
time[1][4] = time[1][4]%60;
}
if (time[1][3]>23)
{
time[1][2] = time[1][2]+time[1][3]/24; // increase days
time[1][3] = time[1][3]%24;
}
switch(time[1][1]) // month
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (time[1][2]>31)
{
time[1][1] = time[1][1]+time[1][2]/31; // icrease months
time[1][2] = time[1][2]%31; // days
}
break;
case 4:
case 6:
case 9:
case 11:
if (time[1][2]>30)
{
time[1][1] = time[1][1]+time[1][2]/30; // increase months
time[1][2] = time[1][2]%30; // days
}
break;
case 2:
if (chkLeapYr(time[1][0])) // leap year
{
if (time[1][2]>29)
{
time[1][1] = time[1][1]+time[1][2]/29; // increase months
time[1][2] = time[1][2]%29; // days
}
}
else // common year
if (time[1][2]>28)
{
time[1][1] = time[1][1]+time[1][2]/28; // increase months
time[1][2] = time[1][2]%28; // days
}
break;
}
if (time[1][1]>12) // months
{
time[1][0] = time[1][0]+time[1][1]/12;
time[1][1] = time[1][1]%12; // months
}
if (time[1][0]>9999)
System.out.println("Invalid year !");
break;
////////
case "m": // minutes
System.out.print(" Increase minutes by ");
temp = input.nextInt();
if (temp<0)
{
System.out.print("Invalid minutes. It should be more than 0. Reenter minutes: ");
temp = input.nextInt();
time[1][4] = time[1][4]+temp;
} else time[1][4] = time[1][4]+temp;
if (time[1][4]>59)
{
time[1][3] = time[1][3]+time[1][4]/60; // increase hours
time[1][4] = time[1][4]%60;
}
if (time[1][3]>23)
{
time[1][2] = time[1][2]+time[1][3]/24; // increase days
time[1][3] = time[1][3]%24;
}
switch(time[1][1]) // month
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (time[1][2]>31)
{
time[1][1] = time[1][1]+time[1][2]/31; // increase months
time[1][2] = time[1][2]%31; // days
}
break;
case 4:
case 6:
case 9:
case 11:
if (time[1][2]>30)
{
time[1][1] = time[1][1]+time[1][2]/30; // icrease months
time[1][2] = time[1][2]%30; // days
}
break;
case 2:
if (chkLeapYr(time[1][0])) // leap year
{
if (time[1][2]>29)
{
time[1][1] = time[1][1]+time[1][2]/29; // increase months
time[1][2] = time[1][2]%29; // days
}
}
else // common year
if (time[1][2]>28)
{
time[1][1] = time[1][1]+time[1][2]/28; // increase months
time[1][2] = time[1][2]%28; // days
}
break;
}
if (time[1][1]>12) // months
{
time[1][0] = time[1][0]+time[1][1]/12;
time[1][1] = time[1][1]%12; // days
}
if (time[1][0]>9999)
System.out.println("Invalid year !");
break;
///
case "h": // hours
System.out.print(" Increase hours by ");
temp = input.nextInt();
if (temp<0)
{
System.out.print("Invalid hours. It should be more than 0. Reenter hours: ");
temp = input.nextInt();
time[1][3] = time[1][3]+temp;
} else time[1][3] = time[1][3]+temp;
if (time[1][3]>23)
{
time[1][2] = time[1][2]+time[1][3]/24; // increase days
time[1][3] = time[1][3]%24;
}
switch(time[1][1]) // month
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (time[1][2]>31)
{
time[1][1] = time[1][1]+time[1][2]/31; // increase months
time[1][2] = time[1][2]%31; // days
}
break;
case 4:
case 6:
case 9:
case 11:
if (time[1][2]>30)
{
time[1][1] = time[1][1]+time[1][2]/30; // increase months
time[1][2] = time[1][2]%30; // days
}
break;
case 2:
if (chkLeapYr(time[1][0])) // leap year
{
if (time[1][2]>29)
{
time[1][1] = time[1][1]+time[1][2]/29; // increase months
time[1][2] = time[1][2]%29; // days
}
}
else // common year
if (time[1][2]>28)
{
time[1][1] = time[1][1]+time[1][2]/28; // increase months
time[1][2] = time[1][2]%28; // days
}
break;
}
if (time[1][1]>12) // months
{
time[1][0] = time[1][0]+time[1][1]/12;
time[1][1] = time[1][1]%12; // months
}
if (time[1][0]>9999)
System.out.println("Invalid year !");
break;
///
case "D":
System.out.print(" Increase days by ");
temp = input.nextInt();
if (temp<0)
{
System.out.print("Invalid days. It should be more than 0. Reenter days: ");
temp = input.nextInt();
time[1][2] = time[1][2]+temp;
} else time[1][2] = time[1][2]+temp;
switch(time[1][1]) // month
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (time[1][2]>31)
{
time[1][1] = time[1][1]+time[1][2]/31; // increase months
time[1][2] = time[1][2]%31; // days
}
break;
case 4:
case 6:
case 9:
case 11:
if (time[1][2]>30)
{
time[1][1] = time[1][1]+time[1][2]/31; // increase months
time[1][2] = time[1][2]%30; // days
}
break;
case 2:
if (chkLeapYr(time[1][0])) // leap year
{
if (time[1][2]>29)
{
time[1][1] = time[1][1]+time[1][2]/29; // increase months
time[1][2] = time[1][2]%29; // days
}
}
else // common year
if (time[1][2]>28)
{
time[1][1] = time[1][1]+time[1][2]/28; // increase months
time[1][2] = time[1][2]%28; // days
}
break;
}
if (time[1][1]>12) // months
{
time[1][0] = time[1][0]+time[1][1]/12;
time[1][1] = time[1][1]%12; // months
}
if (time[1][0]>9999)
System.out.println("Invalid year !");
break;
///
case "M": // Months
System.out.print(" Increase months by ");
temp = input.nextInt();
if (temp<0)
{
System.out.print("Invalid month. It should be more than 0. Reenter months: ");
temp = input.nextInt();
time[1][1] = time[1][1]+temp;
} else time[1][1] = time[1][1]+temp;
if (time[1][1]>12) // months
{
time[1][0] = time[1][0]+time[1][1]/12;
time[1][1] = time[1][1]%12; // days
}
if (time[1][0]>9999)
System.out.println("Invalid year !");
break;
////////
case "Y": // Years
System.out.print(" Increase years by ");
temp = input.nextInt();
if (temp<1 | temp>9999)
{
System.out.println("Invalid year. It should be between 1 and 9999. Reenter years: ");
temp = input.nextInt();
time[1][0] = time[1][0]+temp;
} else time[1][0] = time[1][0]+temp;
if (time[1][0]>9999)
System.out.println("Invalid year !");
break;
default: System.out.println("Invalid date or time!");
}
}
////////////////////////////////////////////////////////////////////////////////
void decreaseTime(String str)
{
int temp;
switch (str)
{
case "1s": // seconds
//System.out.print(" Decrement 1 second.");
temp = 1; // By default 1s
time[2][5] = time[2][5]-temp; // temp>0
if (time[2][5]<0)
{
time[2][5] = 60+time[2][5];
time[2][4] = --time[2][4]; // decrement minutes by 1 mn
}
if (time[2][4]<0)
{
time[2][4] = 60+time[2][4];
time[2][3] = --time[2][3]; // decrement hrs by 1 hr
}
if (time[2][3]<0)
{
time[2][3] = 24+time[2][3];
time[2][2] = --time[2][2]; // decrement days by 1 day
}
switch(time[2][1]) // month
{
case 1: // 31 days per month
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (time[2][2]==0)
{
switch(time[2][1])
{
case 1:
case 8:
time[2][2] = 31+time[2][2]; // days
if (time[2][1]==1)
{
time[2][1]=12;
time[2][0]=--time[2][0];
}
else time[2][1] = --time[2][1]; // months-1
break;
case 3:
if (chkLeapYr(time[2][0])) // leap year
{
time[2][2] = 29+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
else // not leap year = common year
{
time[2][2] = 28+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
break;
case 5:
case 7:
case 10:
case 12:
time[2][2] = 30+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
break;
}
}
if (time[2][2]<0)
{
time[2][2] = 31+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
break;
case 4:
case 6:
case 9:
case 11:
if (time[2][2]==0)
{
time[2][2] = 31+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
if (time[2][2]<0)
{
time[2][2] = 30+time[2][2]; // days
time[2][1] = --time[2][1]; // month+1
}
break;
case 2:
if (chkLeapYr(time[2][0])) // leap year
{
if (time[2][2]==0)
{
time[2][2] = 31+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
if (time[2][2]<0)
{
time[2][2] = 29+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
}
else // common year
{
if (time[2][2]==0)
{
time[2][2] = 31+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
if (time[2][2]<0)
{
time[2][2] = 28+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
}
break;
}
if (time[2][1]<0) // months
{
time[2][1] = 12+time[2][1]; // months
time[2][0] = --time[2][0]; // years-1
}
break;
case "s": // seconds
System.out.print(" Decrease seconds by ");
temp = input.nextInt();
if (temp<0)
{
System.out.print("Invalid seconds. It should be more than 0. Reenter seconds: ");
temp = input.nextInt();
time[2][5] = time[2][5]-temp;
} else time[2][5] = time[2][5]-temp; // temp>0
if (time[2][5]<0)
{
time[2][4] = time[2][4]-(Math.abs(time[2][5])/60+1); // decrease mn
time[2][5] = time[2][5]+(Math.abs(time[2][5])/60+1)*60;
}
if (time[2][4]<0)
{
time[2][3] = time[2][3]-(Math.abs(time[2][4])/60+1); // decrease hr
time[2][4] = time[2][4]+(Math.abs(time[2][4])/60+1)*60;
}
if (time[2][3]<0)
{
time[2][2] = time[2][2]-(Math.abs(time[2][3])/24+1); // decrease days
time[2][3] = time[2][3]+(Math.abs(time[2][3])/24+1)*24;
}
switch(time[2][1]) // month
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (time[2][2]==0)
{
switch(time[2][1])
{
case 1:
case 8:
time[2][2] = 31+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
break;
case 3:
if (chkLeapYr(time[2][0])) // leap year
{
time[2][2] = 29+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
else // not leap year = common year
{
time[2][2] = 28+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
break;
case 5:
case 7:
case 10:
case 12:
time[2][2] = 30+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
break;
}
}
if (time[2][2]<0)
{
time[2][1] = time[2][1]-(Math.abs(time[2][2])/31+1); // months
time[2][2] = time[2][2]+(Math.abs(time[2][2])/31+1)*31; // days
}
break;
case 4:
case 6:
case 9:
case 11:
if (time[2][2]==0)
{
time[2][2] = 31+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
if (time[2][2]<0)
{
time[2][1] = time[2][1]-(Math.abs(time[2][2])/30+1); // months
time[2][2] = time[2][2]+(Math.abs(time[2][2])/30+1)*30; // days
}
break;
case 2:
if (chkLeapYr(time[2][0])) // leap year
{
if (time[2][2]==0)
{
time[2][2] = 31+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
if (time[2][2]<0)
{
time[2][1] = time[2][1]-(Math.abs(time[2][2])/29+1); // months
time[2][2] = time[2][2]+(Math.abs(time[2][2])/29+1)*29; // days
}
}
else // common year
{
if (time[2][2]==0)
{
time[2][2] = 31+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
if (time[2][2]<0)
{
time[2][1] = time[2][1]-(Math.abs(time[2][2])/28+1); // months
time[2][2] = time[2][2]+(Math.abs(time[2][2])/28+1)*28; // days
}
}
break;
}
if (time[2][1]<0) // months
{
time[2][0] = time[2][0]-(Math.abs(time[2][1])/12+1);
time[2][1] = time[2][1]+(Math.abs(time[2][1])/12+1)*12;
}
break;
case "m": // minutes
System.out.print(" Decrease minutes by ");
temp = input.nextInt();
if (temp<0)
{
System.out.print("Invalid minutes. It should be more than 0. Reenter minutes: ");
temp = input.nextInt();
time[2][4] = time[2][4]-temp;
} else time[2][4] = time[2][4]-temp;
if (time[2][4]<0)
{
time[2][3] = time[2][3]-(Math.abs(time[2][4])/60+1); // decrease hrs
time[2][4] = time[2][4]+(Math.abs(time[2][4])/60+1)*60;
}
if (time[2][3]<0)
{
time[2][2] = time[2][2]-(Math.abs(time[2][3])/24+1); // decrease days
time[2][3] = time[2][3]+(Math.abs(time[2][3])/24+1)*24;
}
switch(time[2][1]) // month
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (time[2][2]==0)
{
switch(time[2][1])
{
case 1:
case 8:
time[2][2] = 31+time[2][2]; // days
if (time[2][1]==1)
{
time[2][1]=12;
time[2][0]=--time[2][0];
}
else time[2][1] = --time[2][1]; // months-1
break;
case 3:
if (chkLeapYr(time[2][0])) // leap year
{
time[2][2] = 29+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
else // not leap year = common year
{
time[2][2] = 28+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
break;
case 5:
case 7:
case 10:
case 12:
time[2][2] = 30+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
break;
}
}
if (time[2][2]<0)
{
time[2][1] = time[2][1]-(Math.abs(time[2][2])/31+1); // months
time[2][2] = time[2][2]+(Math.abs(time[2][2])/31+1)*31; // days
}
break;
case 4:
case 6:
case 9:
case 11:
if (time[2][2]==0)
{
time[2][2] = 31+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
if (time[2][2]<0)
{
time[2][1] = time[2][1]-(Math.abs(time[2][2])/30+1); // months
time[2][2] = time[2][2]+(Math.abs(time[2][2])/30+1)*30; // days
}
break;
case 2:
if (chkLeapYr(time[2][0])) // leap year
{
if (time[2][2]==0)
{
time[2][2] = 31+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
if (time[2][2]<0)
{
time[2][1] = time[2][1]-(Math.abs(time[2][2])/29+1); // months
time[2][2] = time[2][2]+(Math.abs(time[2][2])/29+1)*29; // days
}
}
else // common year
{
if (time[2][2]==0)
{
time[2][2] = 31+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
if (time[2][2]<0)
{
time[2][1] = time[2][1]-(Math.abs(time[2][2])/28+1); // months
time[2][2] = time[2][2]+(Math.abs(time[2][2])/28+1)*28; // days
}
}
break;
}
if (time[2][1]<0) // months
{
time[2][0] = time[2][0]-(Math.abs(time[2][1])/12+1);
time[2][1] = time[2][1]+(Math.abs(time[2][1])/12+1)*12;
}
break;
case "h": // hours
System.out.print(" Decrease hours by ");
temp = input.nextInt();
if (temp<0)
{
System.out.print("Invalid hours. It should be more than 0. Reenter hours: ");
temp = input.nextInt();
time[2][3] = time[2][3]-temp;
} else time[2][3] = time[2][3]-temp;
if (time[2][3]<0)
{
time[2][2] = time[2][2]-(Math.abs(time[2][3])/24+1); // decrease days
time[2][3] = time[2][3]+(Math.abs(time[2][3])/24+1)*24;
}
switch(time[2][1]) // month
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (time[2][2]==0)
{
switch(time[2][1])
{
case 1:
case 8:
time[2][2] = 31+time[2][2]; // days
if (time[2][1]==1)
{
time[2][1]=12;
time[2][0]=--time[2][0];
}
else time[2][1] = --time[2][1]; // months-1
break;
case 3:
if (chkLeapYr(time[2][0])) // leap year
{
time[2][2] = 29+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
else // not leap year = common year
{
time[2][2] = 28+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
break;
case 5:
case 7:
case 10:
case 12:
time[2][2] = 30+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
break;
}
}
if (time[2][2]<0)
{
time[2][1] = time[2][1]-(Math.abs(time[2][2])/31+1); // months
time[2][2] = time[2][2]+(Math.abs(time[2][2])/31+1)*31; // days
}
break;
case 4:
case 6:
case 9:
case 11:
if (time[2][2]==0)
{
time[2][2] = 31+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
if (time[2][2]<0)
{
time[2][1] = time[2][1]-(Math.abs(time[2][2])/30+1); // months
time[2][2] = time[2][2]+(Math.abs(time[2][2])/30+1)*30; // days
}
break;
case 2:
if (chkLeapYr(time[2][0])) // leap year
{
if (time[2][2]==0)
{
time[2][2] = 31+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
if (time[2][2]<0)
{
time[2][1] = time[2][1]-(Math.abs(time[2][2])/29+1); // months
time[2][2] = time[2][2]+(Math.abs(time[2][2])/29+1)*29; // days
}
}
else // common year
{
if (time[2][2]==0)
{
time[2][2] = 31+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
if (time[2][2]<0)
{
time[2][1] = time[2][1]-(Math.abs(time[2][2])/28+1); // months
time[2][2] = time[2][2]+(Math.abs(time[2][2])/28+1)*28; // days
}
}
break;
}
if (time[2][1]<0) // months
{
time[2][0] = time[2][0]-(Math.abs(time[2][1])/12+1);
time[2][1] = time[2][1]+(Math.abs(time[2][1])/12+1)*12;
}
break;
case "D":
System.out.print(" Decrease days by ");
temp = input.nextInt();
if (temp<0)
{
System.out.print("Invalid days. It should be more than 0. Reenter hours: ");
temp = input.nextInt();
time[2][2] = time[2][2]-temp;
} else time[2][2] = time[2][2]-temp;
switch(time[2][1]) // month
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (time[2][2]==0)
{
switch(time[2][1])
{
case 1:
case 8:
time[2][2] = 31+time[2][2]; // days
if (time[2][1]==1)
{
time[2][1]=12;
time[2][0]=--time[2][0];
}
else time[2][1] = --time[2][1]; // months-1
break;
case 3:
if (chkLeapYr(time[2][0])) // leap year
{
time[2][2] = 29+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
else // not leap year = common year
{
time[2][2] = 28+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
break;
case 5:
case 7:
case 10:
case 12:
time[2][2] = 30+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
break;
}
}
if (time[2][2]<0)
{
time[2][1] = time[2][1]-(Math.abs(time[2][2])/31+1); // months
time[2][2] = time[2][2]+(Math.abs(time[2][2])/31+1)*31; // days
}
break;
case 4:
case 6:
case 9:
case 11:
if (time[2][2]==0)
{
time[2][2] = 31+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
if (time[2][2]<0)
{
time[2][1] = time[2][1]-(Math.abs(time[2][2])/30+1); // months
time[2][2] = time[2][2]+(Math.abs(time[2][2])/30+1)*30; // days
}
break;
case 2:
if (chkLeapYr(time[2][0])) // leap year
{
if (time[2][2]==0)
{
time[2][2] = 31+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
if (time[2][2]<0)
{
time[2][1] = time[2][1]-(Math.abs(time[2][2])/29+1); // months
time[2][2] = time[2][2]+(Math.abs(time[2][2])/29+1)*29; // days
}
}
else // common year
{
if (time[2][2]==0)
{
time[2][2] = 31+time[2][2]; // days
time[2][1] = --time[2][1]; // months-1
}
if (time[2][2]<0)
{
time[2][1] = time[2][1]-(Math.abs(time[2][2])/28+1); // months
time[2][2] = time[2][2]+(Math.abs(time[2][2])/28+1)*28; // days
}
}
break;
}
if (time[2][1]<0) // months
{
time[2][0] = time[2][0]-(Math.abs(time[2][1])/12+1);
time[2][1] = time[2][1]+(Math.abs(time[2][1])/12+1)*12;
}
break;
case "M": // Months
System.out.print(" Decrease months by ");
temp = input.nextInt();
if (temp<0)
{
System.out.print("Invalid month. It should be between 1 and 12. Reenter months: ");
temp = input.nextInt();
time[2][1] = time[2][1]-temp;
} else time[2][1] = time[2][1]-temp;
if (time[2][1]<0) // months
{
time[2][0] = time[2][0]-(Math.abs(time[2][1])/12+1);
time[2][1] = time[2][1]+(Math.abs(time[2][1])/12+1)*12;
}
break;
case "Y": // Years
System.out.print(" Decrease years by ");
temp = input.nextInt();
if (temp<0 | temp>9999)
{
System.out.println("Invalid year. It should be between 1 and 9999. Reenter years: ");
temp = input.nextInt();
time[2][0] = time[2][0]-temp;
} else time[2][0] = time[2][0]-temp;
break;
default: System.out.println("Invalid date or time!");
}
}
}
public class Timeside
{
public static void main(String[] args)
{
String str = new String("");
String yes="y", no="n"; // yes
String str0="Y", str1="M", str2="D", str3="h", str4="m", str5="s";
MyTime dat = new MyTime(0);
dat.enterDate();
dat.enterTime();
System.out.println();
System.out.print("Input Date & Time: ");
dat.displayMilTime(dat.time[0][1],dat.time[0][2],dat.time[0][0],dat.time[0][3],dat.time[0][4],dat.time[0][5]);
System.out.println();
System.out.print("Reference Date & Time: ");
dat.displayStdTime(dat.time[0][1],dat.time[0][2],dat.time[0][0],dat.time[0][3],dat.time[0][4],dat.time[0][5]);
System.out.println();
System.out.println("By default: Increment 1 second");
dat.increaseTime("1s");
System.out.print("Increment Date & Time: ");
dat.displayMilTime(dat.time[1][1],dat.time[1][2],dat.time[1][0],dat.time[1][3],dat.time[1][4],dat.time[1][5]);
System.out.println();
System.out.println("By default: Decrement 1 second");
dat.decreaseTime("1s");
System.out.print("Decement Date & Time: ");
dat.displayMilTime(dat.time[2][1],dat.time[2][2],dat.time[2][0],dat.time[2][3],dat.time[2][4],dat.time[2][5]);
System.out.println();
System.out.println("Info: You can INCREASE or DECREASE Date and/or Time.");
System.out.println();
System.out.print("Do you want to INCREASE date and time? 'y' for Yes or 'n' for No: ");
str = dat.input.next();
if (str.equals(yes))
{
//System.out.println("Please select either 'Y' for years, 'M' for months, or 'D' for days");
//System.out.println(" and either 'h' for hours, 'm' for minutes, or 's' for seconds");
//System.out.print(" Enter the selection for years: ");
//str = "Y";
//if (str.equals(str0))
dat.increaseTime("Y");
//System.out.print(" Enter the selection for months: ");
//str = dat.input.next();
//if (str.equals(str1))
dat.increaseTime("M");
//System.out.print(" Enter the selection for days: ");
//str = dat.input.next();
//if (str.equals(str2))
dat.increaseTime("D");
//System.out.print(" Enter the selection for hours: ");
//str = dat.input.next();
//if (str.equals(str3))
dat.increaseTime("h");
//System.out.print(" Enter the selection for minutes: ");
//str = dat.input.next();
//if (str.equals(str4))
dat.increaseTime("m");
//System.out.print(" Enter the selection for seconds: ");
//str = dat.input.next();
//if (str.equals(str5))
dat.increaseTime("s");
System.out.print("Increase Date & Time: ");
dat.displayMilTime(dat.time[1][1],dat.time[1][2],dat.time[1][0],dat.time[1][3],dat.time[1][4],dat.time[1][5]);
System.out.println();
}
else if (str.equals(no))
{
System.out.println(" Skip increasing date & time.");
System.out.println();
}
else
{
System.out.println(" Incorrect response of 'y' or 'n'!");
System.out.println();
}
System.out.print("Do you want to DECREASE date and time? 'y' for Yes or 'n' for No: ");
str = dat.input.next();
if (str.equals(yes))
{
//System.out.println("Please select either 'Y' for years, 'M' for months, or 'D' for days");
//System.out.println(" and either 'h' for hours, 'm' for minutes, or 's' for seconds");
//System.out.print(" Enter the selection for years: ");
//str = dat.input.next();
//if (str.equals(str0))
dat.decreaseTime("Y");
//System.out.print(" Enter the selection for months: ");
//str = dat.input.next();
//if (str.equals(str1))
dat.decreaseTime("M");
//System.out.print(" Enter the selection for days: ");
//str = dat.input.next();
//if (str.equals(str2))
dat.decreaseTime("D");
//System.out.print(" Enter the selection for hours: ");
//str = dat.input.next();
//if (str.equals(str3))
dat.decreaseTime("h");
//System.out.print(" Enter the selection for minutes: ");
//str = dat.input.next();
//if (str.equals(str4))
dat.decreaseTime("m");
//System.out.print(" Enter the selection for seconds: ");
//str = dat.input.next();
//if (str.equals(str5))
dat.decreaseTime("s");
System.out.print("Decrease Date & Time: ");
dat.displayMilTime(dat.time[2][1],dat.time[2][2],dat.time[2][0],dat.time[2][3],dat.time[2][4],dat.time[2][5]);
System.out.println();
}
else if (str.equals(no))
{
System.out.println(" Skip decreasing date & time.");
System.out.println();
}
else
{
System.out.println(" Incorrect response of 'y' or 'n'!");
System.out.println();
}
System.out.println("End of the progsm.");
}
}
No comments:
Post a Comment