/// <summary>
 /// Parses a year-month from the input string.
 /// <para>
 /// Parsing is case insensitive.
 /// It accepts formats 'yyyy-MM', 'yyyyMM', 'MMM-yyyy' or 'MMMyyyy'.
 /// Some formats also accept two-digits years (use is not recommended): 'MMM-yy' or 'MMMyy'.
 ///
 /// </para>
 /// </summary>
 /// <param name="str">  the string to parse </param>
 /// <returns> the parsed value </returns>
 /// <exception cref="IllegalArgumentException"> if the string cannot be parsed </exception>
 public static YearMonth parseYearMonth(string str)
 {
     try
     {
         // yyyy-MM
         if (str.Length == 7 && str[4] == '-')
         {
             return(YearMonth.parse(str, YYYY_MM_DASH));
         }
         // MMM-yy
         // MMM-yyyy
         // MMMyy
         // MMMyyyy
         if (str.Length >= 5 && !char.IsDigit(str[0]))
         {
             return(YearMonth.parse(str, MMM_YEAR));
         }
         // d/M/yyyy - handle Excel converting YearMonth to date
         if (str.Length >= 8 && (str[1] == '/' || str[2] == '/'))
         {
             LocalDate date = LocalDate.parse(str, D_M_YEAR_SLASH);
             if (date.DayOfMonth == 1)
             {
                 return(YearMonth.of(date.Year, date.Month));
             }
             throw new System.ArgumentException("Found Excel-style date but day-of-month was not set to 1:" + str);
         }
         // yyyyMM
         return(YearMonth.parse(str, YYYYMM));
     }
     catch (DateTimeParseException)
     {
         throw new System.ArgumentException("Unknown date format, must be formatted as 'yyyy-MM', 'yyyyMM', " + "'MMM-yyyy', 'MMMyyyy', 'MMM-yy' or 'MMMyy' but was: " + str);
     }
 }
Пример #2
0
        private static CurveNode curveIborFutureCurveNode(string conventionStr, string timeStr, string label, QuoteId quoteId, double spread, CurveNodeDate date, CurveNodeDateOrder order)
        {
            Matcher matcher = FUT_TIME_REGEX.matcher(timeStr.ToUpper(Locale.ENGLISH));

            if (matcher.matches())
            {
                Period periodToStart            = Period.parse("P" + matcher.group(1));
                int    sequenceNumber           = int.Parse(matcher.group(2));
                IborFutureConvention convention = IborFutureConvention.of(conventionStr);
                IborFutureTemplate   template   = IborFutureTemplate.of(periodToStart, sequenceNumber, convention);
                return(IborFutureCurveNode.builder().template(template).rateId(quoteId).additionalSpread(spread).label(label).date(date).dateOrder(order).build());
            }
            Matcher matcher2 = FUT_MONTH_REGEX.matcher(timeStr.ToUpper(Locale.ENGLISH));

            if (matcher2.matches())
            {
                YearMonth            yearMonth  = YearMonth.parse(matcher2.group(1), YM_FORMATTER);
                IborFutureConvention convention = IborFutureConvention.of(conventionStr);
                IborFutureTemplate   template   = IborFutureTemplate.of(yearMonth, convention);
                return(IborFutureCurveNode.builder().template(template).rateId(quoteId).additionalSpread(spread).label(label).date(date).dateOrder(order).build());
            }
            throw new System.ArgumentException(Messages.format("Invalid time format for Ibor Future: {}", timeStr));
        }