创建X个日期数的算法

问题描述

目前,我有一个以yyyyMM格式表示日期的字符串列表,如下所示:

  • 202008
  • 202009
  • 202010

我需要在此列表中创建x个条目,每个条目将月份增加一个,因此,如果要创建3个新条目,它们将如下所示:

  • 202011
  • 202012
  • 202101

当前,我的想法是创建一种选择最新日期的方法,将String解析为月份和年份,如果

我想知道的是,是否有我可以使用的更优雅的解决方案,也许是使用现有的日期库(我正在使用Java)。

解决方法

YearMonthDateTimeFormatter

我建议您使用这些modern date-time classes进行操作,如下所示:

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        // Test
        List<String> list = getYearMonths("202011",3);
        System.out.println(list);

        // Bonus: Print each entry of the obtained list,in a new line
        list.forEach(System.out::println);
    }

    public static List<String> getYearMonths(String startWith,int n) {
        List<String> list = new ArrayList<>();

        // Define Formatter
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuuMM");

        // Parse the year-month string using the defined formatter
        YearMonth ym = YearMonth.parse(startWith,formatter);

        for (int i = 1; i <= n; i++) {
            list.add(ym.format(formatter));
            ym = ym.plusMonths(1);// Increase YearMonth by one month
        }
        return list;
    }
}

输出:

[202011,202012,202101]
202011
202012
202101

通过 Trail: Date Time 了解有关现代日期时间API的更多信息。

,

使用正确的日期时间对象:YearMonth

不要将日期作为字符串存储在列表中。就像您对数字使用int和对布尔值使用boolean(我希望!)一样,对日期和时间也使用正确的date-time对象。对于您的用例,YearMonth类是合适的。

就像将int格式化为带有或不带有千位分隔符的格式,以及将boolean格式化为 yes no 一样容易,例如,将YearMonth对象格式化为字符串很简单。如此,当您需要一个字符串而不是之前。

扩展此类YearMonth对象列表的方法是:

public static void extendDateList(List<YearMonth> dates,int numberOfNewDates) {
    if (dates.isEmpty()) {
        throw new IllegalArgumentException("List is empty; don’t know where to pick up");
        // Or may start from some fixed date or current month
    } else {
        YearMonth current = Collections.max(dates);
        for (int i = 0; i < numberOfNewDates; i++) {
            current = current.plusMonths(1);
            dates.add(current);
        }
    }
}

让我们尝试一下:

    List<YearMonth> dates = new ArrayList<YearMonth>(List.of(
            YearMonth.of(2020,Month.AUGUST),YearMonth.of(2020,Month.SEPTEMBER),Month.OCTOBER)));
    
    extendDateList(dates,3);
    
    System.out.println(dates);

输出为:

[2020-08、2020-09、2020-10、2020-11、2020-12、2021-01]

格式化为字符串

我向您保证,您可以轻松拥有自己的琴弦。我建议使用上面带有连字符的格式,其原因有两个:(1)更具可读性,(2)是国际标准ISO 8601格式。无论如何,为了说明您可以按照自己的方式进行操作,我正在使用格式化程序来生成您使用的无连字符格式:

private static final DateTimeFormatter YEAR_MONTH_FORMATTER
        = DateTimeFormatter.ofPattern("uuuuMM");

现在转换为字符串列表是一种方法(如果您的编辑器窗口足够宽):

    List<String> datesAsStrings = dates.stream()
            .map(YEAR_MONTH_FORMATTER::format)
            .collect(Collectors.toList());
    System.out.println(datesAsStrings);

[202008、202009、202010、202011、202012、202101]

链接

,

由于您只是在处理字符串,因此可以创建一个方法来为您生成字符串日期,并在String Array中返回所有这些字符串日期,如下所示:

public static String[] addMonthsToDateString(String startDate,int monthsToAdd) {
    // Break the string date down to integers Year and month.
    int year = Integer.valueOf(startDate.substring(0,4));
    int month = Integer.valueOf(startDate.substring(4));
    // Calculate the number of iterations we need.
    int loopCount = ((month + monthsToAdd) - month);
    // Declare and initialize the String Array we will return.
    String[] stringDates = new String[loopCount];
    
    // Generate the required Date Strings
    for (int i = 0; i < loopCount; i++) {
        stringDates[i] = new StringBuilder("").append(year)
                         .append(String.format("%02d",month)).toString();
        month++;
        if (month == 13) {
            year++;
            month = 1;
        }
    }
    return stringDates;
}

要使用此方法,您需要提供一个字符串开始日期(“ 202008”)和要列出的整数月数(24):

// Get the desired list of string dates:
String[] desiredDates = addMonthsToDateString("202008",24);

// Display the string dates produced into the Console Window:
for (String strg : desiredDates) {
    System.out.println(strg);
}

控制台窗口将显示:

202008
202009
202010
202011
202012
202101
202102
202103
202104
202105
202106
202107
202108
202109
202110
202111
202112
202201
202202
202203
202204
202205
202206
202207