コード例 #1
0
  /**
   * Grab random holiday from the equivalence class that falls between the two dates
   *
   * @param earliest the earliest date parameter as defined in the model
   * @param latest the latest date parameter as defined in the model
   * @return a holiday that falls between the dates
   */
  public String getRandomHoliday(String earliest, String latest) {
    String dateString = "";
    DateTimeFormatter parser = ISODateTimeFormat.date();
    DateTime earlyDate = parser.parseDateTime(earliest);
    DateTime lateDate = parser.parseDateTime(latest);
    List<Holiday> holidays = new LinkedList<>();

    int min = Integer.parseInt(earlyDate.toString().substring(0, 4));
    int max = Integer.parseInt(lateDate.toString().substring(0, 4));
    int range = max - min + 1;
    int randomYear = (int) (Math.random() * range) + min;

    for (Holiday s : EquivalenceClassTransformer.HOLIDAYS) {
      holidays.add(s);
    }
    Collections.shuffle(holidays);

    for (Holiday holiday : holidays) {
      dateString = convertToReadableDate(holiday.forYear(randomYear));
      if (toDate(dateString).after(toDate(earliest)) && toDate(dateString).before(toDate(latest))) {
        break;
      }
    }
    return dateString;
  }
コード例 #2
0
 /**
  * Checks if the date is a holiday
  *
  * @param dateString the date
  * @return true if it is a holiday, false otherwise
  */
 public boolean isHoliday(String dateString) {
   boolean isHoliday = false;
   for (Holiday date : EquivalenceClassTransformer.HOLIDAYS) {
     if (convertToReadableDate(date.forYear(Integer.parseInt(dateString.substring(0, 4))))
         .equals(dateString)) {
       isHoliday = true;
     }
   }
   return isHoliday;
 }