Exemple #1
0
 /**
  * 计算两个日期之间的时间间隔(不计算毫秒数)
  *
  * @param date1,date2
  * @param timeType
  * @return int
  */
 public static int periodBtDate(Date date1, Date date2, TimeType timeType) {
   if (date1 == null || date2 == null) {
     throw new IllegalArgumentException("date param can not be null");
   }
   DateTime start = fromDate(date1);
   DateTime end = fromDate(date2);
   int period = 0;
   switch (timeType.getCode()) {
     case "Y":
       period = Years.yearsBetween(start, end).getYears();
       break;
     case "M":
       period = Months.monthsBetween(start, end).getMonths();
       break;
     case "W":
       period = Weeks.weeksBetween(start, end).getWeeks();
       break;
     case "D":
       period = Days.daysBetween(start, end).getDays();
       break;
     case "H":
       period = Hours.hoursBetween(start, end).getHours();
       break;
     case "MIN":
       period = Minutes.minutesBetween(start, end).getMinutes();
       break;
     case "S":
       period = Seconds.secondsBetween(start, end).getSeconds();
       break;
     default:
       break;
   }
   return Math.abs(period);
 }
  /**
   * Tries to create the HMAC-SHA1 hash. If it doesn't match the signature passed in then an
   * AuthenticationException is thrown.
   */
  public static void matchHMACSHA1Signature(HttpServletRequest request, String secretKey)
      throws UnauthorizedException {
    String username = request.getHeader(AuthorizationConstants.USER_ID_HEADER);
    String uri = request.getRequestURI();
    String signature = request.getHeader(AuthorizationConstants.SIGNATURE);
    String date = request.getHeader(AuthorizationConstants.SIGNATURE_TIMESTAMP);

    // Compute the difference between what time this machine thinks it is (in UTC)
    //   vs. the timestamp in the header of the request (also in UTC)
    DateTime timeStamp = new DateTime(date);
    int timeDiff = Minutes.minutesBetween(new DateTime(), timeStamp).getMinutes();

    if (Math.abs(timeDiff) > MAX_TIMESTAMP_DIFF_MIN) {
      throw new UnauthorizedException("Timestamp in request, " + date + ", is out of date");
    }

    String expectedSignature = HMACUtils.generateHMACSHA1Signature(username, uri, date, secretKey);
    if (!expectedSignature.equals(signature)) {
      throw new UnauthorizedException("Invalid digital signature: " + signature);
    }
  }
Exemple #3
0
 /**
  * 求两个直接的日期差,月份.
  *
  * @param tDate
  * @param txDate
  * @param type 选择 DAYS||MONTHS||YEARS||HOURS||MSECONDS||MINUTES||SECONDS
  * @return
  */
 public static int diff(Date tDate, Date txDate, String type) {
   if (txDate != null && tDate != null) {
     DateTime txDT = new DateTime(txDate);
     DateTime tDT = new DateTime(tDate);
     if (type != null) {
       if (type.toUpperCase().endsWith("DAYS") || type.toUpperCase().endsWith("D")) {
         return Days.daysBetween(txDT, tDT).getDays();
       } else if (type.toUpperCase().endsWith("MONTHS")) {
         return Months.monthsBetween(txDT, tDT).getMonths();
       } else if (type.toUpperCase().endsWith("YEARS") || type.toUpperCase().endsWith("Y")) {
         return Years.yearsBetween(txDT, tDT).getYears();
       } else if (type.toUpperCase().endsWith("HOURS") || type.toUpperCase().endsWith("H")) {
         return Hours.hoursBetween(txDT, tDT).getHours();
       } else if (type.toUpperCase().endsWith("MSECONDS")) {
         return Seconds.secondsBetween(txDT, tDT).getSeconds();
       } else if (type.toUpperCase().endsWith("MINUTES")) {
         return Minutes.minutesBetween(txDT, tDT).getMinutes();
       } else if (type.toUpperCase().endsWith("SECONDS") || type.toUpperCase().endsWith("S")) {
         return Seconds.secondsBetween(txDT, tDT).getSeconds();
       }
     }
   }
   return 0;
 }