Buenas!
Estoy haciendo un programa, de hecho, ya prácticamente está finalizado, para aprender un poco sobre la gestión de fechas. A continuación dejo el código:
Código:
package practica1;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Date;
public class fechas {
public static void main(String[] args) {
final Calendar c1 = new GregorianCalendar(Integer.parseInt(args[0]),Integer.parseInt(args[1]),Integer.parseInt(args[2]));
final Calendar c2 = new GregorianCalendar(Integer.parseInt(args[3]),Integer.parseInt(args[4]),Integer.parseInt(args[5]));
validarFechas(args);
boolean c1esPosteriorOIgual = ordenaFechas(c1,c2);
restaFechas(c1,c2,c1esPosteriorOIgual);
getSemanasCompletas(c1,c2,c1esPosteriorOIgual);
getDiasHabiles(c1,c2,c1esPosteriorOIgual);
}
public static boolean ordenaFechas(Calendar f1, Calendar f2){
Date date1=f1.getTime();
Date date2=f2.getTime();
if(date2.before(date1) || date2.equals(date1)){
return true;
} else
return false;
}
public static void restaFechas(Calendar f1, Calendar f2, boolean c1Posterior){
Date date1=f1.getTime();
Date date2=f2.getTime();
long difEnMilis;
if (c1Posterior == true){
difEnMilis = date1.getTime() - date2.getTime();
}
else{
difEnMilis = date2.getTime() - date1.getTime();
}
GregorianCalendar cal = new GregorianCalendar();
cal.setTimeInMillis(difEnMilis);
System.out.print("El numero de anhos, meses y dias transcurridos entre una fecha y otra es: ");
System.out.print(cal.get(GregorianCalendar.YEAR)-1970 + " anhos, ");
System.out.print(cal.get(GregorianCalendar.MONTH) + " meses, y ");
System.out.print(cal.get(GregorianCalendar.DAY_OF_MONTH)-1 + " dias.\n");
}
public static void getDiasHabiles(Calendar f1, Calendar f2, boolean c1Posterior){
int dias = 0;
Calendar start;
Calendar end;
if (c1Posterior == true){
start = f2;
end = f1;
} else{
start = f1;
end = f2;
}
while(start.before(end) || start.equals(end)){
if(start.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY &&
start.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY){
dias++;
}
start.add(Calendar.DATE,1);
}
System.out.println("El numero de dias habiles entre ambas fechas es de: " + dias);
}
public static void getSemanasCompletas(Calendar f1, Calendar f2, boolean c1Posterior){
Calendar start;
Calendar end;
if (c1Posterior == true){
start = f2;
end = f1;
} else{
start = f1;
end = f2;
}
int Semanas = 0;
Calendar addSemana = GregorianCalendar.getInstance();
addSemana.setTime(start.getTime());
addSemana.add(Calendar.DATE, 6);
while(addSemana.before(end)){
if(addSemana.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) Semanas++;
addSemana.add(Calendar.DATE,1);
}
System.out.println(
"El numero de semanas (de lunes a domingo) entre ambas fechas es: "
+ Semanas);
}
public static void validarFechas(String[] args){
boolean incorrecta = false;
if(Integer.parseInt(args[1]) < 1 || Integer.parseInt(args[1]) > 12){
System.out.println("Fecha incorrecta: " + Integer.parseInt(args[2]) + "/" + Integer.parseInt(args[1]) + "/" + Integer.parseInt(args[0]));
incorrecta = true;
}
if(Integer.parseInt(args[1]) == 1 || Integer.parseInt(args[1]) == 3 || Integer.parseInt(args[1]) == 5 || Integer.parseInt(args[1]) == 7 || Integer.parseInt(args[1]) == 8 || Integer.parseInt(args[1]) == 10 || Integer.parseInt(args[1]) == 12 ){
if(Integer.parseInt(args[2]) < 1 || Integer.parseInt(args[2]) > 31 ){
System.out.println("Fecha incorrecta: " + Integer.parseInt(args[2]) + "/" + Integer.parseInt(args[1]) + "/" + Integer.parseInt(args[0]));
incorrecta = true;
}
}
if(Integer.parseInt(args[1]) == 4 || Integer.parseInt(args[1]) == 6 || Integer.parseInt(args[1]) == 8 || Integer.parseInt(args[1]) == 9 || Integer.parseInt(args[1]) == 11){
if(Integer.parseInt(args[2]) < 1 || Integer.parseInt(args[2]) > 30 ){
System.out.println("Fecha incorrecta: " + Integer.parseInt(args[2]) + "/" + Integer.parseInt(args[1]) + "/" + Integer.parseInt(args[0]));
incorrecta = true;
}
}
if(Integer.parseInt(args[1]) == 2 && isLeapYear(Integer.parseInt(args[0])) == true){
if(Integer.parseInt(args[2]) < 1 || Integer.parseInt(args[2]) > 29){
System.out.println("Fecha incorrecta: " + Integer.parseInt(args[2]) + "/" + Integer.parseInt(args[1]) + "/" + Integer.parseInt(args[0]));
incorrecta = true;
}
} else if(Integer.parseInt(args[1]) == 2 && isLeapYear(Integer.parseInt(args[0])) == false){
if(Integer.parseInt(args[2]) < 1 || Integer.parseInt(args[2]) > 28){
System.out.println("Fecha incorrecta: " + Integer.parseInt(args[2]) + "/" + Integer.parseInt(args[1]) + "/" + Integer.parseInt(args[0]));
incorrecta = true;
}
}
if(Integer.parseInt(args[4]) < 1 || Integer.parseInt(args[4]) > 12){
System.out.println("Fecha incorrecta: " + Integer.parseInt(args[5]) + "/" + Integer.parseInt(args[4]) + "/" + Integer.parseInt(args[3]));
incorrecta = true;
}
if(Integer.parseInt(args[4]) == 1 || Integer.parseInt(args[4]) == 3 || Integer.parseInt(args[4]) == 5 || Integer.parseInt(args[4]) == 7 || Integer.parseInt(args[4]) == 8 || Integer.parseInt(args[4]) == 10 || Integer.parseInt(args[4]) == 12 ){
if(Integer.parseInt(args[5]) < 1 || Integer.parseInt(args[5]) > 31 ){
System.out.println("Fecha incorrecta: " + Integer.parseInt(args[5]) + "/" + Integer.parseInt(args[4]) + "/" + Integer.parseInt(args[0]));
incorrecta = true;
}
}
if(Integer.parseInt(args[4]) == 4 || Integer.parseInt(args[4]) == 6 || Integer.parseInt(args[4]) == 8 || Integer.parseInt(args[4]) == 9 || Integer.parseInt(args[4]) == 11){
if(Integer.parseInt(args[5]) < 1 || Integer.parseInt(args[5]) > 30 ){
System.out.println("Fecha incorrecta: " + Integer.parseInt(args[5]) + "/" + Integer.parseInt(args[4]) + "/" + Integer.parseInt(args[3]));
incorrecta = true;
}
}
if(Integer.parseInt(args[4]) == 2 && isLeapYear(Integer.parseInt(args[3])) == true){
if(Integer.parseInt(args[5]) < 1 || Integer.parseInt(args[5]) > 29){
System.out.println("Fecha incorrecta: " + Integer.parseInt(args[5]) + "/" + Integer.parseInt(args[4]) + "/" + Integer.parseInt(args[3]));
incorrecta = true;
}
} else if(Integer.parseInt(args[4]) == 2 && isLeapYear(Integer.parseInt(args[3])) == false){
if(Integer.parseInt(args[5]) < 1 || Integer.parseInt(args[5]) > 28){
System.out.println("Fecha incorrecta: " + Integer.parseInt(args[5]) + "/" + Integer.parseInt(args[4]) + "/" + Integer.parseInt(args[3]));
incorrecta = true;
}
}
if(incorrecta==true) {
System.exit(0);
}
}
public static boolean isLeapYear(int year) {
GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
return cal.isLeapYear(year);
}
}
Mi duda es:
Al ejecutar el programa tal como está, con la entrada '2014 11 25 2016 2 29', la salida es:
Cita: El numero de anhos, meses y dias transcurridos entre una fecha y otra es: 1 anhos, 3 meses, y 5 dias.
El numero de semanas (de lunes a domingo) entre ambas fechas es: 65
El numero de dias habiles entre ambas fechas es de: 329
Sin embargo, si en la función principal pongo getSemanasCompletas(c1,c2,c1esPosteriorOIgual); DESPUÉS QUE getDiasHabiles(c1,c2,c1esPosteriorOIgual);, es decir,
Código:
public static void main(String[] args) {
final Calendar c1 = new GregorianCalendar(Integer.parseInt(args[0]),Integer.parseInt(args[1]),Integer.parseInt(args[2]));
final Calendar c2 = new GregorianCalendar(Integer.parseInt(args[3]),Integer.parseInt(args[4]),Integer.parseInt(args[5]));
validarFechas(args);
boolean c1esPosteriorOIgual = ordenaFechas(c1,c2);
restaFechas(c1,c2,c1esPosteriorOIgual);
getDiasHabiles(c1,c2,c1esPosteriorOIgual);
getSemanasCompletas(c1,c2,c1esPosteriorOIgual);
}
La salida es:
Cita: El numero de anhos, meses y dias transcurridos entre una fecha y otra es: 1 anhos, 3 meses, y 5 dias.
El numero de dias habiles entre ambas fechas es de: 329
El numero de semanas (de lunes a domingo) entre ambas fechas es: 0
¿Por qué en este caso la salida de número de semanas es 0? Bueno, en realidad, sé que lo que ocurre es que, de algún modo, getDiasHabiles modifica la fecha 2 dejándola igual (con un día más) que la fecha1. Pero no sé por qué ocurre exactamente esto ni como arreglarlo.
Muchas gracias!