如何解析java中的RRULE

时间:2017-04-10 09:04:56

标签: java icalendar rrule

我有规则示例:

<View>
    <Text>Food List</Text>
    <List>
      { this.renderList() }
    </List>
</View>

renderList(){
    return this.props.foodList.map((el, i) => {
        if(this.state.type == 'all' || el.type == this.state.type)
            return  <ListItem
                        key= {i}
                        title= {el.name}
                        subtitle = {el.type}
                    />
        else
          return null;
    })
}

我需要java库来解析在对象中处理的模式。有没有好的java库?

3 个答案:

答案 0 :(得分:5)

您可以使用lib-recur

它仍然受支持并处理RFC 5545和RFC 2445。

RecurrenceRule rule = new RecurrenceRule("FREQ=YEARLY;BYMONTHDAY=23;BYMONTH=5");

DateTime start = new DateTime(1982, 4 /* 0-based month numbers! */,23);

RecurrenceRuleIterator it = rule.iterator(start);

int maxInstances = 100; // limit instances for rules that recur forever

while (it.hasNext() && (!rule.isInfinite() || maxInstances-- > 0))
{
    DateTime nextInstance = it.nextDateTime();
    // do something with nextInstance
}

您可以使用maven进行安装

<!-- https://mvnrepository.com/artifact/org.dmfs/lib-recur -->
<dependency>
    <groupId>org.dmfs</groupId>
    <artifactId>lib-recur</artifactId>
     <version>0.10.2</version>
</dependency>

或带有gradle

// https://mvnrepository.com/artifact/org.dmfs/lib-recur 
compile group: 'org.dmfs', name: 'lib-recur', version: '0.10.2'

此处提供了更多文档:https://github.com/dmfs/lib-recur

答案 1 :(得分:4)

solution is to use

        <dependency>
            <groupId>org.scala-saddle</groupId>
            <artifactId>google-rfc-2445</artifactId>
            <version>20110304</version>
        </dependency>

几个例子:

1转换为java对象:

rule = new RRule("RRULE:FREQ=WEEKLY;INTERVAL=2;BYDAY=TU,WE,TH");

2转回:

rule.toIcal();

答案 2 :(得分:0)

这是我通过https://github.com/mangstadt/biweekly库生成了几个(dateStart-dateEnd)的方法。

收到的是: -开始日期 -结束日期(教师) -一个规则,用|

分隔
DTSTART:2019-07-01T16:00:00Z|DTEND:2019-07-01T17:00:00Z|RRULE:FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR,SA,SU;UNTIL=2019-07-31T23:59:59Z

代码:

// A.  split filter $iCalFilter
Map<String, String> iCalFilterMap = new HashMap<>();
for (String part : iCalFilter.split("\\|")) {
    if (part.contains(":")) {
        String[] subparts = part.split(":", 2);
        iCalFilterMap.put(subparts[0], subparts[1]);
    }
}

// B. generate couples of dates starts and  dates ends
ParseContext context = new ParseContext();
context.setVersion(ICalVersion.V2_0);
RecurrenceRuleScribe scribe = new RecurrenceRuleScribe();
RecurrenceRule rrule = scribe.parseText(iCalFilterMap.get(iCalFilterConstant.RRULE), ICalDataType.DATE_TIME, new ICalParameters(), context);
TimeZone timezone = TimeZone.getTimeZone(iCalFilterConstant.UTC);
int iCalFilterMaximumDayToGenerate = 365;

// starts
List<DateTime> listeDateStart = new ArrayList<>();
if (null != iCalFilterMap.get(iCalFilterConstant.DTSTART)) {
    DateTime dateTimeStart = new DateTime(iCalFilterMap.get(iCalFilterConstant.DTSTART)).withZone(iCalFilterConstant.DATE_TIME_ZONE_UTC);
    DateIterator ditStart = rrule.getDateIterator(dateTimeStart.toDate(), timezone);
    int compteurDateStart = 0;
    while (ditStart.hasNext()) {
        listeDateStart.add(new DateTime(ditStart.next()).withZone(iCalFilterConstant.DATE_TIME_ZONE_UTC));

        // invoid very long loop while trying 9999-12-12T23:59:59
        compteurDateStart++;
        if(compteurDateStart > iCalFilterMaximumDayToGenerate) {
            break;
        }
    }
}


// ends
List<DateTime> listeDateEnd = new ArrayList<>();
DateTime dateTimeEnd;
if (null != iCalFilterMap.get(iCalFilterConstant.DTEND)) {
    dateTimeEnd = new DateTime(iCalFilterMap.get(iCalFilterConstant.DTEND)).withZone(iCalFilterConstant.DATE_TIME_ZONE_UTC);
} else {
    // if DTEND not present, by default we add end of day
    dateTimeEnd = new DateTime(iCalFilterMap.get(iCalFilterConstant.DTSTART)).withZone(iCalFilterConstant.DATE_TIME_ZONE_UTC).withTime(23, 59, 59, 999);
}

DateIterator ditEnd = rrule.getDateIterator(dateTimeEnd.toDate(), timezone);
int compteurDateEnd = 0;
while (ditEnd.hasNext()) {
    listeDateEnd.add(new DateTime(ditEnd.next()).withZone(iCalFilterConstant.DATE_TIME_ZONE_UTC));

    compteurDateEnd++;
    if (compteurDateEnd > iCalFilterMaximumDayToGenerate) {
        break;
    }

}

// C. on set le couple dans le filters LesHalles pour utilisation dans la requete bdd voir CourseJdbcDaoImpl.addiCalFilter
if(listeDateStart.size() == 0 || listeDateEnd.size() == 0) {
    throw new ServiceFunctionalException(new Object[] { iCalFilter  }, MessageFonctionnelTheorique.IN_CALENDAR_RRULE_AUCUNE_DATE_GENEREE);
}
ImmutablePair<List<DateTime>, List<DateTime>> pairListeDatesStartsDatesEnds = new ImmutablePair<>(listeDateStart, listeDateEnd);
filters.setiCalFilterDateStartDateEnds(pairListeDatesStartsDatesEnds);
}

您现在可以将这两个datestart日期结束用于数据库查询