gpt4 book ai didi

java - 我们可以将 Chronounit 配置为从周六到周五安排一周吗

转载 作者:行者123 更新时间:2023-11-30 01:57:46 25 4
gpt4 key购买 nike

大多数情况下,每周的所有日历实用程序都是在周日/周日或周一/周一配置的。

我想将其配置为周六到周五:

假设我每周累积 11 月份的数据:

var sample = Seq(("C1", "01/11/2018", 20), ("C1", "02/11/2018", 5),
("C1", "03/11/2018", 2), ("C1", "04/11/2018", 3), ("C1", "05/11/2018", 5),
("C1", "08/11/2018", 5), ("C1", "10/11/2018", 10), ("C1", "12/11/2018", 10),
("C1", "14/11/2018", 16), ("C1", "15/11/2018", 6), ("C1", "18/11/2018", 22)).toDF("id", "order_date", "orders")


sample = sample.withColumn(
"order_date",
to_date(unix_timestamp($"order_date", "dd/MM/yyyy").cast("timestamp")))

val Row(start: java.sql.Date, end: java.sql.Date) = sample.
select(min($"order_date"), max($"order_date")).
first

val weeksDF = (0L to ChronoUnit.WEEKS.between(
start.toLocalDate, end.toLocalDate)).
map(i => java.sql.Date.valueOf(start.toLocalDate.plusWeeks(i))).
toDF("order_date")



Desired Output:

02/11/2018 25
09/11/2018 15
16/11/2018 42
23/11/2018 22

非常感谢早期的帮助。

最佳答案

我认为你可以用更简单的方式做到这一点,你只需要获取下周五并将所有日期汇总到下周五,这样你就可以为每个日期获取下周五,如下所示:

val localDate = LocalDate.now() // your date here
val nextFriday = localDate.`with`(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY))

然后您将在下周五添加一个新列并按其分组并对计数求和

关于java - 我们可以将 Chronounit 配置为从周六到周五安排一周吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53802138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com