Advertisement
techno-

Ej 1 DS FINAL

Oct 4th, 2022
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.48 KB | None | 0 0
  1. package DateUtilities;
  2.  
  3. public class DateUtilities {
  4.     /**
  5.      * Indicates whether a year is a leap year . A leap year is divisible by 4,
  6.      * unless it is divisible by 100 , in which case it must be divisible by 400
  7.      * in order to be considered a leap year (e.g., 1900 is not a leap year ,
  8.      * but 2000 is) = > See the JUnit seminar for an example .
  9.      * @param year the given year
  10.      * @return True if the given year is a leap year , false otherwise .
  11.      */
  12.     public static boolean isLeap ( int year ) {
  13.         return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
  14.     }
  15.     /**
  16.      * Indicates the number of days of a given month . As the number of days in
  17.      * the month of February depends on the year , it is also necessary to pass
  18.      * the year as an argument .
  19.      * @param month The given month
  20.      * @param year The given year
  21.      * @return The number of days of that month in that year .
  22.      * @throws IllegalArgumentException if the month is not valid .
  23.      */
  24.     public static int numberOfDays ( int month , int year ) throws IllegalArgumentException{
  25.  
  26.         if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
  27.             return 31;
  28.         }
  29.         else if((month == 4 || month == 6 || month == 9 || month == 11)){
  30.             return 30;
  31.         }
  32.         else if(month==2){
  33.             if(isLeap(year))
  34.                 return 29;
  35.             else
  36.                 return 28;
  37.         }else{
  38.             throw new IllegalArgumentException("Incorrecto");
  39.         }
  40.     }
  41.     /**
  42.      * The ISO date format is a standard format that displays the dates
  43.      * starting with the year , followed by the month and the day , i.e. ,
  44.      * "YYYY -MM -DD ". For example , in that format the text " July 28 , 2006"
  45.      * would be represented as "2006 -07 -28".
  46.      * The " convertToISO " method converts a date in the " Month DD , AAAA "
  47.      * format to its ISO representation . For simplicity , let us assume that
  48.      * the values are correctly formatted even if the date is invalid
  49.      * (e.g., " February 31 , 2006" is correctly formatted but it is not a valid date )
  50.      *
  51.      * @param dateText Date in textual format ( USA style ).
  52.      * @return A string with the given date in ISO format .
  53.      */
  54.     public static String convertToISODate ( String dateText ) {
  55.         String dia, mes, year;
  56.  
  57.         String[] partes = dateText.split(" ");
  58.  
  59.  
  60.         dia = partes[1].substring(0,partes[1].length()-1);
  61.         mes = ConvierteMes(partes[0]);
  62.         year = partes[2];
  63.  
  64.         return String.join("-", year, mes, dia);
  65.     }
  66.  
  67.     public static String ConvierteMes(String mes){
  68.         if (mes.equals("January")) return "01";
  69.         if (mes.equals("February")) return "02";
  70.         if (mes.equals("March")) return "03";
  71.         if (mes.equals("April")) return "04";
  72.         if (mes.equals("May")) return "05";
  73.         if (mes.equals("June")) return "06";
  74.         if (mes.equals("July")) return "07";
  75.         if (mes.equals("August")) return "08";
  76.         if (mes.equals("September")) return "09";
  77.         if (mes.equals("October")) return "10";
  78.         if (mes.equals("November")) return "11";
  79.         if (mes.equals("December")) return "12";
  80.         else{
  81.             return "error";
  82.         }
  83.     }
  84.     /**
  85.      * Given a String representing an ISO - formatted date , the methods checks
  86.      * its validity . This includes checking for non - valid characters , erroneous
  87.      * string lengths , and the validity of the date itself (i.e. , checking the
  88.      * number of days of the month ).
  89.      * @param ISODate A date in ISO format
  90.      * @return True if the ISO - formatted date is a valid date , False otherwise .
  91.      */
  92.     public static boolean checkISODate ( String ISODate ) {
  93.         String[] partes = ISODate.split("-");
  94.         int year, mes, dia;
  95.  
  96.         //Comprobamos si el año no contiene caracteres inválidos
  97.         try {
  98.             year = Integer.parseInt(partes[0]);
  99.         }
  100.         catch (Exception e){
  101.             return false;
  102.         }
  103.         //Comprobamos si el mes no contiene caracteres invalidos
  104.         try {
  105.             mes = Integer.parseInt(partes[1]);
  106.         }
  107.         catch (Exception e){
  108.             return false;
  109.         }
  110.         //Comprobamos si el día tiene caracteres invalidos
  111.         try {
  112.             dia = Integer.parseInt(partes[2]);
  113.         }
  114.         catch (Exception e){
  115.             return false;
  116.         }
  117.  
  118.         //Comprobar si tiene guiones
  119.         if(ISODate.equals(partes[0])){
  120.             return false;
  121.         }
  122.         //Comprobar si la longitud es correcta
  123.         if(ISODate.length()!=10){
  124.             return false;
  125.         }
  126.         //Comprobar si el año es menor que 0
  127.         else if(year<0){
  128.             return false;
  129.         }
  130.         //Comprobar si el mes es correcto
  131.         else if(mes<1 || mes>12){
  132.             return false;
  133.         }
  134.  
  135.         else if(dia<1 || dia>31){
  136.             return false;
  137.         }
  138.         else if((mes == 4 || mes == 6 || mes == 9 || mes == 11) && dia>30){
  139.             return false;
  140.         }
  141.         else if(mes == 2 && dia == 28){
  142.             if (isLeap(year)){
  143.                 return false;
  144.             }else
  145.                 return true;
  146.         }
  147.         else if(mes == 2 && dia == 29){
  148.             if (isLeap(year)){
  149.                 return true;
  150.             }else
  151.                 return false;
  152.         }
  153.  
  154.         else return true;
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement