예제 #1
0
  /**
   * ComputeNumDays calculates how many days should be in the current month, since some months have
   * 30 days and others have 31. Leap years are also accounted for.
   *
   * @return days an array of days calculated to fit the current month
   */
  public int[] computeNumDays(int mont, int year) {

    // Simply, we instantiate an array of days, then proceed
    // to get the current month. if the current month matches
    // 0,2,4,6,7,9, or 11 we know there are 31 days.
    // If the current month is February, we ask if it is a leap
    // year. If it is, then an array of 29 days is made. Otherwise,
    // the array is set to 28.
    // In the case that the current month is it not any of those
    // predetermined numbers, the array is set to a size of
    // 30 days.
    cale.set(Calendar.MONTH, mont - 1);
    int[] days;
    int mon = cale.get(Calendar.MONTH);
    if (mon == 0 || mon == 2 || mon == 4 || mon == 6 || mon == 7 || mon == 9 || mon == 11) {
      days = new int[31];
    } else if (mon == 1) {
      if (cale.isLeapYear(year)) {
        days = new int[29];
      } else {
        days = new int[28];
      }
    } else {
      days = new int[30];
    }

    for (int b = 0; b < days.length; b++) {

      days[b] = b + 1;
    }
    return days;
  }
예제 #2
0
  /**
   * Estrazione del numero di giorni in uno specifico mese dell'anno.
   *
   * @param month - Mese (partenza da 0)
   * @param year - Anno
   * @return Restituisce il numero di giorni in un mese; -1 in caso di errore;
   */
  public int getDaysInMonth(int month, int year) {
    int days = -1;
    switch (month) {
      case 0: // GENNAIO
      case 2: // MARZO
      case 4: // MAGGIO
      case 6: // LUGLIO
      case 7: // AGOSTO
      case 9: // OTTOBRE
      case 11: // DICEMBRE
        days = 31;
        break;

      case 1: // FEBBRAIO
        if (calendar.isLeapYear(year)) days = 29;
        else days = 28;
        break;

      case 3: // APRILE
      case 5: // GIUGNO
      case 8: // SETTEMBRE
      case 10: // NOVEMBRE
        days = 30;
        break;

      default:
        days = -1;
        break;
    }

    return days;
  }
예제 #3
0
  public GreatDate() {
    year = calendar.get(Calendar.YEAR);
    month = calendar.get(Calendar.MONTH);
    day = calendar.get(Calendar.DATE);
    hour = calendar.get(Calendar.HOUR);
    minute = calendar.get(Calendar.MINUTE);
    second = calendar.get(Calendar.SECOND);

    if (calendar.isLeapYear(year)) {
      isLeapYear = true;
    }
  }
 static int getDaysInMonth(int month, int year) {
   int days = 0;
   switch (month) {
     case 1: // Jan
     case 3: // March
     case 5: // May
     case 7: // July
     case 8: // Aug
     case 10: // Oct
     case 12: // Dec
       {
         days = 31;
         break;
       }
     case 4: // Apr
     case 6: // June
     case 9: // Sept
     case 11: // Nov
       {
         days = 30;
         break;
       }
     case 2: // Feb
       {
         days = 28;
         GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
         if (cal.isLeapYear(year)) {
           days = 29;
         }
         break;
       }
     default:
       {
         days = 0;
       }
   }
   return days;
 }
예제 #5
0
  public boolean mesAgregarTorneoIni() {

    MesbuenoI = true;
    String mes = modificartorneo.mesini.getText();
    int anoi = Integer.parseInt(modificartorneo.yearini.getText());
    GregorianCalendar calendar = new GregorianCalendar();
    boolean bisiesto;
    bisiesto = calendar.isLeapYear(anoi);
    int diai = Integer.parseInt(modificartorneo.diaini.getText());
    if ("".equals(mes) || " ".equals(mes) || mes == null) {

      JOptionPane.showMessageDialog(
          null,
          "El campo de mes de inicio, no se puede encontrar vacío,"
              + "por favor verifique e intente nuevamente",
          "Información",
          JOptionPane.INFORMATION_MESSAGE);
      modificartorneo.mesini.setText("");
      modificartorneo.yearini.setText("");
      modificartorneo.mesini.requestFocus();
      return false;

    } else {
      mesi = Integer.parseInt(modificartorneo.mesini.getText());
      if (mesi > 12) {
        JOptionPane.showMessageDialog(
            null,
            "El valor introducido en el campo de mes de inicio, no es valido,"
                + "por favor verifique e intente nuevamente",
            "Información",
            JOptionPane.INFORMATION_MESSAGE);
        modificartorneo.mesini.setText("");
        modificartorneo.yearini.setText("");
        modificartorneo.mesini.requestFocus();
        return false;
      }
    }

    if (mesi == 4 || mesi == 6 || mesi == 9 || mesi == 11) {

      if (diai > 30) {
        JOptionPane.showMessageDialog(
            null,
            "El valor introducido en el campo de dia de inicio, no es valido,"
                + "por favor verique e intente nuevamente",
            "Información",
            JOptionPane.INFORMATION_MESSAGE);
        modificartorneo.diaini.setText("");
        modificartorneo.diaini.requestFocus();
        MesbuenoI = false;
      }
    } else if (mesi == 1 || mesi == 3 || mesi == 5 || mesi == 7 || mesi == 8 || mesi == 10
        || mesi == 12) {

      if (diai > 31) {
        JOptionPane.showMessageDialog(
            null,
            "El valor introducido en el campo de dia de inicio, no es valido,"
                + "por favor verique e intente nuevamente",
            "Información",
            JOptionPane.INFORMATION_MESSAGE);
        modificartorneo.diaini.setText("");
        modificartorneo.diaini.requestFocus();
        MesbuenoI = false;
      }
    } else if (mesi == 2) {

      if (bisiesto == false && diai > 28) {
        JOptionPane.showMessageDialog(
            null,
            "El valor introducido en el campo de dia de inicio, no es valido,"
                + "por favor verique e intente nuevamente",
            "Información",
            JOptionPane.INFORMATION_MESSAGE);
        modificartorneo.diaini.setText("");
        modificartorneo.diaini.requestFocus();
        MesbuenoI = false;
      } else if (bisiesto = true && diai > 29) {
        JOptionPane.showMessageDialog(
            null,
            "El valor introducido en el campo de dia de inicio, no es valido,"
                + "por favor verique e intente nuevamente",
            "Información",
            JOptionPane.INFORMATION_MESSAGE);
        modificartorneo.diaini.setText("");
        modificartorneo.diaini.requestFocus();
        MesbuenoI = false;
      }
    }
    return MesbuenoI;
  }
예제 #6
0
 /**
  * 判断给定日期是不是润年
  *
  * @param p_date 给定日期
  * @return boolean 如果给定的年份为闰年,则返回 true;否则返回 false。 @Date: 2006-10-31
  */
 public static boolean isLeapYear(Date p_date) {
   int l_year = getYearOfDate(p_date);
   GregorianCalendar l_calendar = new GregorianCalendar();
   return l_calendar.isLeapYear(l_year);
 }
    /**
     * Prints Month
     *
     * @param mm
     * @param yy
     */
    private void printMonth(int mm, int yy) {
      Log.d(tag, "==> printMonth: mm: " + mm + " " + "yy: " + yy);
      int trailingSpaces = 0;
      int daysInPrevMonth = 0;
      int prevMonth = 0;
      int prevYear = 0;
      int nextMonth = 0;
      int nextYear = 0;

      int currentMonth = mm - 1;
      String currentMonthName = getMonthAsString(currentMonth);
      daysInMonth = getNumberOfDaysOfMonth(currentMonth);

      Log.d(tag, "Current Month: " + " " + currentMonthName + " having " + daysInMonth + " days.");

      GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 1);
      Log.d(tag, "Gregorian Calendar:= " + cal.getTime().toString());

      if (currentMonth == 11) {
        prevMonth = currentMonth - 1;
        daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
        nextMonth = 0;
        prevYear = yy;
        nextYear = yy + 1;
        Log.d(
            tag,
            "*->PrevYear: "
                + prevYear
                + " PrevMonth:"
                + prevMonth
                + " NextMonth: "
                + nextMonth
                + " NextYear: "
                + nextYear);
      } else if (currentMonth == 0) {
        prevMonth = 11;
        prevYear = yy - 1;
        nextYear = yy;
        daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
        nextMonth = 1;
        Log.d(
            tag,
            "**--> PrevYear: "
                + prevYear
                + " PrevMonth:"
                + prevMonth
                + " NextMonth: "
                + nextMonth
                + " NextYear: "
                + nextYear);
      } else {
        prevMonth = currentMonth - 1;
        nextMonth = currentMonth + 1;
        nextYear = yy;
        prevYear = yy;
        daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
        Log.d(
            tag,
            "***---> PrevYear: "
                + prevYear
                + " PrevMonth:"
                + prevMonth
                + " NextMonth: "
                + nextMonth
                + " NextYear: "
                + nextYear);
      }

      int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
      trailingSpaces = currentWeekDay;

      Log.d(tag, "Week Day:" + currentWeekDay + " is " + getWeekDayAsString(currentWeekDay));
      Log.d(tag, "No. Trailing space to Add: " + trailingSpaces);
      Log.d(tag, "No. of Days in Previous Month: " + daysInPrevMonth);

      if (cal.isLeapYear(cal.get(Calendar.YEAR)))
        if (mm == 2) ++daysInMonth;
        else if (mm == 3) ++daysInPrevMonth;

      // Trailing Month days
      for (int i = 0; i < trailingSpaces; i++) {
        Log.d(
            tag,
            "PREV MONTH:= "
                + prevMonth
                + " => "
                + getMonthAsString(prevMonth)
                + " "
                + String.valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET) + i));
        list.add(
            String.valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET) + i)
                + "-GREY"
                + "-"
                + getMonthAsString(prevMonth)
                + "-"
                + prevYear);
      }

      // Current Month Days
      for (int i = 1; i <= daysInMonth; i++) {
        Log.d(
            currentMonthName, String.valueOf(i) + " " + getMonthAsString(currentMonth) + " " + yy);
        if (i == getCurrentDayOfMonth()) {
          list.add(String.valueOf(i) + "-BLUE" + "-" + getMonthAsString(currentMonth) + "-" + yy);
        } else {
          list.add(String.valueOf(i) + "-WHITE" + "-" + getMonthAsString(currentMonth) + "-" + yy);
        }
      }

      // Leading Month days
      for (int i = 0; i < list.size() % 7; i++) {
        Log.d(tag, "NEXT MONTH:= " + getMonthAsString(nextMonth));
        list.add(
            String.valueOf(i + 1) + "-GREY" + "-" + getMonthAsString(nextMonth) + "-" + nextYear);
      }
    }
예제 #8
0
  public static double dayCountFraction(Date startDate, Date endDate, String method)
        /**
         * (I) method: B30/360 B30E/360 ACT/365 (ACT/ACT)ACT/365 FIXED ACT/360 NL/365 FIXED (no
         * leap) B30E/360 (Italy) 30/360SIA
         */
      {

    Date cur_date;
    Date temp;
    int start_year, end_year;
    double sign = 1.0;
    int i;
    long act_days, leap_days, non_leap_days, days_left;
    boolean isleap;
    double result = 0;

    /**
     * This duplicates some code below and dayCountDaysDiff() and is done for performance reasons.
     */
    if (method.equals(DayCount.ACT_365F.name)) {
      return DateUtils.dateDiff(startDate, endDate) / 365.;
    } else if (method.equals(DayCount.ACT_360.name)) {
      return DateUtils.dateDiff(startDate, endDate) / 360.;
    }

    /** Check if same date */
    if (startDate == endDate) {
      return 0;
    }

    /** Check the date order */
    if (startDate.after(endDate)) {
      sign = -1.0;
      /** reverse order */
      temp = startDate;
      startDate = endDate;
      endDate = temp;
    }

    if (method.equals(DayCount.EFFECTIVE_RATE.name)) {
      /**
       * Effective rates have a year fraction of 1.0 or -1.0, depending on the order of the dates
       * (note: if the dates are the same, the year fraction is 0 (handled above).
       */
      return sign;
    }

    /** Calculate the number of days between two dates */
    act_days = dayCountDaysDiff(startDate, endDate, method);

    if (method.equals(DayCount.B30_360.name)
        || method.equals(DayCount.B30E_360.name)
        || method.equals(DayCount.ACT_360.name)) {
      result = act_days / 360.0;
    } else if (method.equals(DayCount.ACT_365F.name)) {
      result = act_days / 365.0;
    } else if (method.equals(DayCount.ACT_365.name) || method.equals(DayCount.ACT_ACT.name)) {

      leap_days = 0;
      non_leap_days = 0;

      /**
       * weighted average of leap days divided by 366 + weighted average of non-leap days divided by
       * 365
       */
      /** handle first year */
      days_left = daysLeftThisYear(startDate);
      GregorianCalendar gcal1 = new GregorianCalendar();
      gcal1.setTime(startDate);
      isleap = gcal1.isLeapYear(gcal1.get(Calendar.YEAR));

      if (isleap) {
        leap_days += Math.min(act_days, days_left);
      } else {
        non_leap_days += Math.min(act_days, days_left);
      }

      /** loop through the years */
      start_year = gcal1.get(Calendar.YEAR);
      GregorianCalendar gcal2 = new GregorianCalendar();
      gcal2.setTime(endDate);
      end_year = gcal2.get(Calendar.YEAR);
      cur_date = startDate;

      /** loop through full years */
      for (i = start_year + 1; i < end_year; i++) {
        /** check if previous year is a leap */
        if (isleap) {
          cur_date = DateUtils.addCalendarDay(cur_date, 366);
        }
        /** next year in days */
        else {
          cur_date = DateUtils.addCalendarDay(cur_date, 365);
        }
        /** next year in days */
        /** check if new year is a leap */
        GregorianCalendar gcalcur = new GregorianCalendar();
        gcalcur.setTime(cur_date);
        boolean cisleap = gcalcur.isLeapYear(gcalcur.get(Calendar.YEAR));
        if (cisleap) {
          leap_days += 366;
        } else {
          non_leap_days += 365;
        }
      }

      /** handle last year */
      if (start_year != end_year) {
        isleap = gcal2.isLeapYear(gcal2.get(Calendar.YEAR));
        cur_date = DateUtils.startOfYear(endDate);

        /** number of days elapsed in the last year */
        days_left = dayCountDaysDiff(cur_date, endDate, method);

        if (isleap) {
          leap_days += days_left;
        } else {
          non_leap_days += days_left;
        }
      }

      /** calculate final day count fraction ISDA interpretation */
      result = leap_days / 366.0 + non_leap_days / 365.0;
    }

    return result * sign;
  }
예제 #9
0
 public boolean isLeapYear(int year) {
   return cale.isLeapYear(year);
 }
예제 #10
0
  /**
   * Converts TT in YYYY MM DD.ddd format to MJD (days).
   *
   * @param yyyymmdd_ddd TT in YYYY MM DD.ddd format.
   * @return TT in MJD (days).
   */
  @SuppressWarnings("fallthrough")
  public static double convertMJD(String yyyymmdd_ddd) {
    StringTokenizer stringTokenizer = new StringTokenizer(yyyymmdd_ddd, " ,: ;/-");
    GregorianCalendar calendar = new GregorianCalendar();

    int year = 0;

    // monthIndex for January is 0, for December is 11
    int monthIndex = 0;
    double day = 0;

    String monthString = "";

    double resultInDays = 0.;

    if (stringTokenizer.hasMoreTokens()) year = Format.toInt(stringTokenizer.nextToken());

    if (stringTokenizer.hasMoreTokens()) {
      monthString = stringTokenizer.nextToken().toLowerCase();

      while (!monthString.startsWith(MONTH_NAMES[monthIndex].toLowerCase())) monthIndex++;
    }

    if (stringTokenizer.hasMoreTokens()) day = Format.toDouble(stringTokenizer.nextToken());

    // Add the remaining 44 days of 1858 (November 18 - December 31 inclusive)
    resultInDays += 44;

    if (year > MJD_0_YEAR) {
      for (int i = MJD_0_YEAR + 1; i < year; i++) {
        resultInDays += 365;

        if (calendar.isLeapYear(i)) resultInDays++;
      }
    } else if (year <= MJD_0_YEAR) {
      for (int i = MJD_0_YEAR; i >= year; i--) {
        resultInDays -= 365;

        if (calendar.isLeapYear(i)) resultInDays--;
      }
    }

    switch (monthIndex) {
      case 12:
        resultInDays += 31;
      case 11:
        resultInDays += 30;
      case 10:
        resultInDays += 31;
      case 9:
        resultInDays += 30;
      case 8:
        resultInDays += 31;
      case 7:
        resultInDays += 31;
      case 6:
        resultInDays += 30;
      case 5:
        resultInDays += 31;
      case 4:
        resultInDays += 30;
      case 3:
        resultInDays += 31;
      case 2:
        resultInDays += 28;
      case 1:
        resultInDays += 31;
    }

    if ((calendar.isLeapYear(year)) && (monthIndex > 1)) resultInDays++;

    resultInDays += day;

    resultInDays = Math.rint(resultInDays * 1E7) / 1E7;

    // just to be on the safe side

    resultInDays = makeMJD(resultInDays);

    return resultInDays;
  }