Beispiel #1
1
 private static long toMillis(
     int year, int month, int day, int hour, int min, int sec, int millis, TimeZone timeZone) {
   checkMonth(month);
   Calendar c = new GregorianCalendar(timeZone);
   c.set(year, month - 1, day, hour, min, sec);
   c.set(Calendar.MILLISECOND, millis);
   return c.getTime().getTime();
 }
  private static boolean keepCurrent(Calendar asOfDate) {
    boolean keepCurrent = false;

    int monthNow = asOfDate.get(Calendar.MONTH);

    // March, June, September, and December are expiration months
    boolean isExpirationMonth = ((monthNow + 1) % 3 == 0);

    if (isExpirationMonth) {
      Calendar volumeShiftDate = (Calendar) asOfDate.clone();

      // Find first Friday
      volumeShiftDate.set(Calendar.DAY_OF_MONTH, 1);
      while (volumeShiftDate.get(Calendar.DAY_OF_WEEK) != Calendar.FRIDAY) {
        volumeShiftDate.add(Calendar.DAY_OF_MONTH, 1);
      }

      // Shift to third Friday
      volumeShiftDate.add(Calendar.WEEK_OF_MONTH, 2);

      // Finally, find the day before second Friday
      volumeShiftDate.add(Calendar.DAY_OF_MONTH, -8);

      if (asOfDate.before(volumeShiftDate)) {
        keepCurrent = true;
      }
    }

    return keepCurrent;
  }
Beispiel #3
1
  void dateTest() throws RuntimeException {
    Locale locTH = new Locale("th", "TH", "TH");
    TimeZone tz = TimeZone.getTimeZone("PST");

    Calendar calGregorian = Calendar.getInstance(tz, Locale.US);
    calGregorian.clear();
    calGregorian.set(2002, 4, 1, 8, 30);
    final Date date = calGregorian.getTime();
    Calendar cal = Calendar.getInstance(tz, locTH);
    cal.clear();
    cal.setTime(date);

    final String strExpected =
        "\u0E27\u0E31\u0E19\u0E1E\u0E38\u0E18\u0E17\u0E35\u0E48\u0020\u0E51\u0020\u0E1E\u0E24\u0E29\u0E20\u0E32\u0E04\u0E21\u0020\u0E1E\u002E\u0E28\u002E\u0020\u0E52\u0E55\u0E54\u0E55\u002C\u0020\u0E58\u0020\u0E19\u0E32\u0E2C\u0E34\u0E01\u0E32\u0020\u0E53\u0E50\u0020\u0E19\u0E32\u0E17\u0E35\u0020\u0E50\u0E50\u0020\u0E27\u0E34\u0E19\u0E32\u0E17\u0E35";
    Date value = cal.getTime();

    // th_TH_TH test
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locTH);
    df.setTimeZone(tz);
    String str = df.format(value);

    if (!strExpected.equals(str)) {
      throw new RuntimeException();
    }
  }
Beispiel #4
1
 private static Date sampleDate() {
   Calendar calendar = Calendar.getInstance();
   // Thursday 2008-04-24 7:10:45pm
   // Chose a Thursday because 2008 starts on a Tuesday - it makes weeks
   // interesting.
   calendar.set(2008, 3 /* 0-based! */, 24, 19, 10, 45);
   return calendar.getTime();
 }
Beispiel #5
1
  /**
   * Finds the next occurrence for monthly recurrence.
   *
   * @param startDate the start date of the previous calendar item.
   * @param endDate the end date of the previous calendar item.
   * @param lastDay if <tt>true</tt> we are interested in last day of the month
   * @return the next item
   */
  public CalendarItemTimerTask nextMonth(Date startDate, Date endDate, boolean lastDay) {
    long duration = sourceTask.getEndDate().getTime() - sourceTask.getStartDate().getTime();
    Calendar cal = Calendar.getInstance();
    cal.setTime(startDate);
    cal = incrementMonths(cal, lastDay, period);
    Date currentDate = new Date();
    if (cal.getTimeInMillis() + duration < currentDate.getTime()) {
      Calendar cal2 = Calendar.getInstance();
      cal2.setTime(currentDate);
      int years = cal2.get(Calendar.YEAR) - cal.get(Calendar.YEAR);
      int months = (years * 12) + (cal2.get(Calendar.MONTH) - cal.get(Calendar.MONTH));
      int monthsToAdd = months;
      monthsToAdd -= months % period;
      cal = incrementMonths(cal, lastDay, monthsToAdd);
      if (cal.getTimeInMillis() + duration < currentDate.getTime()) {
        cal = incrementMonths(cal, lastDay, period);
      }
    }

    Calendar cal2 = (Calendar) cal.clone();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    while (deletedInstances.contains(cal.getTime())) {
      cal = incrementMonths(cal, lastDay, period);
      cal2 = incrementMonths(cal2, lastDay, period);
    }

    startDate = cal2.getTime();
    endDate = new Date(startDate.getTime() + duration);
    if (dateOutOfRange(endDate)) {
      return null;
    }
    boolean executeNow = false;
    if (startDate.before(currentDate)) {
      executeNow = true;
    }

    return new CalendarItemTimerTask(
        sourceTask.getStatus(), startDate, endDate, sourceTask.getId(), executeNow, this);
  }
  /**
   * For the contracts whose volume shifts from the front contract to the back contract on the day
   * preceding the 2nd Friday of expiration month of the front contract.
   */
  public static String getMostLiquid(Calendar asOfDate) {

    int monthNow = asOfDate.get(Calendar.MONTH);
    int yearNow = asOfDate.get(Calendar.YEAR);
    int mostLiquidYear = yearNow;

    int mostLiquidMonth = frontMonths.get(monthNow);

    // special case with December
    if (monthNow == Calendar.DECEMBER) {
      mostLiquidYear = yearNow + 1;
    }

    if (keepCurrent(asOfDate)) {
      mostLiquidMonth = monthNow;
      mostLiquidYear = yearNow;
    }

    Calendar mostLiquidDate = Calendar.getInstance();
    mostLiquidDate.set(Calendar.DAY_OF_MONTH, 1);
    mostLiquidDate.set(Calendar.MONTH, mostLiquidMonth);
    mostLiquidDate.set(Calendar.YEAR, mostLiquidYear);

    SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
    return df.format(mostLiquidDate.getTime());
  }
Beispiel #7
0
  /**
   * Finds the occurrence of the events in the next months
   *
   * @param cal the calendar object
   * @param lastDay if <tt>true</tt> it will return the last day of the month
   * @param period the number of months to add
   * @return the calendar object with set date
   */
  private Calendar incrementMonths(Calendar cal, boolean lastDay, int period) {
    int dayOfMonth = patternSpecific1;
    cal.set(Calendar.DAY_OF_MONTH, 1);
    cal.add(Calendar.MONTH, period);
    if (lastDay || (cal.getActualMaximum(Calendar.DAY_OF_MONTH) < dayOfMonth))
      dayOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

    cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    return cal;
  }
 public void actionPerformed(ActionEvent a) {
   int monthNum = month.getSelectedIndex();
   int dayNum = Integer.parseInt(day.getText());
   int yearNum = Integer.parseInt(year.getText());
   Calendar c = Calendar.getInstance();
   c.set(Calendar.MONTH, monthNum);
   c.set(Calendar.DAY_OF_MONTH, monthNum);
   c.set(Calendar.YEAR, yearNum);
   Date date = c.getTime();
   String dayOfWeek = (new SimpleDateFormat("EEE")).format(date);
   outputLabel.setText(dayOfWeek);
 }
Beispiel #9
0
  /**
   * Gets the value of this datetime as a milliseconds value for {@link java.sql.Date}.
   *
   * @param zone time zone in which to generate a time value for
   */
  public long getJdbcDate(TimeZone zone) {
    Calendar cal = getCalendar(DateTimeUtil.gmtZone);
    cal.setTimeInMillis(getDateValue());

    int year = cal.get(Calendar.YEAR);
    int doy = cal.get(Calendar.DAY_OF_YEAR);

    cal.clear();
    cal.setTimeZone(zone);
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.DAY_OF_YEAR, doy);
    return cal.getTimeInMillis();
  }
Beispiel #10
0
 /**
  * Checks whether the given date is in the recurrent pattern range or not
  *
  * @param date the date
  * @return <tt>true</tt> if the date is in the pattern range.
  */
 private boolean dateOutOfRange(Date date) {
   Calendar cal = Calendar.getInstance();
   cal.setTime(date);
   cal.set(Calendar.HOUR_OF_DAY, 0);
   cal.set(Calendar.MINUTE, 0);
   cal.set(Calendar.SECOND, 0);
   if ((endType != 0x00002023)
       && (endType != 0xFFFFFFFF)
       && cal.getTime().after(windowsTimeToDateObject(this.endDate))) {
     return true; // the series are finished
   }
   return false;
 }
Beispiel #11
0
  /**
   * Finds the occurrence of the events in the next months
   *
   * @param startDate the start date if the calendar item
   * @param dayOfWeekInMonth the number of week days occurrences
   * @return the date of the next occurrence
   */
  private Date getMonthNStartDate(Date startDate, int dayOfWeekInMonth) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(startDate);

    if (dayOfWeekInMonth == -1) {
      Date result = null;
      cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, dayOfWeekInMonth);
      for (int day : allowedDaysOfWeek) {
        cal.set(Calendar.DAY_OF_WEEK, day);
        if (result == null || result.before(cal.getTime())) result = cal.getTime();
      }
      return result;
    } else
      while (dayOfWeekInMonth > 0) {
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (allowedDaysOfWeek.contains(dayOfWeek)) dayOfWeekInMonth--;
        if (dayOfWeekInMonth > 0) cal.add(Calendar.DAY_OF_MONTH, 1);
      }
    return cal.getTime();
  }
Beispiel #12
0
  /**
   * Gets the value of this datetime as a milliseconds value for {@link java.sql.Timestamp}.
   *
   * @param zone time zone in which to generate a time value for
   */
  public long getJdbcTimestamp(TimeZone zone) {
    Calendar cal = getCalendar(DateTimeUtil.gmtZone);
    cal.setTimeInMillis(internalTime);

    int year = cal.get(Calendar.YEAR);
    int doy = cal.get(Calendar.DAY_OF_YEAR);
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    int minute = cal.get(Calendar.MINUTE);
    int second = cal.get(Calendar.SECOND);
    int millis = cal.get(Calendar.MILLISECOND);

    cal.clear();
    cal.setTimeZone(zone);
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.DAY_OF_YEAR, doy);
    cal.set(Calendar.HOUR_OF_DAY, hour);
    cal.set(Calendar.MINUTE, minute);
    cal.set(Calendar.SECOND, second);
    cal.set(Calendar.MILLISECOND, millis);
    return cal.getTimeInMillis();
  }
Beispiel #13
0
  static Date parseDate(String s) {
    Calendar c = new GregorianCalendar(TimeZone.getTimeZone("UTC"));

    int year = Integer.parseInt(s.substring(0, 4));
    int month = Integer.parseInt(s.substring(4, 6)) - 1;
    int date = Integer.parseInt(s.substring(6, 8));
    int hour = Integer.parseInt(s.substring(8, 10));
    int minute = Integer.parseInt(s.substring(10, 12));
    int second = Integer.parseInt(s.substring(12, 14));
    c.set(year, month, date, hour, minute, second);

    return c.getTime();
  }
Beispiel #14
0
  /** Typical main method ("main program") declaration */
  public static void main(String[] av) {

    Locale l1 = new Locale("en", "US"), l2 = new Locale("es", "ES");

    // Create a Date object for May 5, 1986
    Calendar c = Calendar.getInstance();
    c.set(1986, 04, 05); // May 5, 1986
    Date d1 = c.getTime();

    // Create a Date object for today.
    Date d2 = new Date(); // today

    DateFormat df_us = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, l1),
        df_sp = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, l2);
    System.out.println("Date d1 for US is " + df_us.format(d1));
    System.out.println("Date d1 for Spain is " + df_sp.format(d1));
    System.out.println("Date d2 is " + df_us.format(d2));
  }
Beispiel #15
0
  public void testDateAdd() {
    assertEquals("2008/04/24 19:10:45", SAMPLE_DATE);

    // 2008-02-01 0:00:00
    Calendar calendar = Calendar.getInstance();

    calendar.set(2007, 1 /* 0-based! */, 1, 0, 0, 0);
    final Date feb2007 = calendar.getTime();
    assertEquals("2007/02/01 00:00:00", feb2007);

    assertEquals("2008/04/24 19:10:45", Vba.dateAdd("yyyy", 0, SAMPLE_DATE));
    assertEquals("2009/04/24 19:10:45", Vba.dateAdd("yyyy", 1, SAMPLE_DATE));
    assertEquals("2006/04/24 19:10:45", Vba.dateAdd("yyyy", -2, SAMPLE_DATE));
    // partial years interpolate
    final Date sampleDatePlusTwoPointFiveYears = Vba.dateAdd("yyyy", 2.5, SAMPLE_DATE);
    if (isPST) {
      // Only run test in PST, because test would produce different
      // results if start and end are not both in daylight savings time.
      final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.US);
      final String dateString = dateFormat.format(sampleDatePlusTwoPointFiveYears);
      // We allow "2010/10/24 07:10:45" for computers that have an out of
      // date timezone database. 2010/10/24 is in daylight savings time,
      // but was not according to the old rules.
      assertTrue(
          "Got " + dateString,
          dateString.equals("2010/10/24 06:40:45") || dateString.equals("2010/10/24 07:10:45"));
    }
    assertEquals("2009/01/24 19:10:45", Vba.dateAdd("q", 3, SAMPLE_DATE));

    // partial months are interesting!
    assertEquals("2008/06/24 19:10:45", Vba.dateAdd("m", 2, SAMPLE_DATE));
    assertEquals("2007/01/01 00:00:00", Vba.dateAdd("m", -1, feb2007));
    assertEquals("2007/03/01 00:00:00", Vba.dateAdd("m", 1, feb2007));
    assertEquals("2007/02/08 00:00:00", Vba.dateAdd("m", .25, feb2007));
    // feb 2008 is a leap month, so a quarter month is 7.25 days
    assertEquals("2008/02/08 06:00:00", Vba.dateAdd("m", 12.25, feb2007));

    assertEquals("2008/05/01 19:10:45", Vba.dateAdd("y", 7, SAMPLE_DATE));
    assertEquals("2008/05/02 01:10:45", Vba.dateAdd("y", 7.25, SAMPLE_DATE));
    assertEquals("2008/04/24 23:10:45", Vba.dateAdd("h", 4, SAMPLE_DATE));
    assertEquals("2008/04/24 20:00:45", Vba.dateAdd("n", 50, SAMPLE_DATE));
    assertEquals("2008/04/24 19:10:36", Vba.dateAdd("s", -9, SAMPLE_DATE));
  }
  /**
   * Parses a formatted time string into a Date.
   *
   * @param s The string, in the form YYYYMMDDHHMMSS.
   * @return The Date object.
   * @throws TextParseExcetption The string was invalid.
   */
  public static Date parse(String s) throws TextParseException {
    if (s.length() != 14) {
      throw new TextParseException("Invalid time encoding: " + s);
    }

    Calendar c = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    c.clear();
    try {
      int year = Integer.parseInt(s.substring(0, 4));
      int month = Integer.parseInt(s.substring(4, 6)) - 1;
      int date = Integer.parseInt(s.substring(6, 8));
      int hour = Integer.parseInt(s.substring(8, 10));
      int minute = Integer.parseInt(s.substring(10, 12));
      int second = Integer.parseInt(s.substring(12, 14));
      c.set(year, month, date, hour, minute, second);
    } catch (NumberFormatException e) {
      throw new TextParseException("Invalid time encoding: " + s);
    }
    return c.getTime();
  }
Beispiel #17
0
  /**
   * Finds the next occurrence for monthly Nth recurrence.
   *
   * @param startDate the start date of the previous calendar item.
   * @param endDate the end date of the previous calendar item.
   * @return the next item
   */
  public CalendarItemTimerTask nextMonthN(Date startDate, Date endDate) {
    int dayOfWeekInMonth = (patternSpecific2 == 5 ? -1 : patternSpecific2);
    long duration = sourceTask.getEndDate().getTime() - sourceTask.getStartDate().getTime();
    Calendar cal = Calendar.getInstance();
    cal.setTime(startDate);
    cal.set(Calendar.DAY_OF_MONTH, 1);
    cal.add(Calendar.MONTH, period);
    cal.setTime(getMonthNStartDate(cal.getTime(), dayOfWeekInMonth));
    Date currentDate = new Date();
    if (cal.getTimeInMillis() + duration < currentDate.getTime()) {
      Calendar cal2 = Calendar.getInstance();
      cal2.setTime(currentDate);
      int years = cal2.get(Calendar.YEAR) - cal.get(Calendar.YEAR);
      int months = (years * 12) + (cal2.get(Calendar.MONTH) - cal.get(Calendar.MONTH));
      int monthsToAdd = months;
      monthsToAdd -= months % period;
      cal.set(Calendar.DAY_OF_MONTH, 1);
      cal.add(Calendar.MONTH, monthsToAdd);
      cal.setTime(getMonthNStartDate(cal.getTime(), dayOfWeekInMonth));
      if (cal.getTimeInMillis() + duration < currentDate.getTime()) {
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.add(Calendar.MONTH, monthsToAdd);
        cal.setTime(getMonthNStartDate(cal.getTime(), dayOfWeekInMonth));
      }
    }

    Calendar cal2 = (Calendar) cal.clone();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    while (deletedInstances.contains(cal.getTime())) {
      cal.set(Calendar.DAY_OF_MONTH, 1);
      cal.add(Calendar.MONTH, period);
      startDate = null;
      for (int dayOfWeek : allowedDaysOfWeek) {
        cal.set(Calendar.DAY_OF_WEEK, dayOfWeek);
        cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, dayOfWeekInMonth);
        if ((cal.after(startDate) && dayOfWeekInMonth == -1)
            || (cal.before(startDate) && dayOfWeekInMonth != -1)
            || startDate == null) {
          startDate = cal.getTime();
          cal2.set(Calendar.YEAR, cal.get(Calendar.YEAR));
          cal2.set(Calendar.MONTH, cal.get(Calendar.MONTH));
          cal2.set(Calendar.DATE, cal.get(Calendar.DATE));
        }
      }
    }

    startDate = cal2.getTime();
    endDate = new Date(startDate.getTime() + duration);

    if (dateOutOfRange(endDate)) return null;

    boolean executeNow = false;
    if (startDate.before(currentDate)) {
      executeNow = true;
    }

    return new CalendarItemTimerTask(
        sourceTask.getStatus(), startDate, endDate, sourceTask.getId(), executeNow, this);
  }
Beispiel #18
0
  /**
   * Calculates and creates the next calendar item.
   *
   * @param previousStartDate the start date of the previous occurrence.
   * @param previousEndDate the end date of the previous occurrence.
   * @return the new calendar item or null if there are no more calendar items from that recurrent
   *     series.
   */
  public CalendarItemTimerTask next(Date previousStartDate, Date previousEndDate) {
    if (dateOutOfRange(new Date())) {
      return null;
    }
    Date startDate = previousStartDate;
    Date endDate = null;
    boolean executeNow = false;
    long duration = sourceTask.getEndDate().getTime() - sourceTask.getStartDate().getTime();
    switch (patternType) {
      case Day:
        {
          startDate = new Date(startDate.getTime() + period * 60000);
          endDate = new Date(previousEndDate.getTime() + period * 60000);
          Date currentDate = new Date();
          if (endDate.before(currentDate)) {
            long offset = currentDate.getTime() - endDate.getTime();
            offset -= offset % (period * 60000);
            if (endDate.getTime() + offset < currentDate.getTime()) {
              offset += period * 60000;
            }

            startDate = new Date(startDate.getTime() + offset);
          }

          Calendar cal = Calendar.getInstance();
          cal.setTime(startDate);
          Calendar cal2 = (Calendar) cal.clone();
          cal.set(Calendar.HOUR_OF_DAY, 0);
          cal.set(Calendar.MINUTE, 0);
          cal.set(Calendar.SECOND, 0);
          cal.set(Calendar.MILLISECOND, 0);
          while (deletedInstances.contains(cal.getTime())) {
            cal.add(Calendar.MINUTE, period);
            cal2.add(Calendar.MINUTE, period);
          }

          if (dateOutOfRange(cal.getTime())) {
            return null;
          }
          startDate = cal2.getTime();
          endDate = new Date(startDate.getTime() + duration);
          if (startDate.before(currentDate)) {
            executeNow = true;
          }

          return new CalendarItemTimerTask(
              sourceTask.getStatus(), startDate, endDate, sourceTask.getId(), executeNow, this);
        }
      case Week:
        {
          Calendar cal = Calendar.getInstance();
          /** The enum for the firstDow field is the same as Calendar day of week enum + 1 day */
          cal.setFirstDayOfWeek(firstDow + 1);
          cal.setTime(startDate);
          int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
          int index = allowedDaysOfWeek.indexOf(dayOfWeek);
          if (++index < allowedDaysOfWeek.size()) {
            cal.set(Calendar.DAY_OF_WEEK, allowedDaysOfWeek.get(index));
            startDate = cal.getTime();
            endDate = new Date(startDate.getTime() + duration);
          } else {
            cal.set(Calendar.DAY_OF_WEEK, allowedDaysOfWeek.get(0));
            cal.add(Calendar.WEEK_OF_YEAR, period);
            startDate = cal.getTime();
            endDate = new Date(startDate.getTime() + duration);
          }
          Date currentDate = new Date();
          if (endDate.before(currentDate)) {
            cal.set(Calendar.DAY_OF_WEEK, allowedDaysOfWeek.get(0));
            endDate = new Date(cal.getTimeInMillis() + duration);
            long offset = (currentDate.getTime() - endDate.getTime());

            // 1 week = 604800000 is milliseconds
            offset -= offset % (period * 604800000);
            if (endDate.getTime() + offset < currentDate.getTime()) {
              cal.add(Calendar.WEEK_OF_YEAR, (int) (offset / (period * 604800000)));
              int i = 1;
              while (((cal.getTimeInMillis() + duration) < (currentDate.getTime()))) {
                if (i == allowedDaysOfWeek.size()) {
                  cal.add(Calendar.WEEK_OF_YEAR, period);
                  i = 0;
                }
                cal.set(Calendar.DAY_OF_WEEK, allowedDaysOfWeek.get(i));
                i++;
              }

              startDate = cal.getTime();
            } else {
              startDate = new Date(cal.getTimeInMillis() + offset);
            }
          }

          cal.setTime(startDate);
          Calendar cal2 = (Calendar) cal.clone();
          cal.set(Calendar.HOUR_OF_DAY, 0);
          cal.set(Calendar.MINUTE, 0);
          cal.set(Calendar.SECOND, 0);
          cal.set(Calendar.MILLISECOND, 0);
          dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
          index = allowedDaysOfWeek.indexOf(dayOfWeek) + 1;
          while (deletedInstances.contains(cal.getTime())) {
            if (index >= allowedDaysOfWeek.size()) {
              index = 0;
              cal.add(Calendar.WEEK_OF_YEAR, period);
              cal2.add(Calendar.WEEK_OF_YEAR, period);
            }
            cal.set(Calendar.DAY_OF_WEEK, allowedDaysOfWeek.get(index));
            cal2.set(Calendar.DAY_OF_WEEK, allowedDaysOfWeek.get(index));
            index++;
          }
          startDate = cal2.getTime();
          endDate = new Date(startDate.getTime() + duration);
          if (dateOutOfRange(endDate)) return null;
          if (startDate.before(currentDate)) {
            executeNow = true;
          }

          return new CalendarItemTimerTask(
              sourceTask.getStatus(), startDate, endDate, sourceTask.getId(), executeNow, this);
        }
      case Month:
      case MonthEnd:
      case HjMonth:
      case HjMonthEnd:
        {
          return nextMonth(startDate, endDate, false);
        }
      case MonthNth:
      case HjMonthNth:
        {
          if (patternSpecific1 == 0x7f && patternSpecific2 == 0x05) {
            return nextMonth(startDate, endDate, true);
          }

          return nextMonthN(startDate, endDate);
        }
    }
    return null;
  }
Beispiel #19
0
  private void saveExcelPoject(File file) throws IOException {
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet("timeplan");
    // Заголовок в 0 строке
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);
    cell.setCellValue("Филиал");
    cell = row.createCell(1);
    cell.setCellValue("Город");
    Calendar cal = Calendar.getInstance();
    cal.set(2017, 0, 5); // Начальная дата проекта
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yy");
    for (int i = 0; i < 3 * 52; i++) { // Счетчик по неделям
      cell = row.createCell(i + 2);
      cell.setCellValue(sdf.format(cal.getTime()));
      cal.add(Calendar.WEEK_OF_YEAR, 1); // Следующая неделя
    }

    // sheet.setColumnWidth(0, 256);

    // Цвета ячеек
    CellStyle[] styles = new CellStyle[6];
    styles[0] = wb.createCellStyle();
    styles[0].setFillForegroundColor(HSSFColor.RED.index);
    styles[0].setFillPattern(FillPatternType.SOLID_FOREGROUND);
    styles[1] = wb.createCellStyle();
    styles[1].setFillForegroundColor(HSSFColor.GREEN.index);
    styles[1].setFillPattern(FillPatternType.SOLID_FOREGROUND);
    styles[2] = wb.createCellStyle();
    styles[2].setFillForegroundColor(HSSFColor.BLUE.index);
    styles[2].setFillPattern(FillPatternType.SOLID_FOREGROUND);
    styles[3] = wb.createCellStyle();
    styles[3].setFillForegroundColor(HSSFColor.ROSE.index);
    styles[3].setFillPattern(FillPatternType.SOLID_FOREGROUND);
    styles[4] = wb.createCellStyle();
    styles[4].setFillForegroundColor(HSSFColor.LIGHT_BLUE.index);
    styles[4].setFillPattern(FillPatternType.SOLID_FOREGROUND);
    styles[5] = wb.createCellStyle();
    styles[5].setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
    styles[5].setFillPattern(FillPatternType.SOLID_FOREGROUND);

    short rowIdx = 0;
    for (Region region : this.regions) {
      row = sheet.createRow(++rowIdx);
      cell = row.createCell(0);
      cell.setCellValue(region.filial);
      cell = row.createCell(1);
      cell.setCellValue(region.name);

      cal = Calendar.getInstance();
      cal.set(2017, 0, 5); // Начальная дата проекта
      for (int i = 0; i < 3 * 52; i++) { // Счетчик по неделям
        short color = region.getDateColorIndex(cal.getTime());
        if (color >= 0) {
          cell = row.createCell(i + 2);
          cell.setCellStyle(styles[color]);
        }

        cal.add(Calendar.WEEK_OF_YEAR, 1); // Следующая неделя
      }
    }

    try (FileOutputStream fileOut = new FileOutputStream(file)) {
      wb.write(fileOut);
    }
  }
Beispiel #20
0
  /** Decode the value from a string. */
  public void decodeVal(String val) throws Exception {
    char[] c = val.toCharArray();
    try {
      int i = 0;

      int year =
          (int) (c[i++] - '0') * 1000
              + (int) (c[i++] - '0') * 100
              + (int) (c[i++] - '0') * 10
              + (int) (c[i++] - '0') * 1;

      if (c[i++] != '-') throw new Exception();

      int mon = (int) (c[i++] - '0') * 10 + (int) (c[i++] - '0') * 1;

      if (c[i++] != '-') throw new Exception();

      int day = (int) (c[i++] - '0') * 10 + (int) (c[i++] - '0') * 1;

      if (c[i++] != 'T') throw new Exception();

      int hour = (int) (c[i++] - '0') * 10 + (int) (c[i++] - '0') * 1;

      if (c[i++] != ':') throw new Exception();

      int min = (int) (c[i++] - '0') * 10 + (int) (c[i++] - '0') * 1;

      if (c[i++] != ':') throw new Exception();

      int sec = (int) (c[i++] - '0') * 10 + (int) (c[i++] - '0') * 1;

      int ms = 0;
      if (c[i] == '.') {
        i++;
        ms = (c[i++] - '0') * 100;
        if ('0' <= c[i] && c[i] <= '9') ms += (c[i++] - '0') * 10;
        if ('0' <= c[i] && c[i] <= '9') ms += (c[i++] - '0') * 1;

        // skip any additional fractional digits
        while (i < c.length && '0' <= c[i] && c[i] <= '9') i++;
      }

      // timezone offset sign
      int tzOff = 0;
      char sign = c[i++];
      if (sign != 'Z') {
        if (sign != '+' && sign != '-') throw new Exception();

        // timezone hours
        int hrOff = (int) (c[i++] - '0');
        if (i < c.length && c[i] != ':') hrOff = hrOff * 10 + (int) (c[i++] - '0');

        // timezone minutes
        int minOff = 0;
        if (i < c.length) {
          if (c[i++] != ':') throw new Exception();
          minOff = 10 * (int) (c[i++] - '0') + (int) (c[i++] - '0');
        }

        tzOff = hrOff * (60 * 60 * 1000) + minOff * (60 * 1000);
        if (sign == '-') tzOff *= -1;
      }

      Calendar cal = new GregorianCalendar(year, mon - 1, day, hour, min, sec);
      cal.set(Calendar.MILLISECOND, ms);
      cal.setTimeZone(new SimpleTimeZone(tzOff, "Offset"));

      // save
      set(cal.getTime().getTime(), timeZone);
    } catch (Exception e) {
      throw new Exception("Invalid abstime: " + val);
    }
  }
Beispiel #21
0
  public static void main(String[] args) {
    Calendar fromCal = Calendar.getInstance();
    Calendar toCal = Calendar.getInstance();

    fromCal.set(2010, 0, 1);
    toCal.set(2010, 0, 1);
    printResult(fromCal, toCal);

    fromCal.set(2010, 0, 21);
    toCal.set(2010, 0, 21);
    printResult(fromCal, toCal);

    fromCal.set(2010, 0, 1);
    toCal.set(2010, 2, 1);
    printResult(fromCal, toCal);

    fromCal.set(2010, 0, 1);
    toCal.set(2010, 2, 23);
    printResult(fromCal, toCal);

    fromCal.set(2010, 0, 23);
    toCal.set(2010, 2, 21);
    printResult(fromCal, toCal);

    fromCal.set(2011, 0, 22);
    toCal.set(2010, 2, 21);
    printResult(fromCal, toCal);
  }