提问人:huy hoang trong 提问时间:10/5/2023 最后编辑:huy hoang trong 更新时间:10/5/2023 访问量:67
期间:周、月、年(农历)、越南语或中文
Period week, month, year in lunar calendar Vietnamese or Chinese
问:
我想显示两个日期之间的整周开始日期。
假设我选择的开始日期为 2023 年 7 月 25 日 - 2023 年 8 月 25 日,那么它应该返回结果:
25 July 2023
2 Aug 2023
9 Aug 2023
16 Aug 2023
23 Aug 2023
如果农历有 lib 句点日、周、月、年越南语或中文
List<LocalDate> weekDates = new ArrayList<>();
LocalDate tmp = LocalDate.of(2023, 7, 25);
LocalDate end = LocalDate.of(2023, 8, 25);
// Loop until we surpass end date
while(tmp.isBefore(end)) {
weekDates.add(tmp);
tmp = tmp.plusWeeks(1);
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd LL yyyy");
for (int i = 0; i < weekDates.size(); i++) {
String formattedString = weekDates.get(i).format(formatter);
System.out.println(formattedString);
}
答:
0赞
Nidheesh R
10/5/2023
#1
在两个给定日期之间生成每周的开始日期时,您的代码看起来基本是正确的。但是,有几点需要注意:
您提供的代码已生成 2023 年 7 月 25 日至 2023 年 8 月 25 日之间每周的所需开始日期。
您正在使用 DateTimeFormatter 来设置输出日期的格式。在您的评论中,您提到了农历周期,但您的代码使用的是公历。如果要将这些日期转换为农历,则需要合并农历库或 API,这可能需要额外的代码。
下面是带有注释的代码,以使其清晰:
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) {
List<LocalDate> weekDates = new ArrayList<>();
LocalDate tmp = LocalDate.of(2023, 7, 25);
LocalDate end = LocalDate.of(2023, 8, 25);
// Loop until we surpass the end date
while (tmp.isBefore(end)) {
weekDates.add(tmp);
tmp = tmp.plusWeeks(1);
}
// Format and print the dates
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM yyyy");
for (int i = 0; i < weekDates.size(); i++) {
String formattedString = weekDates.get(i).format(formatter);
System.out.println(formattedString);
}
}
}
如果需要将这些日期转换为农历,则需要使用特定于越南或中国农历的农历库或 API,因为农历与公历不同。
评论
0赞
Anonymous
10/6/2023
很好,谢谢。要获得 ,如问题所述,请仅使用格式模式字符串中的一个。2 Aug 2023
d
评论