scala - 行之间的scala循环
问题描述
我有这样的事情:
NonWorkingDays =Seq(("2021-01-01"),("2021-01-02"),("2021-01-03"),("2021-01-06"),("2021-01-09"),
("2021-01-10"),("2021-01-16"),("2021-01-17"),("2021-01-23"),("2021-01-24"),
("2021-01-30"),("2021-01-31")).toDF("festivos")
东风:
|Starts | End |Resta|
+-----------+----------+-----+
| 2021-01-04|2021-01-05| 1|
| 2021-01-11|2021-01-14| 3|
+-----------+----------+-----+
如何在 Starts > End 之间进行循环?我的意思是:
for (a <- "2021-01-04" to "2021-01-10") println(a)
解决方案
I do not know Scala
but I believe anyone who knows Scala
also knows Java
. Given below is how it can be done in Java
:
import java.time.LocalDate;
import java.time.Period;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
List<LocalDate> nonWorkingDays = Stream
.of("2021-01-01", "2021-01-02", "2021-01-03", "2021-01-06", "2021-01-09", "2021-01-10", "2021-01-16",
"2021-01-17", "2021-01-23", "2021-01-24", "2021-01-30", "2021-01-31")
.map(LocalDate::parse) // LocalDate#parse
.sorted() // Remove it if you do not need to sort the list
.collect(Collectors.toList());
for (int i = 0; !nonWorkingDays.get(i).isAfter(LocalDate.parse("2021-01-10")); i++) {
System.out.println(nonWorkingDays.get(i));
}
// Days between 2021-01-11 and 2021-01-14
int days = Period.between(LocalDate.parse("2021-01-11"), LocalDate.parse("2021-01-14")).getDays();
System.out.println(days);
}
}
Output:
2021-01-01
2021-01-02
2021-01-03
2021-01-06
2021-01-09
2021-01-10
3
推荐阅读
- php - 如何在不更改服务器主机名和路由的情况下将现有 Web 项目移动到 Laravel
- node.js - 为什么 Node RxJS .subscribe() 没有被 bindNodeCallback() 触发?
- hyperledger - 如何在没有交易处理器功能的情况下自动建立资产关系?
- xml - 离线时使用外部xsd文件解析tomcat web.xml
- android - Android:如何在两个测试代码上使用 Mock?
- jmeter - 在执行基于云的远程主机期间,是否有任何 jmeter 插件可用于显示统计信息?
- vim - 使用 Vim 扩展时如何在 Esc 上的 VSCode 中自动保存文件?
- git - 如何调用 git show head:
在jgit? - java - 我如何正确调用我创建的方法以便我能够在另一个中使用它?
- javascript - 增加一个值的问题 它可能是什么?