コード例 #1
0
  public static String calculateName(DateIntervalType intervalType, Date d) {
    if (d == null) return null;

    Locale l = Locale.getDefault();
    if (MILLENIUM.equals(intervalType)
        || CENTURY.equals(intervalType)
        || DECADE.equals(intervalType)
        || YEAR.equals(intervalType)) {
      SimpleDateFormat format = new SimpleDateFormat("yyyy", l);
      return format.format(d);
    }
    if (QUARTER.equals(intervalType) || MONTH.equals(intervalType)) {
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM", l);
      return format.format(d);
    }
    if (WEEK.equals(intervalType) || DAY.equals(intervalType) || DAY_OF_WEEK.equals(intervalType)) {
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
      return format.format(d);
    }
    if (HOUR.equals(intervalType)) {
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH");
      return format.format(d);
    }
    if (MINUTE.equals(intervalType)) {
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
      return format.format(d);
    }
    if (SECOND.equals(intervalType)) {
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      return format.format(d);
    }
    return format.format(d);
  }
コード例 #2
0
  protected void nextIntervalDate(Calendar c, DateIntervalType intervalType, int intervals) {

    if (MILLENIUM.equals(intervalType)) {
      c.add(Calendar.YEAR, 1000 * intervals);
    } else if (CENTURY.equals(intervalType)) {
      c.add(Calendar.YEAR, 100 * intervals);
    } else if (DECADE.equals(intervalType)) {
      c.add(Calendar.YEAR, 10 * intervals);
    } else if (YEAR.equals(intervalType)) {
      c.add(Calendar.YEAR, intervals);
    } else if (QUARTER.equals(intervalType)) {
      c.add(Calendar.MONTH, 3 * intervals);
    } else if (MONTH.equals(intervalType)) {
      c.add(Calendar.MONTH, intervals);
    } else if (WEEK.equals(intervalType)) {
      c.add(Calendar.DAY_OF_MONTH, 7 * intervals);
    } else if (DAY.equals(intervalType) || DAY_OF_WEEK.equals(intervalType)) {
      c.add(Calendar.DAY_OF_MONTH, intervals);
    } else if (HOUR.equals(intervalType)) {
      c.add(Calendar.HOUR_OF_DAY, intervals);
    } else if (MINUTE.equals(intervalType)) {
      c.add(Calendar.MINUTE, intervals);
    } else if (SECOND.equals(intervalType)) {
      c.add(Calendar.SECOND, intervals);
    } else {
      // Default to year to avoid infinite loops
      c.add(Calendar.YEAR, intervals);
    }
  }