Java Code Examples for org.joda.time.DateTime.plusMinutes()

The following are Jave code examples for showing how to use plusMinutes() of the org.joda.time.DateTime class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: asura   File: DateCalculator.java   Source Code and License Vote up 6 votes
/**
 * 计算两个时间段间隔里面按照固定间隔返回时间点集合
 * <p/>
 * 这里会 startDate 到 endDate 最后endDate如果相等会算入集合
 * 如:[2016-10-20 12:00:00.000 -2016-10-20 13:00:00.000,'yyyy-MM-dd HH:mm',30]
 * 返回:["2016-10-20 12:00","2016-10-20 12:30","2016-10-20 13:00"]
 * @param date1
 * @param date2
 * @param minutesSplit 间隔多少分
 * @param pattern
 * @return
 */
public static List<String> getTimePointBetweenDates(@NotNull Date date1, @NotNull Date date2, @NotNull String pattern, int minutesSplit) {
    Objects.requireNonNull(date1, "startDate must not null");
    Objects.requireNonNull(date2, "endDate must not null");
    Objects.requireNonNull(pattern, "pattern must not null");
    Date startDate = date1;
    Date endDate = date2;
    //调整顺序
    if (date1.after(date2)) {
        startDate = date2;
        endDate = date1;
    }
    DateTime startDateTime = new DateTime(startDate);
    DateTime endDateTime = new DateTime(endDate);
    List<String> points = new ArrayList<>();
    while (startDateTime.isBefore(endDateTime.getMillis()) || startDateTime.getMillis() == endDateTime.getMillis()) {
        points.add(DateFormatter.format(startDateTime.getMillis(), pattern));
        startDateTime = startDateTime.plusMinutes(minutesSplit);
    }
    return points;
}
 
Example 2
Project: exam   File: CalendarController.java   Source Code and License Vote up 6 votes
protected Collection<Interval> gatherSuitableSlots(ExamRoom room, LocalDate date, Integer examDuration) {
    Collection<Interval> examSlots = new ArrayList<>();
    // Resolve the opening hours for room and day
    List<ExamRoom.OpeningHours> openingHours = room.getWorkingHoursForDate(date);
    if (!openingHours.isEmpty()) {
        // Get suitable slots based on exam duration
        for (Interval slot : allSlots(openingHours, room, date)) {
            DateTime beginning = slot.getStart();
            DateTime openUntil = getEndOfOpeningHours(beginning, openingHours);
            if (!beginning.plusMinutes(examDuration).isAfter(openUntil)) {
                DateTime end = beginning.plusMinutes(examDuration);
                examSlots.add(new Interval(beginning, end));
            }
        }
    }
    return examSlots;
}
 
Example 3
Project: cakes   File: JodaTimeDemo1.java   Source Code and License Vote up 5 votes
/**
 * 时间加减操作,plus 负数时是向前推移
 */
@Test
public void testTimePlus() {
    // 获取当前时间
    DateTime dt = new DateTime();
    String currentTime = dt.toString("yyyy-MM-dd HH:mm:ss");
    System.out.println("currentTime: " + currentTime);

    // 相对当前时间 向后5天,5天后
    DateTime plus5Days = dt.plusDays(5);
    String plus5DaysStr = plus5Days.toString("yyyy-MM-dd HH:mm:ss");
    System.out.println("plus 5 days: " + plus5DaysStr);

    // 相对当前时间 向后5个小时,5小时后
    DateTime plus5Hours = dt.plusHours(5);
    String plus5HoursStr = plus5Hours.toString("yyyy-MM-dd HH:mm:ss");
    System.out.println("plus 5 hours: " + plus5HoursStr);

    // 相对当前时间,向后5分钟,5分钟后
    DateTime plus5Minutes = dt.plusMinutes(5);
    String plus5MinutesStr = plus5Minutes.toString("yyyy-MM-dd HH:mm:ss");
    System.out.println("plus 5 minutes: " + plus5MinutesStr);

    // 相对当前时间,向前5年,5年前
    DateTime plus5Years = dt.plusYears(-5);
    String plus5YearsStr = plus5Years.toString("yyyy-MM-dd HH:mm:ss");
    System.out.println("5 years ago: " + plus5YearsStr);

    // 相对当前时间,向前5个月
    DateTime plusMonths = dt.plusMonths(-5);
    String plusMonthsStr = plusMonths.toString("yyyy-MM-dd HH:mm:ss");
    System.out.println("5 month ago: " + plusMonthsStr);
}
 
Example 4
Project: exam   File: SessionController.java   Source Code and License Vote up 5 votes
private Reservation getUpcomingExternalReservation(String eppn) {
    DateTime now = AppUtil.adjustDST(new DateTime());
    int lookAheadMinutes = Minutes.minutesBetween(now, now.plusDays(1).withMillisOfDay(0)).getMinutes();
    DateTime future = now.plusMinutes(lookAheadMinutes);
    List<Reservation> reservations = Ebean.find(Reservation.class).where()
            .eq("externalUserRef", eppn)
            .isNotNull("externalRef")
            .le("startAt", future)
            .gt("endAt", now)
            .orderBy("startAt")
            .findList();
    return reservations.isEmpty() ? null : reservations.get(0);
}
 
Example 5
Project: exam   File: SystemRequestHandler.java   Source Code and License Vote up 5 votes
private Optional<ExamEnrolment> getNextEnrolment(Long userId, int minutesToFuture) {
    DateTime now = AppUtil.adjustDST(new DateTime());
    DateTime future = now.plusMinutes(minutesToFuture);
    List<ExamEnrolment> results = Ebean.find(ExamEnrolment.class)
            .fetch("reservation")
            .fetch("reservation.machine")
            .fetch("reservation.machine.room")
            .fetch("exam")
            .fetch("externalExam")
            .where()
            .eq("user.id", userId)
            .disjunction()
            .eq("exam.state", Exam.State.PUBLISHED)
            .eq("exam.state", Exam.State.STUDENT_STARTED)
            .jsonEqualTo("externalExam.content", "state", Exam.State.PUBLISHED.toString())
            .jsonEqualTo("externalExam.content", "state", Exam.State.STUDENT_STARTED.toString())
            .endJunction()
            .le("reservation.startAt", future)
            .gt("reservation.endAt", now)
            .isNotNull("reservation.machine")
            .orderBy("reservation.startAt")
            .findList();
    if (results.isEmpty()) {
        return Optional.empty();
    }
    return Optional.of(results.get(0));
}
 
Example 6
Project: exam   File: ExamAutoSaverActor.java   Source Code and License Vote up 5 votes
private void markEnded(List<ExamParticipation> participations) {
    for (ExamParticipation participation : participations) {
        Exam exam = participation.getExam();
        Reservation reservation = participation.getReservation();
        DateTime reservationStart = new DateTime(reservation.getStartAt());
        DateTime participationTimeLimit = reservationStart.plusMinutes(exam.getDuration());
        DateTime now = AppUtil.adjustDST(DateTime.now(), reservation.getMachine().getRoom());
        if (participationTimeLimit.isBefore(now)) {
            participation.setEnded(now);
            participation.setDuration(
                    new DateTime(participation.getEnded().getMillis() - participation.getStarted().getMillis()));

            GeneralSettings settings = SettingsController.getOrCreateSettings("review_deadline", null, "14");
            int deadlineDays = Integer.parseInt(settings.getValue());
            DateTime deadline = new DateTime(participation.getEnded()).plusDays(deadlineDays);
            participation.setDeadline(deadline);

            participation.save();
            Logger.info("{}: ... setting exam {} state to REVIEW", getClass().getCanonicalName(), exam.getId());
            exam.setState(Exam.State.REVIEW);
            exam.save();
            if (exam.isPrivate()) {
                // Notify teachers
                Set<User> recipients = new HashSet<>();
                recipients.addAll(exam.getParent().getExamOwners());
                recipients.addAll(exam.getExamInspections().stream().map(
                        ExamInspection::getUser).collect(Collectors.toSet()));
                AppUtil.notifyPrivateExamEnded(recipients, exam, composer);
            }
        } else {
            Logger.info("{}: ... exam {} is ongoing until {}", getClass().getCanonicalName(), exam.getId(),
                    participationTimeLimit);
        }
    }
}
 
Example 7
Project: sjk   File: CDNCacheImpl.java   Source Code and License Vote up 5 votes
@Override
public void setCacheScanSoftIfHit(HttpServletResponse resp) {
    DateTime now = new DateTime();

    long keepMinute = now.getMillis() / interval * interval;
    DateTime keepMinuteDateTime = new DateTime(keepMinute);
    int min = keepMinuteDateTime.getMinuteOfHour();
    min = getPrevIntervalMinute(min);
    DateTime lastModified = keepMinuteDateTime.minuteOfHour().setCopy(min);
    resp.setHeader("Last-Modified", toGMT(lastModified));
    DateTime expires = now.plusMinutes(expiresMinuteForScansoftIfHit);
    resp.setHeader("Expires", toGMT(expires));
    resp.setHeader("Cache-Control", maxAgeIfHit);
}
 
Example 8
Project: JuniperBotJ   File: RemindCommand.java   Source Code and License Vote up 4 votes
@Override
public boolean doCommand(MessageReceivedEvent message, BotContext context, String content) throws DiscordException {
    try {
        DateTime date = null;
        String reminder = null;
        Matcher m = PATTERN.matcher(content);
        if (m.find()) {
            date = FORMATTER.parseDateTime(String.format("%s %s", m.group(1), m.group(2)));
            reminder = m.group(3);
            if (DateTime.now().isAfter(date)) {
                messageService.onError(message.getChannel(), "discord.command.remind.error.future");
                return fail(message);
            }
        }

        String keyWord = messageService.getMessage("discord.command.remind.keyWord");
        m = Pattern.compile(String.format(RELATIVE_PATTERN_FORMAT, keyWord)).matcher(content);
        if (m.find()) {
            Long millis = SEQUENCE_PARSER.parse(m.group(1));
            reminder = m.group(2);
            if (millis != null && StringUtils.isNotEmpty(reminder)) {
                date = DateTime.now().plus(millis);
            }
        }

        if (date != null && reminder != null) {
            createReminder(message.getChannel(), message.getMember(), reminder, date.toDate());
            return ok(message, "discord.command.remind.done");
        }
    } catch (IllegalArgumentException e) {
        // fall down
    }

    String prefix = context.getConfig() != null ? context.getConfig().getPrefix() : configService.getDefaultPrefix();

    DateTime current = DateTime.now();
    current = current.plusMinutes(1);
    EmbedBuilder builder = messageService.getBaseEmbed();
    builder.setTitle(messageService.getMessage("discord.command.remind.help.title"));
    builder.addField(
            messageService.getMessage("discord.command.remind.help.field1.title"),
            messageService.getMessage("discord.command.remind.help.field1.value", prefix, FORMATTER.print(current)), false);
    builder.addField(
            messageService.getMessage("discord.command.remind.help.field2.title"),
            messageService.getMessage("discord.command.remind.help.field2.value", prefix), false);
    messageService.sendMessageSilent(message.getChannel()::sendMessage, builder.build());
    return false;
}