コード例 #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);
    }
  }
コード例 #3
0
 protected Calendar firstIntervalDate(
     DateIntervalType intervalType, Date minDate, ColumnGroup columnGroup) {
   Calendar c = GregorianCalendar.getInstance();
   c.setLenient(false);
   c.setTime(minDate);
   if (YEAR.equals(intervalType)) {
     c.set(Calendar.MONTH, 0);
     c.set(Calendar.DAY_OF_MONTH, 1);
     c.set(Calendar.HOUR, 0);
     c.set(Calendar.MINUTE, 0);
     c.set(Calendar.SECOND, 0);
     c.set(Calendar.MILLISECOND, 0);
   }
   if (QUARTER.equals(intervalType)) {
     int currentMonth = c.get(Calendar.MONTH);
     int firstMonthYear = columnGroup.getFirstMonthOfYear().getIndex();
     int rest = Quarter.getPositionInQuarter(firstMonthYear, currentMonth + 1);
     c.add(Calendar.MONTH, rest * -1);
     c.set(Calendar.DAY_OF_MONTH, 1);
     c.set(Calendar.HOUR, 0);
     c.set(Calendar.MINUTE, 0);
     c.set(Calendar.SECOND, 0);
     c.set(Calendar.MILLISECOND, 0);
   }
   if (MONTH.equals(intervalType)) {
     c.set(Calendar.DAY_OF_MONTH, 1);
     c.set(Calendar.HOUR, 0);
     c.set(Calendar.MINUTE, 0);
     c.set(Calendar.SECOND, 0);
     c.set(Calendar.MILLISECOND, 0);
   }
   if (DAY.equals(intervalType) || DAY_OF_WEEK.equals(intervalType) || WEEK.equals(intervalType)) {
     c.set(Calendar.HOUR, 0);
     c.set(Calendar.MINUTE, 0);
     c.set(Calendar.SECOND, 0);
     c.set(Calendar.MILLISECOND, 0);
   }
   if (HOUR.equals(intervalType)) {
     c.set(Calendar.MINUTE, 0);
     c.set(Calendar.SECOND, 0);
     c.set(Calendar.MILLISECOND, 0);
   }
   if (MINUTE.equals(intervalType)) {
     c.set(Calendar.SECOND, 0);
     c.set(Calendar.MILLISECOND, 0);
   }
   if (SECOND.equals(intervalType)) {
     c.set(Calendar.MILLISECOND, 0);
   }
   return c;
 }
コード例 #4
0
ファイル: Date.java プロジェクト: gitter-badger/dungeon
 /**
  * Constructs a date from the provided parameters.
  *
  * @param year the year, positive
  * @param month the month, positive, smaller than or equal to the number of months in a year
  * @param day the day, positive, smaller than or equal to the number of days in a month
  */
 public Date(long year, long month, long day) {
   if (year <= 0) {
     DungeonLogger.warning("Tried to construct Date with nonpositive year.");
     year = 1;
   }
   if (month <= 0) {
     DungeonLogger.warning("Tried to construct Date with nonpositive month.");
     month = 1;
   } else if (month > YEAR.as(MONTH)) {
     DungeonLogger.warning("Tried to construct Date with nonexistent month.");
     month = YEAR.as(MONTH);
   }
   if (day <= 0) {
     DungeonLogger.warning("Tried to construct Date with nonpositive day.");
     day = 1;
   } else if (day > MONTH.as(DAY)) {
     DungeonLogger.warning("Tried to construct Date with nonexistent day.");
     day = MONTH.as(DAY);
   }
   time =
       YEAR.milliseconds * (year - 1)
           + MONTH.milliseconds * (month - 1)
           + DAY.milliseconds * (day - 1);
 }
コード例 #5
0
 public int getMinecraftDuration() {
   switch (this) {
     case DAY:
       return time / 72; // 20 minutes
     case HOUR:
       return time / 144; // 25 seconds
     case MONTH: // Moon phases cycle every 8 days
       return DAY.getMinecraftDuration() * 8;
     case WEEK: // 20 minutes
       return DAY.getMinecraftDuration() * 2;
     case YEAR: // 8 real hours
       return MONTH.getMinecraftDuration() * 6;
     case MINUTE:
     case SECOND:
     case TICK:
     default:
       return time;
   }
 }