recurring event logic

后端 未结 3 2096
北荒
北荒 2021-01-02 02:40

I\'m working on a Groovy/Java calendar-type application that allows the user to enter events with a start date and an optional recurrence. If it\'s a recurring event, it may

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-02 03:18

    I am not very familiar with Groovy library but since Groovy is running on JVM we then I suppose you should be able to use Java / Scala library as well.

    What you need here is a professional schedule generation library like Lamma(http://lamma.io) instead of a general purpose date time library like Joda.

        // monthly on a date of the month that corresponds to the start date
        // output: [Date(2014,6,10), Date(2014,7,10), Date(2014,8,10), Date(2014,9,10), Date(2014,10,10)]
        System.out.println(Dates.from(2014, 6, 10).to(2014, 10, 10).byMonth().build());
    
        // weekly on a day of the week of that corresponds to the start date
        // output: [Date(2014,6,10), Date(2014,6,17), Date(2014,6,24), Date(2014,7,1), Date(2014,7,8)]
        System.out.println(Dates.from(2014, 6, 10).to(2014, 7, 10).byWeek().build());
    
        // every 2 weeks on a day of the week of that corresponds to the start date
        // output: [Date(2014,6,10), Date(2014,6,24), Date(2014,7,8)]
        System.out.println(Dates.from(2014, 6, 10).to(2014, 7, 10).byWeeks(2).build());
    
        // edge cases are handled properly, for example, leap day
        // output: [Date(2012,2,29), Date(2013,2,28), Date(2014,2,28), Date(2015,2,28), Date(2016,2,29)]
        System.out.println(Dates.from(2012, 2, 29).to(2016, 2, 29).byYear().build());
    

提交回复
热议问题