Calendario

Realizar un programa en Java, que mediante un menú permita las tres opciones siguientes, más una última opción para finalizar el programa:


Opción 1: El usuario / a nos entrará el "Nombre" y la "fecha de nacimiento" de una persona. dd / mm / aaaa

El programa mostrará un calendario situado al mes y al día en que nació y nos dirá su signo del zodiaco. Tendremos que controlar que se hayan entrado correctamente los datos. Nos piden también que pongamos entre llaves el día en que nació y el día de la Semana.

Ejemplo: Nombre: Marta Riells Fecha: 14/07/1970

JULIO - 1970

Dl [M] X J V S D
1 2 3 4 5
6 7 8 9 10 11 12
13 [14] 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

Marta es Cáncer


Opción 2: El usuario / a nos introducirá en el mes (mm) y el año (aaaa) que se quiere mostrar.

Se nos visualizará el calendario del mes para pantalla y además se debe permitir:

cambiar de mes mediante las letras: ◄ A (de Anterior) y S (de Siguiente) ►

y cambiar de año mediante: ▲ K (de Anterior) y M (de Siguiente) ▼


Opción 3: El usuario nos introducirá la semana (ss) y el año (aaaa) que se quiere mostrar.

Se nos visualizará el calendario por pantalla indicando la semana deseada:

Ejemplo: Semana: 07 Año: 2008

  Febrero 2008
septiembre L M X J V S D
5 1 2 3
6 4 5 6 7 8 9 10
7 11 12 13 14 15 16 17 <<<<
8 18 19 20 21 22 23 24
9 25 26 27 28 29


La fórmula que permite conocer el primer día del mes "m" del año "a":

a) Para los meses de Enero o Febrero:

n = a + 31 * (m - 1) + (a - 1) div 4-3 * ((a + 99) div 100) div 4

b) El resto de meses:

n = a + 31 * (m-1) + 1 - (4 * m + 23) div 10 + en div 4 - (3 * (a div 100 + 1)) div 4-1

Anotaciones:

• (n Mod 7) indica el día de la semana (0 = Domingo, 1 = Lunes, etc ...)

• Mod -> es el módulo de la división entera (%) / Div. -> es la división entera.



  1. //importamos las librerias
  2.    import java.io.*;
  3.    import java.util.*;
  4.    import cs1.Keyboard;
  5.    import java.lang.String;
  6.    import java.text.NumberFormat;
  7.    import java.text.DecimalFormat;
  8.    import java.text.SimpleDateFormat;
  9.    import java.text.DateFormat;
  10.    import java.text.ParseException;
  11.    import java.util.Date;
  12.    public class calendario {
  13.       public static void main (String[] args){
  14.      
  15.       //Definimos las variables
  16.          char menu, letra='0';
  17.          String nombre, fecha, StringMes="";
  18.          int dia, mes, año, PrimerDiaMes, DiaDeLaSemana, numDias = 1, posicion,j=0, z=1, h=1;
  19.          Double n;
  20.          boolean most=false, añocam=false;
  21.      
  22.          //Introducimos por teclado la opcion correspondiente para el menu
  23.          System.out.print ("Introduce una letra de la a la c, para escojer una opcion: ");
  24.          menu = Keyboard.readChar();   
  25.          menu=Character.toLowerCase(menu);
  26.      
  27.      
  28.          switch (menu) {
  29.          
  30.             default: System.out.print ("\nNo has introducido una opcion correcta! ");
  31.                break;
  32.          
  33.             case 'a':
  34.                {
  35.                   System.out.println("Opció 1:  el usuario entrarà el “Nombre” y la “fecha de nacimiento” de una persona en formato dd/mm/aaaa");
  36.                      
  37.                   System.out.print (" introduce el nombre : ");
  38.                   nombre = Keyboard.readString();
  39.                
  40.                   System.out.print ("\nIntroduce la fecha de nacimiento (dd/mm/aaa): ");
  41.                   fecha = Keyboard.readString();
  42.                
  43.                   DateFormat df = new SimpleDateFormat("dd/MM/yyyy");//las fechas en nuestro programa tendrán ese formato.
  44.                   NumberFormat nf = new DecimalFormat("#0");//para la nota      
  45.                
  46.                   try {
  47.                      Date fecha1;
  48.                  
  49.                      fecha1 = df.parse(fecha);//convertir el string de la fecha a Date.
  50.                      
  51.                   }
  52.                      catch (ParseException e) {
  53.                         System.out.println("La fecha no es correcta segun el patrón: " +((SimpleDateFormat)df).toPattern() );
  54.                         System.exit(0);
  55.                      } //fin comprobación de error de fechas
  56.                        
  57.                   System.out.println("");
  58.                   System.out.print("\tNom: " + nombre + "\t\tData: " + fecha);//Muestra el nombre y fecha introducido por pantalla
  59.                
  60.                   dia = Integer.valueOf(fecha.substring(0,2));
  61.                   mes = Integer.valueOf(fecha.substring(3,5));
  62.                   año = Integer.valueOf(fecha.substring(6,10));
  63.                
  64.                   switch (mes) {
  65.                  
  66.                      case 1:
  67.                      case 2:
  68.                         n = (double)año + 31*((double)mes - 1) + ((double)año - 1) / 4 - 3*(((double)año + 99) /100) / 4;
  69.                         break;
  70.                      default:
  71.                      //n = a + 31*(m-1) + 1 - (4*m + 23) div 10 + a div 4 - (3*(a div 100 + 1)) div 4 - 1
  72.                         n = (double)año + 31*((double)mes-1)+1- (4*(int)mes + 23) / 10 + (double)año / 4 -(3*((double)año / 100 + 1))/4-1;
  73.                         break;
  74.                   }
  75.                
  76.                   PrimerDiaMes = Integer.valueOf(nf.format(n)) % 7;//(n Mod 7) indica el dia de la setmana ( 0 = Diumenge, 1 = Dilluns, etc...)
  77.                
  78.                  // System.out.print ("\n\n"+PrimerDiaMes+"\n\n");
  79.                
  80.                   switch (mes) {
  81.                      case 1:
  82.                         StringMes = "Enero";
  83.                         break;
  84.                      case 2:
  85.                         StringMes = "Febrero";
  86.                         break;
  87.                      case 3:
  88.                         StringMes = "Marzo";
  89.                         break;
  90.                      case 4:
  91.                         StringMes = "Abril";
  92.                         break;
  93.                      case 5:
  94.                         StringMes = "Mayo";
  95.                         break;
  96.                      case 6:
  97.                         StringMes = "Junio";
  98.                         break;
  99.                      case 7:
  100.                         StringMes = "Julio";
  101.                         break;
  102.                      case 8:
  103.                         StringMes = "Agosto";
  104.                         break;
  105.                      case 9:
  106.                         StringMes = "Septiembre";
  107.                         break;
  108.                      case 10:
  109.                         StringMes = "Octubre";
  110.                         break;
  111.                      case 11:
  112.                         StringMes = "Noviembre";
  113.                         break;
  114.                      case 12:
  115.                         StringMes = "Diciembre";
  116.                         break;
  117.                   }
  118.                
  119.                //cuantos dias tiene el mes
  120.                   switch (mes) {
  121.                      case 1:
  122.                      case 3:
  123.                      case 5:
  124.                      case 7:
  125.                      case 8:
  126.                      case 10:
  127.                      case 12:
  128.                         numDias = 31;
  129.                         break;
  130.                      case 4:
  131.                      case 6:
  132.                      case 9:
  133.                      case 11:
  134.                         numDias = 30;
  135.                         break;
  136.                      case 2:
  137.                         if ( ((año % 4 == 0) && !(año % 100 == 0)) || (año % 400 == 0) ) {
  138.                            numDias = 29;}
  139.                         else {
  140.                            numDias = 28;}
  141.                         break;
  142.                   }//fin switch
  143.                
  144.                
  145.                //CALENDARIO
  146.                   System.out.print ("\n\n               " + StringMes + " " + año+"\n--------------------------------------------------");
  147.                
  148.                   n = (+ dia) - 1;
  149.                   DiaDeLaSemana = Integer.valueOf(nf.format(n)) % 7;
  150.                
  151.                   switch (DiaDeLaSemana) {
  152.                      case 0:
  153.                         System.out.print ("\nDL DT      DC      DJ      DV      DS      [DG]\n");
  154.                         break;
  155.                      case 1:
  156.                         System.out.print ("\n[DL]       DT      DC      DJ      DV      DS      DG\n");
  157.                         break;
  158.                      case 2:
  159.                         System.out.print ("\nDL [DT]    DC      DJ      DV      DS      DG\n");
  160.                         break;
  161.                      case 3:
  162.                         System.out.print ("\nDL DT      [DC]    DJ      DV      DS      DG\n");
  163.                         break;
  164.                      case 4:
  165.                         System.out.print ("\nDL DT      DC      [DJ]    DV      DS      DG\n");
  166.                         break;
  167.                      case 5:
  168.                         System.out.print ("\nDL DT      DC      DJ      [DV]    DS      DG\n");
  169.                         break;
  170.                      case 6:
  171.                         System.out.print ("\nDL DT      DC      DJ      DV      [DS]    DG\n");
  172.                         break;
  173.                  
  174.                   }    
  175.                
  176.                   switch (PrimerDiaMes) {
  177.                      case 0:
  178.                         posicion = 7;
  179.                      
  180.                         System.out.print ("                                             ");
  181.                      
  182.                         for (int i = 1; i <= numDias; i++) {
  183.                        
  184.                            if (== dia) {
  185.                               System.out.print ("["+i+"]        ");
  186.                            }
  187.                            else {
  188.                               System.out.print (i+"     ");
  189.                            }
  190.                        
  191.                            posicion = posicion + 1;
  192.                        
  193.                            if (posicion > 7) { System.out.println(); posicion = 1;}
  194.                        
  195.                         }
  196.                      
  197.                         break;
  198.                  
  199.                      case 1:
  200.                         posicion = 1;
  201.                      
  202.                         System.out.print ("");
  203.                      
  204.                         for (int i = 1; i <= numDias; i++) {
  205.                        
  206.                            if (== dia) {
  207.                               System.out.print ("["+i+"]        ");
  208.                            }
  209.                            else {
  210.                               System.out.print (i+"     ");
  211.                            }
  212.                        
  213.                            posicion = posicion + 1;
  214.                        
  215.                            if (posicion > 7) { System.out.println(); posicion = 1;}
  216.                        
  217.                         }
  218.                      
  219.                         break;
  220.                  
  221.                      case 2:
  222.                      
  223.                         posicion = 2;
  224.                      
  225.                         System.out.print ("     ");
  226.                      
  227.                         for (int i = 1; i <= numDias; i++) {
  228.                        
  229.                            if (== dia) {
  230.                               System.out.print ("["+i+"]        ");
  231.                            }
  232.                            else {
  233.                               System.out.print (i+"     ");
  234.                            }
  235.                        
  236.                            posicion = posicion + 1;
  237.                        
  238.                            if (posicion > 7) { System.out.println(); posicion = 1;}
  239.                        
  240.                         }
  241.                      
  242.                         break;
  243.                  
  244.                      case 3:
  245.                         posicion = 3;
  246.                      
  247.                         System.out.print ("             ");
  248.                      
  249.                         for (int i = 1; i <= numDias; i++) {
  250.                        
  251.                            if (== dia) {
  252.                               System.out.print ("["+i+"]        ");
  253.                            }
  254.                            else {
  255.                               System.out.print (i+"     ");
  256.                            }
  257.                        
  258.                            posicion = posicion + 1;
  259.                        
  260.                            if (posicion > 7) { System.out.println(); posicion = 1;}
  261.                        
  262.                         }
  263.                      
  264.                         break;
  265.                  
  266.                      case 4:
  267.                         posicion = 4;
  268.                      
  269.                         System.out.print ("                     ");
  270.                      
  271.                         for (int i = 1; i <= numDias; i++) {
  272.                        
  273.                            if (== dia) {
  274.                               System.out.print ("["+i+"]        ");
  275.                            }
  276.                            else {
  277.                               System.out.print (i+"     ");
  278.                            }
  279.                        
  280.                            posicion = posicion + 1;
  281.                        
  282.                            if (posicion > 7) { System.out.println(); posicion = 1;}
  283.                        
  284.                         }
  285.                      
  286.                         break;
  287.                  
  288.                      case 5:
  289.                         posicion = 5;
  290.                      
  291.                         System.out.print ("                             ");
  292.                      
  293.                         for (int i = 1; i <= numDias; i++) {
  294.                        
  295.                            if (== dia) {
  296.                               System.out.print ("["+i+"]        ");
  297.                            }
  298.                            else {
  299.                               System.out.print (i+"     ");
  300.                            }
  301.                        
  302.                            posicion = posicion + 1;
  303.                        
  304.                            if (posicion > 7) { System.out.println(); posicion = 1;}
  305.                        
  306.                         }
  307.                      
  308.                         break;
  309.                  
  310.                      case 6:
  311.                         posicion = 6;
  312.                      
  313.                         System.out.print ("                                     ");
  314.                      
  315.                         for (int i = 1; i <= numDias; i++) {
  316.                        
  317.                            if (== dia) {
  318.                               System.out.print ("["+i+"]        ");
  319.                            }
  320.                            else {
  321.                               System.out.print (i+"     ");
  322.                            }
  323.                        
  324.                            posicion = posicion + 1;
  325.                        
  326.                            if (posicion > 7) { System.out.println(); posicion = 1;}
  327.                        
  328.                         }
  329.                      
  330.                         break;
  331.                  
  332.                   }    
  333.                
  334.                //CALCULAR SIGNO ZODIACAL
  335.                   if ( (mes == 3 && dia >= 21) || (mes == 4 && dia <=20)  ) {
  336.                      System.out.print ("\n\n    En/Na "+nombre+" és Àries");
  337.                   }
  338.                   else {
  339.                      if ( (mes == 4 && dia >= 21) || (mes == 5 && dia <=20)  ) {
  340.                         System.out.print ("\n\n En/Na "+nombre+" Taure");
  341.                      }
  342.                      else {
  343.                         if ( (mes == 5 && dia >= 21) || (mes == 6 && dia <=21)  ) {
  344.                            System.out.print ("\n\n      En/Na "+nombre+" és Gèminis");
  345.                         }
  346.                         else {
  347.                            if ( (mes == 6 && dia >= 22) || (mes == 7 && dia <=22)  ) {
  348.                               System.out.print ("\n\n   En/Na "+nombre+" és Càncer");
  349.                            }
  350.                            else{
  351.                               if ( (mes == 7 && dia >= 23) || (mes == 8 && dia <=22)  ) {
  352.                                  System.out.print ("\n\n        En/Na "+nombre+" és Lleó");
  353.                               }
  354.                               else{
  355.                                  if ( (mes == 8 && dia >= 23) || (mes == 9 && dia <=23)  ) {
  356.                                     System.out.print ("\n\n     En/Na "+nombre+" és Verge");
  357.                                  }
  358.                                  else{
  359.                                     if ( (mes == 9 && dia >= 23) || (mes == 10 && dia <=22)  ) {
  360.                                        System.out.print ("\n\n  En/Na "+nombre+" és Balança");
  361.                                     }
  362.                                     else{
  363.                                        if ( (mes == 10 && dia >= 23) || (mes == 11 && dia <=21)  ) {
  364.                                           System.out.print ("\n\n       En/Na "+nombre+" és Escorpió");
  365.                                        }
  366.                                        else{
  367.                                           if ( (mes == 11 && dia >= 22) || (mes == 12 && dia <=21)  ) {
  368.                                              System.out.print ("\n\n    En/Na "+nombre+" és Sagitari");
  369.                                           }
  370.                                           else{
  371.                                              if ( (mes == 12 && dia >= 22) || (mes == 1 && dia <=19)  ) {
  372.                                                 System.out.print ("\n\n En/Na "+nombre+" és Capricorn");
  373.                                              }
  374.                                              else{
  375.                                                 if ( (mes == 1 && dia >= 20) || (mes == 2 && dia <=18)  ) {
  376.                                                    System.out.print ("\n\n      En/Na "+nombre+" és Acuari");
  377.                                                 }
  378.                                                 else{
  379.                                                    System.out.print ("\n\n      En/Na "+nombre+" és Peixos");
  380.                                                 }
  381.                                              }
  382.                                           }
  383.                                        }
  384.                                     }
  385.                                  }
  386.                               }
  387.                            }   
  388.                         }
  389.                      }
  390.                   }                            
  391.                
  392.                }
  393.                
  394.            
  395.                break;
  396.          
  397.             case 'b':
  398.                {
  399.                   System.out.println("Opció 2:  L'usuari/a ens introduirà el mes (mm) i l'any (aaaa) que es vol mostrar.");
  400.                
  401.                
  402.                   NumberFormat nf = new DecimalFormat("#0");    
  403.                
  404.                //Introducimos el mes y el año 
  405.                   do {
  406.                      System.out.print ("\nIntrodueix el mes: ");
  407.                      mes = Keyboard.readInt();
  408.                   }
  409.                   while(mes<1 || mes>12);
  410.                
  411.                
  412.                   System.out.print ("\nIntrodueix l'any : ");
  413.                   año = Keyboard.readInt();
  414.                
  415.                
  416.                   do {
  417.                      switch (mes) {
  418.                      
  419.                         case 1:
  420.                            n = (double)año + 31*((double)mes - 1) + ((double)año - 1) / 4 - 3*(((double)año + 99)/ 100) / 4;
  421.                            break;
  422.                         case 2:
  423.                            n = (double)año + 31*((double)mes - 1) + ((double)año - 1) / 4 - 3*(((double)año + 99)/ 100) / 4;
  424.                            break;
  425.                         default:
  426.                            n = (double)año + 31*((double)mes-1) + 1 - (4*(double)mes + 23) / 10 + (double)año / 4- (3*((double)año / 100 + 1)) / 4 - 1;
  427.                            break;
  428.                      }
  429.                  
  430.                      PrimerDiaMes = Integer.valueOf(nf.format(n)) % 7;//(n Mod 7) indica el dia de la setmana ( 0 = Diumenge, 1 = Dilluns, etc...)
  431.                  
  432.                      switch (mes) {
  433.                         case 1:
  434.                            StringMes = "Enero";
  435.                            break;
  436.                         case 2:
  437.                            StringMes = "Febrero";
  438.                            break;
  439.                         case 3:
  440.                            StringMes = "Marzo";
  441.                            break;
  442.                         case 4:
  443.                            StringMes = "Abril";
  444.                            break;
  445.                         case 5:
  446.                            StringMes = "Mayo";
  447.                            break;
  448.                         case 6:
  449.                            StringMes = "Junio";
  450.                            break;
  451.                         case 7:
  452.                            StringMes = "Julio";
  453.                            break;
  454.                         case 8:
  455.                            StringMes = "Agosto";
  456.                            break;
  457.                         case 9:
  458.                            StringMes = "Septiembre";
  459.                            break;
  460.                         case 10:
  461.                            StringMes = "Octubre";
  462.                            break;
  463.                         case 11:
  464.                            StringMes = "Noviembre";
  465.                            break;
  466.                         case 12:
  467.                            StringMes = "Diciembre";
  468.                            break;
  469.                      }
  470.                  
  471.                   //cuantos dias tiene el mes
  472.                      switch (mes) {
  473.                         case 1:
  474.                         case 3:
  475.                         case 5:
  476.                         case 7:
  477.                         case 8:
  478.                         case 10:
  479.                         case 12:
  480.                            numDias = 31;
  481.                            break;
  482.                         case 4:
  483.                         case 6:
  484.                         case 9:
  485.                         case 11:
  486.                            numDias = 30;
  487.                            break;
  488.                         case 2:
  489.                            if ( ((año % 4 == 0) && !(año % 100 == 0)) || (año % 400 == 0) ) {
  490.                               numDias = 29;}
  491.                            else {
  492.                               numDias = 28;}
  493.                            break;
  494.                      }//fin switch
  495.                  
  496.                            
  497.                   //Calendario
  498.                      System.out.print ("\n\n            " + StringMes + " " + año+"\n--------------------------------------------------");
  499.                      System.out.print ("\nDL    DT      DC      DJ      DV      DS      DG\n");
  500.                          
  501.                        
  502.                        
  503.                  
  504.                  
  505.                      switch (PrimerDiaMes) {
  506.                      
  507.                         case 0:
  508.                            posicion = 7;
  509.                            System.out.print ("                                          ");
  510.                            for (int i = 1; i <= numDias; i++) {
  511.                            
  512.                            
  513.                               System.out.print (i+"     ");
  514.                            
  515.                               posicion = posicion + 1;
  516.                            
  517.                               if (posicion > 7) { System.out.println(); posicion = 1;}
  518.                              
  519.                            }
  520.                            break;
  521.                         case 1:
  522.                            posicion = 1;
  523.                        
  524.                            System.out.print ("");
  525.                        
  526.                            for (int i = 1; i <= numDias; i++) {
  527.                            
  528.                               System.out.print (i+"     ");
  529.                            
  530.                               posicion = posicion + 1;
  531.                            
  532.                               if (posicion > 7) { System.out.println(); posicion = 1;}
  533.                              
  534.                            }
  535.                        
  536.                            break;
  537.                        
  538.                         case 2:
  539.                            posicion = 2;
  540.                        
  541.                            System.out.print ("  ");
  542.                        
  543.                            for (int i = 1; i <= numDias; i++) {
  544.                            
  545.                               System.out.print (i+"     ");
  546.                               posicion = posicion + 1;
  547.                            
  548.                            
  549.                               if (posicion > 7) { System.out.println(); posicion = 1;}
  550.                            }
  551.                        
  552.                            break;
  553.                      
  554.                        
  555.                         case 3:
  556.                        
  557.                            posicion = 3;
  558.                        
  559.                            System.out.print ("          ");
  560.                        
  561.                            for (int i = 1; i <= numDias; i++) {
  562.                            
  563.                               System.out.print (i+"     ");
  564.                            
  565.                               posicion = posicion + 1;
  566.                            
  567.                               if (posicion > 7) { System.out.println(); posicion = 1;}
  568.                                                        
  569.                            }
  570.                            break;
  571.                      
  572.                                                
  573.                         case 4:
  574.                            posicion = 4;
  575.                        
  576.                            System.out.print ("                  ");
  577.                        
  578.                            for (int i = 1; i <= numDias; i++) {
  579.                            
  580.                               System.out.print (i+"     ");
  581.                            
  582.                               posicion = posicion + 1;
  583.                            
  584.                               if (posicion > 7) { System.out.println(); posicion = 1;}
  585.                              
  586.                            }
  587.                        
  588.                            break;
  589.                        
  590.                         case 5:
  591.                            posicion = 5;
  592.                        
  593.                            System.out.print ("                          ");
  594.                        
  595.                            for (int i = 1; i <= numDias; i++) {
  596.                            
  597.                               System.out.print (i+"     ");
  598.                            
  599.                               posicion = posicion + 1;
  600.                            
  601.                               if (posicion > 7) { System.out.println(); posicion = 1;}
  602.                            
  603.                            }
  604.                            break;
  605.                        
  606.                          
  607.                        
  608.                         case 6:
  609.                            {
  610.                               posicion = 6;
  611.                            
  612.                               System.out.print ("                                       ");
  613.                            
  614.                               for (int i = 1; i <= numDias; i++) {
  615.                              
  616.                              
  617.                                  System.out.print (i+"  ");
  618.                              
  619.                                  posicion = posicion + 1;
  620.                              
  621.                                  if (posicion > 7) { System.out.println(); posicion = 1;}
  622.                              
  623.                               }
  624.                               break;
  625.                            }}System.out.print ("\n");
  626.                      System.out.print ("\nCanviar de mes mitjançant les lletres A ( d'Anterior ) i S ( de Següent )  ");
  627.                      System.out.print ("\nCanviar d'any mitjançant: K  (d'Anterior ) i M ( de Següent )  ");
  628.                      letra = Keyboard.readChar();
  629.                      letra=Character.toUpperCase(letra);
  630.                      switch (letra) {
  631.                            
  632.                         case 'A' :
  633.                            if (mes==1){
  634.                               mes=12;
  635.                               año=año-1;}
  636.                            else{
  637.                               mes=mes-1;}
  638.                            break;
  639.                         case 'S':
  640.                            if (mes==12){
  641.                               mes=1;
  642.                               año=año+1;}
  643.                            else{
  644.                               mes=mes+1;}
  645.                            break;                          
  646.                         case 'M':
  647.                            año=año+1;
  648.                            break;
  649.                         case 'K':
  650.                            año=año-1;
  651.                            break;}
  652.                            
  653.                                                              
  654.                   }while(letra==('A')||letra==('S')||letra==('K')||letra==('M'));
  655.                
  656.                
  657.                
  658.                                        
  659.                              
  660.                   break;
  661.                          
  662.                }              
  663.            
  664.             case 'c':  System.out.println("Opció 3:  L'usuari ens introduirà la setmana (ss) i l'any (aaaa) que es vol mostrar.");
  665.                {
  666.                                
  667.                //Introducimos por teclado el dia y año
  668.                   System.out.print("\nIntrodueix Setmana: "); int setm=Keyboard.readInt();
  669.                   System.out.print("\nIntrodueix Any: "); int iany=Keyboard.readInt();
  670.                   System.out.println("");
  671.                   Calendar cal = Calendar.getInstance();
  672.                
  673.                   cal.set(Calendar.YEAR, iany);
  674.                //cal.get(Calendar.YEAR);
  675.                   cal.set(Calendar.WEEK_OF_YEAR, setm);
  676.                
  677.                   int imes=cal.get(Calendar.MONTH);
  678.                   imes=imes+1;
  679.                
  680.                   System.out.println ("\tSetmana: "     +setm+          "\tAny: " +iany);
  681.                
  682.                   switch(imes){
  683.                      case 1:
  684.                         System.out.println("            Gener - "+cal.get(Calendar.YEAR));
  685.                         break;
  686.                      case 2:
  687.                         System.out.println("            Febrer - "+cal.get(Calendar.YEAR));
  688.                         break;
  689.                      case 3:
  690.                         System.out.println("            Març - "+cal.get(Calendar.YEAR));
  691.                         break;
  692.                      case 4:
  693.                         System.out.println("            Abril - "+cal.get(Calendar.YEAR));
  694.                         break;
  695.                      case 5:
  696.                         System.out.println("            Maig - "+cal.get(Calendar.YEAR));
  697.                         break;
  698.                      case 6:
  699.                         System.out.println("            Juny - "+cal.get(Calendar.YEAR));
  700.                         break;
  701.                      case 7:
  702.                         System.out.println("            Juliol - "+cal.get(Calendar.YEAR));
  703.                         break;
  704.                      case 8:
  705.                         System.out.println("            Agost - "+cal.get(Calendar.YEAR));
  706.                         break;
  707.                      case 9:
  708.                         System.out.println("            Septembre - "+cal.get(Calendar.YEAR));
  709.                         break;
  710.                      case 10:
  711.                         System.out.println("            Octubre - "+cal.get(Calendar.YEAR));
  712.                         break;
  713.                      case 11:
  714.                         System.out.println("            Novrembre - "+cal.get(Calendar.YEAR));
  715.                         break;
  716.                      case 12:
  717.                         System.out.println("            Desembre - "+cal.get(Calendar.YEAR));
  718.                         break;
  719.                   }
  720.                   System.out.println("");
  721.                
  722.                
  723.                
  724.                   if(imes==1 || imes==2){
  725.                  
  726.                      int primerdia= (iany+(31*(imes-1))+((iany-1)/4)-((3*((iany+99)/100)/4)));
  727.                      PrimerDiaMes=primerdia%7;
  728.                  
  729.                   }
  730.                   else {
  731.                      int primerdia=(iany+(31*(imes-1))+1-(((4*imes)+23)/10)+(iany/4)-(((3*((iany/100)+1)))/4)-1);
  732.                      PrimerDiaMes=primerdia%7;
  733.                   }
  734.                   System.out.println("set       DL      DM      DM      DJ      DV      DS      DG");
  735.                
  736.                   int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
  737.                   Calendar cal2 = Calendar.getInstance();
  738.                   cal2.set(Calendar.MONTH, imes-1);
  739.                
  740.                   cal2.set(Calendar.YEAR, iany);
  741.                   cal2.set(Calendar.WEEK_OF_MONTH1);
  742.                
  743.                //**********************************************
  744.                
  745.                   Calendar cal3=Calendar.getInstance();
  746.                   cal3.clear();
  747.                   cal3.set(Calendar.YEAR, iany-1);
  748.                   int semant=  cal3.getActualMaximum(Calendar.WEEK_OF_YEAR);
  749.                   int esmesenero= cal2.get(Calendar.MONTH);
  750.                   z=cal2.get(Calendar.WEEK_OF_YEAR);
  751.                
  752.                   if(setm==53){
  753.                      z=cal2.get(Calendar.WEEK_OF_YEAR)+1;
  754.                      System.out.print(z);
  755.                   }
  756.                   else if( semant==52 && esmesenero==0 && PrimerDiaMes<=4){
  757.                      z=cal2.get(Calendar.WEEK_OF_YEAR);
  758.                      System.out.print(z);
  759.                   }
  760.                   else if( semant==52 && esmesenero==0 && ((PrimerDiaMes<=4)==false) ){
  761.                      System.out.print("52");
  762.                      z=cal2.get(Calendar.WEEK_OF_YEAR)-1;
  763.                   }
  764.                   else if(PrimerDiaMes>4 && esmesenero==0){
  765.                      System.out.print("53");
  766.                      z=cal2.get(Calendar.WEEK_OF_YEAR)-1;
  767.                   }
  768.                   else{
  769.                      z=cal2.get(Calendar.WEEK_OF_YEAR);
  770.                      System.out.print(z);
  771.                   }
  772.                //********************************************
  773.                   switch (PrimerDiaMes){
  774.                      case 0:
  775.                         System.out.print("                                              ");
  776.                         j=7;
  777.                         break;
  778.                      case 1:
  779.                         j=1;
  780.                         break;
  781.                      case 2:
  782.                         System.out.print("      ");
  783.                         j=2;
  784.                         break;
  785.                      case 3:
  786.                         System.out.print("              ");
  787.                         j=3;
  788.                         break;
  789.                      case 4:
  790.                         System.out.print("                      ");
  791.                         j=4;
  792.                         break;
  793.                      case 5:
  794.                         System.out.print("                              ");
  795.                         j=5;
  796.                         break;
  797.                      case 6:
  798.                         System.out.print("                                      ");
  799.                         j=6;
  800.                         break;
  801.                   }
  802.                   System.out.print("\t");
  803.                   for (int i=1; i<=days; i++){
  804.                      System.out.print(i+"       ");
  805.                      if (j%7==0){
  806.                         z++;
  807.                         cal2.set(Calendar.WEEK_OF_YEAR, z);
  808.                         h=cal2.get(Calendar.WEEK_OF_YEAR)-1;
  809.                         if(h==setm){
  810.                            System.out.print("<<<<<");  
  811.                            most=true;
  812.                         }
  813.                         System.out.print("\n");
  814.                         System.out.print(z+"\t");
  815.                      }
  816.                      j++;
  817.                   }
  818.                   if( most==false){
  819.                      int ultitit=cal.get(Calendar.DAY_OF_WEEK)+1;
  820.                      int imprtab= 7-ultitit;
  821.                      switch(imprtab){
  822.                         case 0:
  823.                            System.out.print(" <<<<<");
  824.                            break;
  825.                         case 1:
  826.                            System.out.print("\t"+" <<<<<");
  827.                            break;
  828.                         case 2:
  829.                            System.out.print("\t\t"+" <<<<<");
  830.                            break;
  831.                         case 3:
  832.                            System.out.print("\t\t\t"+" <<<<<");
  833.                            break;
  834.                         case 4:
  835.                            System.out.print("\t\t\t\t"+" <<<<<");
  836.                            break;
  837.                         case 5:
  838.                            System.out.print("\t\t\t\t\t"+" <<<<<");
  839.                            break;
  840.                         case 6:
  841.                            System.out.print("\t\t\t\t\t\t"+" <<<<<");
  842.                            break;
  843.                      }
  844.                   }
  845.                }
  846.          }               }
  847.                  
  848.                      
  849.    }

No hay comentarios:

Publicar un comentario