Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package DateUtilities;
- public class DateUtilities {
- /**
- * Indicates whether a year is a leap year . A leap year is divisible by 4,
- * unless it is divisible by 100 , in which case it must be divisible by 400
- * in order to be considered a leap year (e.g., 1900 is not a leap year ,
- * but 2000 is) = > See the JUnit seminar for an example .
- * @param year the given year
- * @return True if the given year is a leap year , false otherwise .
- */
- public static boolean isLeap ( int year ) {
- return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
- }
- /**
- * Indicates the number of days of a given month . As the number of days in
- * the month of February depends on the year , it is also necessary to pass
- * the year as an argument .
- * @param month The given month
- * @param year The given year
- * @return The number of days of that month in that year .
- * @throws IllegalArgumentException if the month is not valid .
- */
- public static int numberOfDays ( int month , int year ) throws IllegalArgumentException{
- if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
- return 31;
- }
- else if((month == 4 || month == 6 || month == 9 || month == 11)){
- return 30;
- }
- else if(month==2){
- if(isLeap(year))
- return 29;
- else
- return 28;
- }else{
- throw new IllegalArgumentException("Incorrecto");
- }
- }
- /**
- * The ISO date format is a standard format that displays the dates
- * starting with the year , followed by the month and the day , i.e. ,
- * "YYYY -MM -DD ". For example , in that format the text " July 28 , 2006"
- * would be represented as "2006 -07 -28".
- * The " convertToISO " method converts a date in the " Month DD , AAAA "
- * format to its ISO representation . For simplicity , let us assume that
- * the values are correctly formatted even if the date is invalid
- * (e.g., " February 31 , 2006" is correctly formatted but it is not a valid date )
- *
- * @param dateText Date in textual format ( USA style ).
- * @return A string with the given date in ISO format .
- */
- public static String convertToISODate ( String dateText ) {
- String dia, mes, year;
- String[] partes = dateText.split(" ");
- dia = partes[1].substring(0,partes[1].length()-1);
- mes = ConvierteMes(partes[0]);
- year = partes[2];
- return String.join("-", year, mes, dia);
- }
- public static String ConvierteMes(String mes){
- if (mes.equals("January")) return "01";
- if (mes.equals("February")) return "02";
- if (mes.equals("March")) return "03";
- if (mes.equals("April")) return "04";
- if (mes.equals("May")) return "05";
- if (mes.equals("June")) return "06";
- if (mes.equals("July")) return "07";
- if (mes.equals("August")) return "08";
- if (mes.equals("September")) return "09";
- if (mes.equals("October")) return "10";
- if (mes.equals("November")) return "11";
- if (mes.equals("December")) return "12";
- else{
- return "error";
- }
- }
- /**
- * Given a String representing an ISO - formatted date , the methods checks
- * its validity . This includes checking for non - valid characters , erroneous
- * string lengths , and the validity of the date itself (i.e. , checking the
- * number of days of the month ).
- * @param ISODate A date in ISO format
- * @return True if the ISO - formatted date is a valid date , False otherwise .
- */
- public static boolean checkISODate ( String ISODate ) {
- String[] partes = ISODate.split("-");
- int year, mes, dia;
- //Comprobamos si el año no contiene caracteres inválidos
- try {
- year = Integer.parseInt(partes[0]);
- }
- catch (Exception e){
- return false;
- }
- //Comprobamos si el mes no contiene caracteres invalidos
- try {
- mes = Integer.parseInt(partes[1]);
- }
- catch (Exception e){
- return false;
- }
- //Comprobamos si el día tiene caracteres invalidos
- try {
- dia = Integer.parseInt(partes[2]);
- }
- catch (Exception e){
- return false;
- }
- //Comprobar si tiene guiones
- if(ISODate.equals(partes[0])){
- return false;
- }
- //Comprobar si la longitud es correcta
- if(ISODate.length()!=10){
- return false;
- }
- //Comprobar si el año es menor que 0
- else if(year<0){
- return false;
- }
- //Comprobar si el mes es correcto
- else if(mes<1 || mes>12){
- return false;
- }
- else if(dia<1 || dia>31){
- return false;
- }
- else if((mes == 4 || mes == 6 || mes == 9 || mes == 11) && dia>30){
- return false;
- }
- else if(mes == 2 && dia == 28){
- if (isLeap(year)){
- return false;
- }else
- return true;
- }
- else if(mes == 2 && dia == 29){
- if (isLeap(year)){
- return true;
- }else
- return false;
- }
- else return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement