Example #1
0
  static void f2() {
    LocalTime time = LocalTime.of(20, 30);
    int hour = time.getHour(); // 20
    int minute = time.getMinute(); // 30
    time = time.withSecond(6); // 20:30:06
    time = time.plusMinutes(3); // 20:33:06

    System.out.println(hour);
    System.out.println(minute);
    System.out.println(time);
  }
  public LocalTime getTime() {
    try {
      LocalTime t = LocalTime.of(0, 0);
      String k1[] = this.getText().split(":");

      if (k1.length == 3) {
        t = t.plusHours(Integer.parseInt(k1[0]));
        try {
          t = t.plusMinutes(Integer.parseInt(k1[1]));
          try {
            t = t.plusSeconds(Integer.parseInt(k1[2]));
          } catch (NumberFormatException ignored) {

          }
        } catch (NumberFormatException ignored) {

        }
      } else if (k1.length == 2) {
        String k2[] = k1[1].split(",");
        if (k2.length == 2) {
          t = t.plusMinutes(Integer.parseInt(k1[0]));
          try {
            t = t.plusSeconds(Integer.parseInt(k2[0]));
            try {
              t = t.plusNanos(Integer.parseInt(k2[1] + "0000000"));
            } catch (NumberFormatException ignored) {

            }
          } catch (NumberFormatException ignored) {

          }
        }
      }
      return t;
    } catch (Exception e) {
      return null;
    }
  }
  // Calculate How many time
  public List<TestingCenterTimeSlots> generateTimeSlots(Exam exam) {
    TestingCenterInfo tci = tcr.findByTerm(tcr.getCurrentTerm().getTermId());
    int gap = tci.getGap();
    int examDuration = exam.getDuration();
    int openMinutes = (int) ChronoUnit.MINUTES.between(tci.getOpen(), tci.getClose());

    // Ensure time chunk devides 30
    int timeChuck =
        (examDuration + gap) % 30 == 0 ? examDuration : 30 * ((examDuration + gap) / 30 + 1);

    LocalTime endTime = exam.getEndDateTime().toLocalTime();
    LocalTime beginTime = exam.getStartDateTime().toLocalTime();
    LocalTime openTime = tci.getOpen();
    LocalTime closeTime = tci.getClose();
    LocalDate beginDate = exam.getStartDateTime().toLocalDate();
    LocalDate endDate = exam.getEndDateTime().toLocalDate();

    beginTime = adjustTime(beginTime); // Exam Begin Time
    endTime = adjustTime(endTime); // Exam End Time

    // Calculate Duration According to different day.
    int startDayDuration = (int) ChronoUnit.MINUTES.between(beginTime, closeTime);
    int dayLast = (int) ChronoUnit.DAYS.between(beginDate, endDate) - 1;
    int endDayDuration = (int) ChronoUnit.MINUTES.between(openTime, endTime);

    int startDayChucks = startDayDuration / timeChuck;
    int endDayChucks = endDayDuration / timeChuck;
    int regularDayChuncks = dayLast >= 0 ? (dayLast * openMinutes) / timeChuck : 0;
    int dailyChuncks = openMinutes / timeChuck;

    LocalDate dateCursor = beginDate;
    LocalTime timeCursor = beginTime;
    List<TestingCenterTimeSlots> timeSlotses = new ArrayList<>();
    if (startDayChucks > 0) {
      for (int i = 0; i < startDayChucks; i++) {
        LocalDateTime slotsBegin = dateCursor.atTime(timeCursor);
        LocalDateTime slotsEnd = dateCursor.atTime(timeCursor.plusMinutes(examDuration));

        TestingCenterTimeSlots t =
            new TestingCenterTimeSlots(
                exam.getExamId(),
                slotsBegin,
                slotsEnd,
                tci.getNumSeats(),
                tci.getNumSetAsideSeats());
        timeSlotses.add(t);

        timeCursor = timeCursor.plusMinutes(timeChuck);
      }
    }
    if (regularDayChuncks > 0) {

      for (int i = 0; i < dayLast; i++) {
        dateCursor = dateCursor.plusDays(1);
        timeCursor = adjustTime(openTime);

        for (int j = 0; j < dailyChuncks; j++) {
          LocalDateTime slotsBegin = dateCursor.atTime(timeCursor);
          LocalDateTime slotsEnd = dateCursor.atTime(timeCursor.plusMinutes(examDuration));

          TestingCenterTimeSlots t =
              new TestingCenterTimeSlots(
                  exam.getExamId(),
                  slotsBegin,
                  slotsEnd,
                  tci.getNumSeats(),
                  tci.getNumSetAsideSeats());
          timeSlotses.add(t);

          timeCursor = timeCursor.plusMinutes(timeChuck);
        }
      }
    }
    if (endDayChucks > 0) {
      dateCursor = dateCursor.plusDays(1);
      timeCursor = adjustTime(openTime);

      for (int i = 0; i < endDayChucks; i++) {
        LocalDateTime slotsBegin = dateCursor.atTime(timeCursor);
        LocalDateTime slotsEnd = dateCursor.atTime(timeCursor.plusMinutes(examDuration));

        TestingCenterTimeSlots t =
            new TestingCenterTimeSlots(
                exam.getExamId(),
                slotsBegin,
                slotsEnd,
                tci.getNumSeats(),
                tci.getNumSetAsideSeats());
        timeSlotses.add(t);

        timeCursor = timeCursor.plusMinutes(timeChuck);
      }
    }
    return timeSlotses;
  }