Ow ... more exceptional code and brute-force date parsing



  • public class ConstraintException extends Exception ...
    
    public class EnumConstraintException extends ConstraintException ...
    
    public class CharacterConstraintException extends ConstraintException ...
    
    public class NumericConstraintException extends ConstraintException ...
    
    public class OverflowException extends NumericConstraintException ...
    
    public class UnderflowException extends NumericConstraintException ...

    The way these are all used ... bounds-checking during object instantiation and/or within setters ... could be handled more simply by using IllegalArgumentException.


    Also found during this recent spelunk into the code (gotta love brute-force):


    public static Calendar parseCalendar(String dateString) throws DateFormatException {
      Date date = parseDate(dateString);
      Calendar calObject = Calendar.getInstance();
      calObject.setTime(date);
      return calObject;
    }
    

    public static Date parseDate(String dateString) throws DateFormatException {
    if (dateString == null) {
    return null;
    }

    int day = 0;
    int month = 0;
    int year = 0;
    int hour = 0;
    int min = 0;
    int sec = 0;

    Calendar calObject = Calendar.getInstance();

    Date rtn = null;

    String date = null;
    String time = null;
    String sep = null;
    String holder = null;

    String[] dateArray = dateString.split(" ");
    StringTokenizer tokenizer = null;
    StringBuffer buffer = new StringBuffer();

    // try to determine if time is included separated by a space
    if (dateArray.length == 2) {
    date = new String(dateArray[0]);
    time = new String(dateArray[1]);
    } else if (dateArray.length > 2) {
    if (dateArray.length == 3) {
    date = new String(dateString);
    } else {
    date = new String(dateArray[0] + "-" + dateArray[1] + "-" + dateArray[2]);
    buffer = new StringBuffer();
    for (int x = 3; x < dateArray.length; x++) {
    if (x != 3) {
    buffer.append(":");
    }
    buffer.append(dateArray[x]);
    }
    time = new String(buffer.toString());
    }
    } else {
    date = new String(dateString);
    }

    // try to determine the separator string
    if (date.indexOf("-") > 0) {
    sep = "-";
    } else if (date.indexOf(":") > 0) {
    sep = ":";
    } else if (date.indexOf(";") > 0) {
    sep = ";";
    } else if (date.trim().indexOf(" ") > 0) {
    sep = " ";
    } else if (date.indexOf("/") > 0) {
    sep = "/";
    } else {
    throw new DateFormatException("unable to determine proper separator character in date string");
    }

    try {
    tokenizer = new StringTokenizer(date, sep);
    if (!(sep.equals("/"))) {
    day = Integer.parseInt(tokenizer.nextToken());
    holder = tokenizer.nextToken().trim().toUpperCase();
    // do this check to see if a number was entered
    if (holder != holder.trim().toLowerCase()) {
    if (holder.startsWith("JAN")) {
    month = 0;
    } else if (holder.startsWith("F")) {
    month = 1;
    } else if (holder.startsWith("MAR")) {
    month = 2;
    } else if (holder.startsWith("AP")) {
    month = 3;
    } else if (holder.startsWith("MAY")) {
    month = 4;
    } else if (holder.startsWith("JUN")) {
    month = 5;
    } else if (holder.startsWith("JUL")) {
    month = 6;
    } else if (holder.startsWith("AU")) {
    month = 7;
    } else if (holder.startsWith("S")) {
    month = 8;
    } else if (holder.startsWith("O")) {
    month = 9;
    } else if (holder.startsWith("N")) {
    month = 10;
    } else if (holder.startsWith("D")) {
    month = 11;
    }
    } else {
    month = Integer.parseInt(holder) - 1;
    }
    } else {
    holder = tokenizer.nextToken().trim().toUpperCase();
    // check to see if a number was entered
    if (holder != holder.trim().toLowerCase()) {
    if (holder.startsWith("JAN")) {
    month = 0;
    } else if (holder.startsWith("F")) {
    month = 1;
    } else if (holder.startsWith("MAR")) {
    month = 2;
    } else if (holder.startsWith("AP")) {
    month = 3;
    } else if (holder.startsWith("MAY")) {
    month = 4;
    } else if (holder.startsWith("JUN")) {
    month = 5;
    } else if (holder.startsWith("JUL")) {
    month = 6;
    } else if (holder.startsWith("AU")) {
    month = 7;
    } else if (holder.startsWith("S")) {
    month = 8;
    } else if (holder.startsWith("O")) {
    month = 9;
    } else if (holder.startsWith("N")) {
    month = 10;
    } else if (holder.startsWith("D")) {
    month = 11;
    }
    } else {
    month = Integer.parseInt(holder) - 1;
    }
    day = Integer.parseInt(tokenizer.nextToken());
    }
    year = Integer.parseInt(tokenizer.nextToken());
    } catch (Exception e) {
    e.printStackTrace();
    throw new DateFormatException("date unparsable");
    }

    // now the time if needed
    if (time != null) {
    // try to determine the separator string
    if (date.indexOf("-") > 0) {
    sep = "-";
    } else if (date.indexOf(":") > 0) {
    sep = ":";
    } else if (date.indexOf(";") > 0) {
    sep = ";";
    } else if (date.trim().indexOf(" ") > 0) {
    sep = " ";
    } else {
    throw new DateFormatException("unable to determine proper separator character in date string");
    }

    tokenizer = new StringTokenizer(time, sep);
    
    try { 
      hour = Integer.parseInt(tokenizer.nextToken());
    } catch (NumberFormatException nfe) {
      hour = 0;
    } catch (NoSuchElementException nsee) {
    } catch (Exception e) {
      e.printStackTrace();
      throw new DateFormatException("hour field unparsable");
    }
    
    try { 
      min = Integer.parseInt(tokenizer.nextToken());
    } catch (NumberFormatException nfe) {
      min = 0;
    } catch (NoSuchElementException nsee) {
    } catch (Exception e) {
      e.printStackTrace();
      throw new DateFormatException("minute field unparsable");
    }
    
    try { 
      sec = Integer.parseInt(tokenizer.nextToken());
    } catch (NumberFormatException nfe) {
      sec = 0;
    } catch (NoSuchElementException nsee) {
    } catch (Exception e) {
      e.printStackTrace();
      throw new DateFormatException("second field unparsable");
    }
    

    }

    // now put it all together
    calObject.set(year, month, day, hour, min, sec);
    return calObject.getTime();
    }



  •         } else if (holder.startsWith("O")) {
              month = 9;
    

    So... I can write my dates like "23 Oktoberfest 2011" and other such nonsense?



  • Nice... though for some reason Jabuary doesn't work...



  • @zelmak said:

    Also found during this recent spelunk into the code (gotta love brute-force)

    No no, that hasn't even seen brute force. It's just complicated. Here's some brute-force date parsing for you:

    Date parseDate(String dateString)
    {
      int year, month, day;
      for(year=1900; year<2100; ++year)
        for(month=1; month<=12; ++month)
          for(day=1; day<=31; ++day)
          {
            Date date;
            try
            {
              date = new Date(year, month, day);
            }
            catch(InvalidDateException)
            {
              continue;
            }
            if(date.toString()==dateString)
              return date;
          }
      return null;
    }
    


  • @tdb said:

    No no, that hasn't even seen brute force. It's just complicated. Here's some brute-force date parsing for you:

    Date parseDate(String dateString)
    {
      int year, month, day;
      for(year=1900; year<2100; ++year)
        for(month=1; month<=12; ++month)
          for(day=1; day<=31; ++day)
          {
            Date date;
            try
            {
              date = new Date(year, month, day);
            }
            catch(InvalidDateException)
            {
              continue;
            }
            if(date.toString()==dateString)
              return date;
          }
      return null;
    }

     

     +1

     


Log in to reply