Ejemplo n.º 1
1
  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;
  }
 private void doTest(String candidate, int expectedHours, int expectedMins) throws ParseException {
   DateFormat df = new SimpleDateFormat("HH:mm");
   Date result = df.parse(candidate);
   Calendar cal = Calendar.getInstance();
   cal.setTime(result);
   assertEquals(expectedHours, cal.get(Calendar.HOUR_OF_DAY));
   assertEquals(expectedMins, cal.get(Calendar.MINUTE));
 }
Ejemplo n.º 3
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);
  }
Ejemplo n.º 4
0
  /**
   * 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());
  }
Ejemplo n.º 5
0
 public void testDateValue() {
   Date date = new Date();
   final Date date1 = Vba.dateValue(date);
   Calendar calendar = Calendar.getInstance();
   calendar.setTime(date1);
   assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY));
   assertEquals(0, calendar.get(Calendar.MINUTE));
   assertEquals(0, calendar.get(Calendar.SECOND));
   assertEquals(0, calendar.get(Calendar.MILLISECOND));
 }
Ejemplo n.º 6
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();
  }
  /**
   * Converts a Date into a formatted string.
   *
   * @param date The Date to convert.
   * @return The formatted string.
   */
  public static String format(Date date) {
    Calendar c = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    StringBuffer sb = new StringBuffer();

    c.setTime(date);
    sb.append(w4.format(c.get(Calendar.YEAR)));
    sb.append(w2.format(c.get(Calendar.MONTH) + 1));
    sb.append(w2.format(c.get(Calendar.DAY_OF_MONTH)));
    sb.append(w2.format(c.get(Calendar.HOUR_OF_DAY)));
    sb.append(w2.format(c.get(Calendar.MINUTE)));
    sb.append(w2.format(c.get(Calendar.SECOND)));
    return sb.toString();
  }
Ejemplo n.º 8
0
 /* paint() - get current time and draw (centered) in Component. */
 public void paint(Graphics g) {
   Calendar myCal = Calendar.getInstance();
   StringBuffer sb = new StringBuffer();
   sb.append(tf.format(myCal.get(Calendar.HOUR)));
   sb.append(':');
   sb.append(tflz.format(myCal.get(Calendar.MINUTE)));
   sb.append(':');
   sb.append(tflz.format(myCal.get(Calendar.SECOND)));
   String s = sb.toString();
   FontMetrics fm = getFontMetrics(getFont());
   int x = (getSize().width - fm.stringWidth(s)) / 2;
   // System.out.println("Size is " + getSize());
   g.drawString(s, x, 10);
 }
Ejemplo n.º 9
0
 public void testDate() {
   final Date date = Vba.date();
   assertNotNull(date);
   Calendar calendar = Calendar.getInstance();
   calendar.setTime(date);
   assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY));
   assertEquals(0, calendar.get(Calendar.MILLISECOND));
 }
Ejemplo n.º 10
0
  static String formatDate(Date d) {
    Calendar c = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    StringBuffer sb = new StringBuffer();
    NumberFormat w4 = new DecimalFormat();
    w4.setMinimumIntegerDigits(4);
    w4.setGroupingUsed(false);
    NumberFormat w2 = new DecimalFormat();
    w2.setMinimumIntegerDigits(2);

    c.setTime(d);
    sb.append(w4.format(c.get(c.YEAR)));
    sb.append(w2.format(c.get(c.MONTH) + 1));
    sb.append(w2.format(c.get(c.DAY_OF_MONTH)));
    sb.append(w2.format(c.get(c.HOUR_OF_DAY)));
    sb.append(w2.format(c.get(c.MINUTE)));
    sb.append(w2.format(c.get(c.SECOND)));
    return sb.toString();
  }
Ejemplo n.º 11
0
 public static String getZoneOffset(Date date, Locale locale) {
   String dateStr;
   Calendar cal = Calendar.getInstance(locale);
   cal.setTime(date);
   int offset = cal.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000);
   if (offset > -1) dateStr = " +" + offset + "h";
   else dateStr = " -" + offset + "h";
   return dateStr;
 }
Ejemplo n.º 12
0
  private void saveStartProjects(File file) throws IOException {
    Calendar cal = Calendar.getInstance();

    try (PrintWriter out = new PrintWriter(file, "windows-1251")) {
      for (Region region : this.regions) {
        cal.setTime(region.start);

        out.write("\"");
        out.write(region.filial + " " + region.name);
        out.write("\";;;");

        int skeepCells = (cal.get(Calendar.YEAR) - 2017) * 5 + (cal.get(Calendar.MONDAY) % 4);
        for (int i = 0; i < skeepCells; i++) {
          out.write(";");
        }
        out.println("X");
      }
    }
  }
Ejemplo n.º 13
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();
  }
Ejemplo n.º 14
0
  /**
   * Map millis and timeZone to its component fields.
   *
   * <p>Bits0: ------------------------------------------------ Field Num Bits Range Loc
   * ------------------------------------------------ Year 16 short 16-31 Millis 16 short 0-15
   *
   * <p>Bits1: ------------------------------------------------ Field Num Bits Range Loc
   * ------------------------------------------------ Daylight 1 0-1 29-29 Month 4 1-12 25-28 Day 5
   * 1-31 20-24 Hour 5 0-23 15-19 Minutes 6 0-59 9-14 Seconds 6 0-59 3-8 Weekday 3 0-6 0-2
   * ------------------------------------------------
   */
  private void millisToFields() {
    // init a calendar with timeZone and millis
    Calendar calendar = new GregorianCalendar(timeZone);
    Date date = new Date(millis);
    calendar.setTime(date);

    // set year bits
    int x = calendar.get(Calendar.YEAR);
    bits0 |= ((x & 0xFFFF) << 16);

    // set millisecond bits
    x = calendar.get(Calendar.MILLISECOND);
    bits0 |= ((x & 0xFFFF) << 0);

    // set month bits
    x = calendar.get(Calendar.MONTH) + 1;
    bits1 |= ((x & 0x0F) << 25);

    // set day bits
    x = calendar.get(Calendar.DAY_OF_MONTH);
    bits1 |= ((x & 0x1F) << 20);

    // set hour bits
    x = calendar.get(Calendar.HOUR_OF_DAY);
    bits1 |= ((x & 0x1F) << 15);

    // set minute bits
    x = calendar.get(Calendar.MINUTE);
    bits1 |= ((x & 0x3F) << 9);

    // set seconds bits
    x = calendar.get(Calendar.SECOND);
    bits1 |= ((x & 0x3F) << 3);

    // set weekday
    x = calendar.get(Calendar.DAY_OF_WEEK) - 1;
    bits1 |= ((x & 0x07) << 0);

    // set daylight bit
    if (timeZone.inDaylightTime(date)) bits1 |= (0x01 << 29);
  }
Ejemplo n.º 15
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();
  }
 public static void main(String[] args) {
   int months[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
   System.out.println("What is your birthday? (MM/dd/yyyy)");
   Scanner scanner = new Scanner(System.in);
   String birthDay = scanner.nextLine();
   System.out.println("What is your name?");
   String name = scanner.nextLine();
   String[] parts = birthDay.split("/");
   Calendar time = Calendar.getInstance();
   int ageMillenniums = 0;
   int ageCenturies = 0;
   int ageDecades = 0;
   String stringMillenniums = "";
   String stringCenturies = "";
   String stringDecades = "";
   String stringYears = "";
   String stringMonths = "";
   String stringDays = "";
   String stringHours = "";
   String stringMinutes = "";
   String stringSeconds = "";
   int day = (time.get(Calendar.DATE));
   int month = (months[time.get(Calendar.MONTH)]);
   int year = (time.get(Calendar.YEAR));
   int hour = (time.get(Calendar.HOUR));
   int minute = (time.get(Calendar.MINUTE));
   int second = (time.get(Calendar.SECOND));
   int ageDays = day - Integer.parseInt(parts[1]);
   int ageMonths = month - Integer.parseInt(parts[0]);
   int ageYears = year - Integer.parseInt(parts[2]);
   if (ageDays < 0) {
     if (month == 2
         || month == 4
         || month == 6
         || month == 8
         || month == 9
         || month == 11
         || month == 1) {
       ageMonths--;
       ageDays += 31;
     } else if (month == 5 || month == 7 || month == 10 || month == 12) {
       ageMonths--;
       ageDays += 30;
     } else {
       ageMonths--;
       ageDays += 28;
     }
   }
   if (ageMonths < 0) {
     ageYears--;
     ageMonths += 12;
   }
   while (ageYears > 1000) {
     ageYears -= 1000;
     ageMillenniums++;
   }
   while (ageYears > 100) {
     ageYears -= 100;
     ageCenturies++;
   }
   while (ageYears > 10) {
     ageYears -= 10;
     ageDecades++;
   }
   if (ageMillenniums == 0) {
     stringMillenniums = "";
   } else if (ageMillenniums == 1) {
     stringMillenniums = ageMillenniums + " millennium, ";
   } else {
     stringMillenniums = ageMillenniums + " millenniums, ";
   }
   if (ageCenturies == 0) {
     stringCenturies = "";
   } else if (ageCenturies == 1) {
     stringCenturies = ageCenturies + " century, ";
   } else {
     stringCenturies = ageCenturies + " centuries, ";
   }
   if (ageDecades == 0) {
     stringDecades = "";
   } else if (ageDecades == 1) {
     stringDecades = ageDecades + " decade, ";
   } else {
     stringDecades = ageDecades + " decades, ";
   }
   if (ageYears == 0) {
     stringYears = "";
   } else if (ageYears == 1) {
     stringYears = ageYears + " year, ";
   } else {
     stringYears = ageYears + " years, ";
   }
   if (ageMonths == 0) {
     stringMonths = "";
   } else if (ageMonths == 1) {
     stringMonths = ageMonths + " month, ";
   } else {
     stringMonths = ageMonths + " months, ";
   }
   if (ageDays == 0) {
     stringDays = "";
   } else if (ageDays == 1) {
     stringDays = ageDays + " day, ";
   } else {
     stringDays = ageDays + " days, ";
   }
   if (hour == 0) {
     stringHours = "";
   } else if (hour == 1) {
     stringHours = hour + " hour, ";
   } else {
     stringHours = hour + " hours, ";
   }
   if (minute == 0) {
     stringMinutes = "";
   } else if (minute == 1) {
     stringMinutes = minute + " minute, ";
   } else {
     stringMinutes = minute + " minutes, ";
   }
   if (second == 0) {
     stringSeconds = "";
   } else if (second == 1) {
     stringSeconds = second + " second ";
   } else {
     stringSeconds = second + " seconds ";
   }
   System.out.println(
       name
           + " is "
           + stringMillenniums
           + stringCenturies
           + stringDecades
           + stringYears
           + stringMonths
           + stringDays
           + stringHours
           + stringMinutes
           + stringSeconds
           + "old");
 }
Ejemplo n.º 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);
  }
Ejemplo n.º 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;
  }
Ejemplo n.º 19
0
 private int getDay(long miliSecs) {
   Calendar calendar = Calendar.getInstance();
   calendar.setTime(new Date(miliSecs));
   return calendar.get(calendar.DAY_OF_MONTH);
 }
Ejemplo n.º 20
0
  public static String getTimeShow(String dateString) {
    Date thenDate = stringToDate(dateString);
    Calendar then = Calendar.getInstance();
    then.setTime(thenDate);
    Calendar current = Calendar.getInstance();

    long differ = current.getTimeInMillis() - then.getTimeInMillis();

    if (differ < 0) {
      return "Milai";
    } else {
      if (differ >= 0L && differ < 300000L) {
        return "Just now";
      }

      if (differ >= 300000L && differ < 10800000L) {
        return MessageFormat.format("{0}:{1}", then.get(Calendar.HOUR), then.get(Calendar.MINUTE));
      }

      if (then.get(Calendar.YEAR) != current.get(Calendar.YEAR)) {
        return MessageFormat.format(
            "{0}/{1}/{2}",
            then.get(Calendar.YEAR) - 2000, then.get(Calendar.MONTH), then.get(Calendar.DATE));
      }

      if (current.get(Calendar.DAY_OF_YEAR) - then.get(Calendar.DAY_OF_YEAR) == 1) {
        return "Yesterday";
      }

      if (then.get(Calendar.MONTH) != current.get(Calendar.MONTH)) {
        MDebug.log("[DATE]" + then.get(Calendar.DATE));
        return MessageFormat.format(
            "{0}/{1}/{2}",
            then.get(Calendar.YEAR) - 2000,
            then.get(Calendar.MONTH),
            then.get(Calendar.DAY_OF_MONTH));
      }

      int dayDiff = current.get(Calendar.DAY_OF_MONTH) - then.get(Calendar.DAY_OF_MONTH);
      if (dayDiff > 7) {
        return MessageFormat.format(
            "{0}/{1}/{2}",
            then.get(Calendar.YEAR) - 2000, then.get(Calendar.MONTH), then.get(Calendar.DATE));
      } else {
        if (dayDiff == 0) {
          int hour = then.get(Calendar.HOUR);
          if (0 <= hour && hour < 5) {
            return "MidNight";
          } else if (5 <= hour && hour < 12) {
            return "BeforeNoon";
          } else if (12 <= hour && hour < 19) {
            return "AfterNoon";
          } else if (19 <= hour && hour != 0) {
            return "Night";
          }
        }

        if (current.get(Calendar.WEEK_OF_MONTH) == then.get(Calendar.WEEK_OF_MONTH)) {
          return getDayString(then.get(Calendar.DAY_OF_WEEK));
        } else {
          return MessageFormat.format(
              "{0}/{1}/{2}",
              then.get(Calendar.YEAR) - 2000, then.get(Calendar.MONTH), then.get(Calendar.DATE));
        }
      }
    }
  }
 public int generate(final Calendar when) {
   return (when.get(Calendar.HOUR_OF_DAY) * 60 + when.get(Calendar.MINUTE)) / minutes;
 }