public void insertSessions(String eventId, LocalDate date) { LocalDateTime dateTime = date.toLocalDateTime(LocalTime.MIDNIGHT); dateTime = dateTime.plusHours(9); Session session1 = new Session(); session1.setEventId(eventId); session1.setTitle("Session 1"); session1.setTimeslot(new Interval(dateTime.toDateTime(), Minutes.minutes(60))); session1.setRoom(room1); sessionDao.saveSession(session1); dateTime = dateTime.plusHours(1); Session session2 = new Session(); session2.setEventId(eventId); session2.setTitle("Session 2"); session2.setTimeslot(new Interval(dateTime.toDateTime(), Minutes.minutes(60))); session2.setRoom(room2); sessionDao.saveSession(session2); dateTime = dateTime.plusHours(1); Session session3 = new Session(); session3.setEventId(eventId); session3.setTitle("Session 3"); session3.setTimeslot(new Interval(dateTime.toDateTime(), Minutes.minutes(60))); session3.setRoom(room3); sessionDao.saveSession(session3); }
/** * Defines the magnitudes that can be added to the timestamp * * @param token token of form "[number][magnitude]" (ex. "1d") * @return integer indicating the magnitude of the date for the calendar system */ public static ReadablePeriod getTimePeriod(String token) { String valString = token.substring(0, token.length() - 1); int value = Integer.parseInt(valString); char mag = token.charAt(token.length() - 1); ReadablePeriod period; switch (mag) { case 's': period = Seconds.seconds(value); break; case 'm': period = Minutes.minutes(value); break; case 'h': period = Hours.hours(value); break; case 'd': period = Days.days(value); break; case 'M': period = Months.months(value); break; case 'y': period = Years.years(value); break; default: logger.warn("Invalid date magnitude: {}. Defaulting to seconds.", mag); period = Seconds.seconds(value); break; } return period; }
/** * 计算两个日期之间的时间间隔(不计算毫秒数) * * @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); } }
@Test(timeout = 1000L) public void shouldNotLoopForever() throws Exception { // given final MinutesHolder minutesHolder = new MinutesHolder(); // when minutesHolder.addObserver( new Observer() { @Override public void update(Observable observable, Object data) { Minutes minutes = (Minutes) data; assertThat(minutesHolder, sameInstance(observable)); observable.deleteObserver(this); // any method that would trigger the observer minutesHolder.setMinutes(minutes); observable.addObserver(this); } }); minutesHolder.setMinutes(Minutes.minutes(1)); // then assertThat(minutesHolder.getMinutes().getMinutes(), equalTo(1)); // no StackOverflowError }
/** * 求两个直接的日期差,月份. * * @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; }