Ejemplo n.º 1
0
  public static Shift getCurrentShift() {
    Calendar calendar = Calendar.getInstance();
    Calendar calendar2 = Calendar.getInstance();
    calendar.clear();

    calendar.set(Calendar.HOUR, calendar2.get(Calendar.HOUR));
    calendar.set(Calendar.MINUTE, calendar2.get(Calendar.MINUTE));
    calendar.set(Calendar.AM_PM, calendar2.get(Calendar.AM_PM));

    Date currentTime = calendar.getTime();

    ShiftDAO shiftDAO = new ShiftDAO();
    List<Shift> shifts = shiftDAO.findAll();

    for (Shift shift : shifts) {
      Date startTime = new Date(shift.getStartTime().getTime());
      Date endTime = new Date(shift.getEndTime().getTime());

      if (currentTime.after(startTime) && currentTime.before(endTime)) {
        return shift;
      }
    }

    calendar.add(Calendar.DATE, 1);
    currentTime = calendar.getTime();
    for (Shift shift : shifts) {
      Date startTime = new Date(shift.getStartTime().getTime());
      Date endTime = new Date(shift.getEndTime().getTime());

      if (currentTime.after(startTime) && currentTime.before(endTime)) {
        return shift;
      }
    }

    return null;
  }
  public void updateView() {
    if (shift == null) {
      return;
    }

    tfShiftName.setText(shift.getName());

    Date startTime = shift.getStartTime();
    Date endTime = shift.getEndTime();

    Calendar c = Calendar.getInstance();
    c.setTime(startTime);

    startHour.setSelectedIndex(c.get(Calendar.HOUR) - 1);
    startMin.setSelectedIndex(c.get(Calendar.MINUTE));
    startAmPm.setSelectedIndex(c.get(Calendar.AM_PM) == Calendar.AM ? 0 : 1);

    c.setTime(endTime);
    endHour.setSelectedIndex(c.get(Calendar.HOUR) - 1);
    endMin.setSelectedIndex(c.get(Calendar.MINUTE));
    endAmPm.setSelectedIndex(c.get(Calendar.AM_PM) == Calendar.AM ? 0 : 1);
  }