This program tells me which time a start date and an end, now I ask you whether you want to enter another period when you s
(yes) adds the starting period, but this want to join with above, for example, I add the following:
16/08/1993 - 09/09/2014 = 21 years, 0 months, 24 days.
14/02/1995 - 18/05/2001 = 6 years, 3 months, 35 days
Total:.................................27 years, 3 months y 59 days
NOTE: I do not know if something is wrong and try everything. I am new to java.
public void diferencia(){
char seguir=0;
Scanner teclado =new Scanner(System.in);
do{
System.out.println("day/Month/year ");
System.out.println("Write the first date: ");
String dayini = teclado.next();
System.out.println("write the second date: ");
String dateactual = teclado.next();
String[] aFechaIng = dateini.split("/");
Integer dayini = Integer.parseInt(aFechaIng[0]);
Integer monthini = Integer.parseInt(aFechaIng[1]);
Integer yearini = Integer.parseInt(aFechaIng[2]);
String[] aFecha = dateactual.split("/");
Integer dayactual= Integer.parseInt(aFecha[0]);
Integer monthactual = Integer.parseInt(aFecha[1]);
Integer yearactual = Integer.parseInt(aFecha[2]);
int b = 0;
int days = 0; //DAYS
int month = 0; //MONTH
int years = 0; //YEARS
int months = 0; //MONTHS
month = monthini - 1;
// LEAP YEAR
if(month==2){
if ((yearactual % 4 == 0) && ((yearactual % 100 != 0) || (yearactual % 400 == 0))){
b = 29;
}else{
b = 28;
}
}else if(month <= 7){
if(month == 0){
b = 31;
}else if(month % 2==0){
b = 30;
}else{
b = 31;
}
}else if(month > 7){
if(month % 2 == 0){
b = 31;
}else{
b = 30;
}
}
if((yearini > yearactual) || (yearini == yearactual && monthini > monthactual) ||
(yearini == yearactual && monthini == monthactual && dayini > dayactual)){
// errors
System.out.println("The start date must be minor ");
}else{ //time periods
if(monthini <= monthactual){
years = yearactual - yearini;
if (dayini <= dayactual){
mmonths = monthactual - monthini;
days = b - (dayini - dayactual);
}else{
if(monthactual == monthini){
years = years - 1;
}
months = (monthactual - monthini - 1 + 12) % 12;
days = b - (dayini - dayactual);
}
}else{
years = yearactual - yearini - 1;
//System.out.println("Años?¿: " + anios);
if(dayini > dayactual){
months = monthactual - monthsini - 1 + 12;
days = b - (dayini - dayactual);
}else{
months = monthactual - monthini + 12;
days = dayactual - dayini;
}
}
}
System.out.println("Years: "+years);
System.out.println("Months: "+months);
System.out.println("Days: "+days);
System.out.println("You want to add another period?? ");
seguir = teclado.next().charAt(0);
}while(seguir!='n');
}//finish method diferencia