【问题标题】:Get list of string dates between a given date range - Scala获取给定日期范围之间的字符串日期列表 - Scala
【发布时间】:2021-01-01 14:56:05
【问题描述】:

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

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

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

【问题讨论】:

    标签: java list scala date datetimeformatter


    【解决方案1】:

    是的...如果您以您提供的格式添加破折号,您可以使用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
    

    【讨论】:

    • 你应该使用DateTimeFormatter而不是添加破折号。
    • 我无法导入 java.time.datesUntil。它是否属于差异包名称?
    • 我的错误。这是java.time.LocalDate
    • 我认为 Java 8 中没有 datesUntil
    【解决方案2】:

    你可以这样做:

    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());
        }
    }
    

    【讨论】:

    • @BasicLearner 您能自己将这个答案中的 Java 代码手动翻译成 Scala 吗?算法和方法调用当然也适用于 Scala。
    猜你喜欢
    • 2017-12-20
    • 2020-07-06
    • 2020-06-05
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 2016-10-27
    • 2016-12-10
    • 1970-01-01
    相关资源
    最近更新 更多