0

我正在尝试在 Scala 中获取给定范围的字符串日期列表。有没有直接/更短的方法来实现这一目标?

val format = "yyyMMdd"
val startDate = "20200101"
val endDate = "20200131"

预期输出 = 列表(2020101,20200102, ....., 20200131)

4

2 回答 2

0

是的...如果您以您提供的格式添加破折号,您可以使用它LocalData来解析日期,而无需使用日期格式化程序使其复杂化。例如yyyy-MM-dd

val startDate = LocalDate.parse("2020-01-01")

val endDate = LocalDate.parse("2020-01-31") 

然后您可以使用java.time.LocalDate.datesUntil为您生成日期。之后,如果您真的想要对没有破折号的日期进行List一些操作,则需要进行一些收集操作。String这让你大部分时间。

startDate.datesUntil(endDate).collect(Collectors.toList()).asScala.map(date => date.toString)

List[String] = ArrayBuffer(2020-01-01, 2020-01-02, 2020-01-03, ...).toList
于 2020-09-14T21:34:08.160 回答
0

你可以这样做:

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

public class Main {
    public static void main(String[] args) {
        // Tests
        System.out.println(getDateList("20200101", "20200110"));
        System.out.println(getDateList("20200101", "20200131"));
    }

    static List<String> getDateList(String strStartDate, String strEndDate) {
        // List to be populated with the desired strings
        List<String> result = new ArrayList<>();

        // Formatter for the desired pattern
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");

        // Parse strings to LocalDate instances
        LocalDate startDate = LocalDate.parse(strStartDate, formatter);
        LocalDate endDate = LocalDate.parse(strEndDate, formatter);

        // Loop starting with start date until end date with a step of one day
        for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
            result.add(date.format(formatter));
        }

        // Return the populated list
        return result;
    }
}

输出:

[20200101, 20200102, 20200103,..., 20200110]
[20200101, 20200102, 20200103,..., 20200131]

使用 Java Stream API 的解决方案:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        // Tests
        System.out.println(getDateList("20200101", "20200110"));
        System.out.println(getDateList("20200101", "20200131"));
    }

    static List<String> getDateList(String strStartDate, String strEndDate) {
        // Formatter for the input and desired pattern
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");

        // Parse strings to LocalDate instances
        LocalDate startDate = LocalDate.parse(strStartDate, formatter);
        LocalDate endDate = LocalDate.parse(strEndDate, formatter);

        return Stream.iterate(startDate, date -> date.plusDays(1))
                .limit(ChronoUnit.DAYS.between(startDate, endDate.plusDays(1)))
                .map(date -> date.format(formatter))
                .collect(Collectors.toList());
    }
}
于 2020-09-14T21:36:17.777 回答