code: private static long getPeriodBoundary(long timestamp, RateBucketPeriod period) {
          Calendar lastCal = Calendar.getInstance();
          lastCal.setTimeInMillis(timestamp);
          switch (period) {
          case Second:
              lastCal.set(Calendar.MILLISECOND, 0);
              lastCal.add(Calendar.SECOND, 1);
              return lastCal.getTimeInMillis();
          case Minute:
              lastCal.set(Calendar.MILLISECOND, 0);
              lastCal.set(Calendar.SECOND, 0);
              lastCal.add(Calendar.MINUTE, 1);
              return lastCal.getTimeInMillis();
          case Hour:
              lastCal.set(Calendar.MILLISECOND, 0);
              lastCal.set(Calendar.SECOND, 0);
              lastCal.set(Calendar.MINUTE, 0);
              lastCal.add(Calendar.HOUR_OF_DAY, 1);
              return lastCal.getTimeInMillis();
          case Day:
              lastCal.set(Calendar.MILLISECOND, 0);
              lastCal.set(Calendar.SECOND, 0);
              lastCal.set(Calendar.MINUTE, 0);
              lastCal.set(Calendar.HOUR_OF_DAY, 0);
              lastCal.add(Calendar.DAY_OF_YEAR, 1);
              return lastCal.getTimeInMillis();
          case Month:
              lastCal.set(Calendar.MILLISECOND, 0);
              lastCal.set(Calendar.SECOND, 0);
              lastCal.set(Calendar.MINUTE, 0);
              lastCal.set(Calendar.HOUR_OF_DAY, 0);
              lastCal.set(Calendar.DAY_OF_MONTH, 1);
              lastCal.add(Calendar.MONTH, 1);
              return lastCal.getTimeInMillis();
          case Year:
              lastCal.set(Calendar.MILLISECOND, 0);
              lastCal.set(Calendar.SECOND, 0);
              lastCal.set(Calendar.MINUTE, 0);
              lastCal.set(Calendar.HOUR_OF_DAY, 0);
              lastCal.set(Calendar.DAY_OF_YEAR, 1);
              lastCal.add(Calendar.YEAR, 1);
              return lastCal.getTimeInMillis();
          }
          return Long.MAX_VALUE;
      }
docs: Gets the boundary timestamp for the given rate bucket period.  In other words,
  returns the timestamp associated with when the rate period will reset.
  @param timestamp
  @param period