Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

public class Calendario { private int diaRef; private int mesRef; private int yearRef; private int semanaRef;

//private String semana[] = {"Domingo","Lunes","Martes","Miercoles","Jueves","Viernes","Sabado"}; //private String meses[] = {"Enero","Febrero","Marzo","Abril","Mayo","Junio", //"Julio","Agosto","Setiembre","Octubre","Noviembre","Diciembre"}; private int calendario[][] = new int[7][7]; public Calendario(){ cargaInicializacion(); buscarFecha(); //System.out.println(semana[semanaRef - 1] + ", " + diaRef + " de " + meses[mesRef - 1] + " del ao " + yearRef); imprimirCalendario(); } public static void main(String args[]){ new Calendario(); } private void cargaInicializacion(){ diaRef = 18; mesRef = 8; yearRef = 2011; semanaRef = 5; }

private void buscarFecha(){ int dia;int mes;int year; int may = 1;

//dia = LeerTeclado.readInt("dia"); dia = 1; mes = LeerTeclado.readInt("mes"); year = LeerTeclado.readInt("ao");

if(year < yearRef){ may = 0; }else if(year == yearRef){ if(mes < mesRef){ may = 0; }else if(mes == mesRef){ if(dia < diaRef){ may = 0; } } }

while(dia != diaRef || mes != mesRef || year != yearRef){ if(may == 1){ diaRef++; semanaRef++; if(semanaRef == 8) semanaRef = 1;

}else{ diaRef--; semanaRef--; if(semanaRef == 0) semanaRef = 7; } validaFecha(may); } } private void validaFecha(int may){ int diaTope; if(may == 1){ diaTope = dameTopeMes(mesRef); if(diaRef > diaTope){ diaRef = 1; mesRef++; if(mesRef == 13){ mesRef = 1; yearRef++; } } }else{ if(diaRef == 0){ mesRef--; if(mesRef == 0){ mesRef = 12; yearRef--;

} diaRef = dameTopeMes(mesRef); } } } private int dameTopeMes(int mes){ int tope = 0; if(mes == 1 || mes == 3 || mes == 5 || mes == 7 || mes == 8 || mes == 10 || mes == 12){ tope = 31; } if(mes == 4 || mes == 6 || mes == 9 || mes == 11){ tope = 30; } if(mes == 2){ if( yearRef % 4 != 0){ tope = 28; }else{ if(yearRef % 100 != 0){ tope = 29; }else{ if(yearRef % 400 == 0){ tope = 29; }else{ tope = 28; }

} } } return tope; } private void imprimirCalendario(){ int cantDias = dameTopeMes(mesRef); int sem = 0; String semana = "DLMMJVS"; for(int fil = 0; fil<6; fil++){ for(int col = 0; col<7; col++){ if( (col == semanaRef - 1) || sem != 0) { sem++; if(sem <= cantDias){ System.out.print("| " + sem + " | "); } } } System.out.println(); } } }

You might also like