为了账号安全,请及时绑定邮箱和手机立即绑定

在 Java 中获取一年中的所有双周日期

在 Java 中获取一年中的所有双周日期

PIPIONE 2022-05-21 14:05:16
我想计算一年的双周日期。所以我每个月应该有 2 次约会。到目前为止我已经厌倦了-private static void getAllBiWeeks() {        int lastYear = java.time.Year.now().getValue() - 1;        int currentYear = java.time.Year.now().getValue();        int nextYear = java.time.Year.now().getValue() + 1;        System.out.println("lastYear - " + lastYear);        System.out.println("currentYear - " + currentYear);        System.out.println("nextYear - " + nextYear);        LocalDate lastYearDate = LocalDate.of(lastYear, 12, 01);        LocalDate currentYearFirstDate = LocalDate.of(currentYear, 01, 01);        LocalDate currentYearLastDate = LocalDate.of(currentYear, 12, 31);        LocalDate nextYearFirstDate = LocalDate.of(nextYear, 01, 01);        System.out.println("lastYearDate - " + lastYearDate);        System.out.println("currentYearFirstDate - " + currentYearFirstDate);        System.out.println("nextYearFirstDate - " + nextYearFirstDate);        LocalDate lastYearLastDate = LocalDate.of(lastYear, 12, 31);        LocalDate lastYearLast15Days = lastYearLastDate.minusWeeks(2);        System.out.println("lastYearLast15Days - " + lastYearLast15Days);        System.out.println(lastYearLast15Days.isBefore(nextYearFirstDate));        for (LocalDate date = currentYearFirstDate; date.isBefore(currentYearLastDate); date = date.plusWeeks(2)) {            System.out.println(date);        }    }但它没有给出正确的日期,它给出了以下输出 -lastYear - 2018currentYear - 2019nextYear - 2020lastYearDate - 2018-12-01currentYearFirstDate - 2019-01-01nextYearFirstDate - 2020-01-01lastYearLast15Days - 2018-12-17true2019-01-012019-01-152019-01-292019-02-122019-02-262019-03-122019-03-262019-04-092019-04-232019-05-072019-05-212019-06-042019-06-182019-07-022019-07-162019-07-302019-08-132019-08-272019-09-102019-09-242019-10-082019-10-222019-11-052019-11-192019-12-032019-12-17我怎样才能得到这样的日期 -2019-01-012019-01-162019-02-012019-02-16..
查看完整描述

3 回答

?
HUWWW

TA贡献1874条经验 获得超12个赞

我想计算一年的双周日期。所以我每个月应该有 2 次约会。


不,不一定。一年有 52 周(加上一点),但只有 12 个月。双周 - 每隔一周 - 每年会给你 26 或 27 个日期,而“每月 2 个日期”会给你每年 24 个日期。


如果您想要一年中每个月的第 1 和第 16(不是第 15?),我建议您循环执行此操作:


for (int month = 1; month <= 12; month++) {

    System.out.println(LocalDate.of(year, month, 1);

    System.out.println(LocalDate.of(year, month, 16);

}

听起来这将为您提供您正在寻找的输出 - 但您应该意识到这不是大多数人所理解的“双周刊”。


查看完整回答
反对 回复 2022-05-21
?
炎炎设计

TA贡献1808条经验 获得超4个赞

您可以使用lastDayOfMonth()方法 from TemporalAdjusters。例子:


import java.time.LocalDate;

import static java.time.temporal.TemporalAdjusters.lastDayOfMonth;


public class NewClass2 {


    public static void main(String[] args) {

        LocalDate currentYearFirstDate =  LocalDate.of(2019, 01, 01);

        LocalDate currentYearLastDate  =  LocalDate.of(2019, 12, 31);

        for (LocalDate date = currentYearFirstDate; date.isBefore(currentYearLastDate);date = date.plusMonths(1)) { 

            LocalDate firstHalfStart = date;

            LocalDate firstHalfEnd   = date.plusDays(14);

            LocalDate secondHalfStart = date.plusDays(15);

            LocalDate secondHalfEnd   = date.with(lastDayOfMonth());

            System.out.println(firstHalfStart + " to " + firstHalfEnd + " , " + secondHalfStart + " to " + secondHalfEnd);

        }

    }


}


查看完整回答
反对 回复 2022-05-21
?
墨色风雨

TA贡献1853条经验 获得超6个赞

对于将来有相同要求的其他人-


使用这个


for (int month = 1; month <= 12; month++) {

            int lastDayOfMonth = YearMonth.of(currentYear, month).lengthOfMonth();

            System.out.println(LocalDate.of(currentYear, month, 1)+" To "+LocalDate.of(currentYear, month, 15)+" , "+LocalDate.of(currentYear, month, 16)+" To "+LocalDate.of(currentYear, month, lastDayOfMonth));

}

我得到了我需要的——


2019-01-01 To 2019-01-15 , 2019-01-16 To 2019-01-31

2019-02-01 To 2019-02-15 , 2019-02-16 To 2019-02-28

2019-03-01 To 2019-03-15 , 2019-03-16 To 2019-03-31

2019-04-01 To 2019-04-15 , 2019-04-16 To 2019-04-30

2019-05-01 To 2019-05-15 , 2019-05-16 To 2019-05-31

2019-06-01 To 2019-06-15 , 2019-06-16 To 2019-06-30

2019-07-01 To 2019-07-15 , 2019-07-16 To 2019-07-31

2019-08-01 To 2019-08-15 , 2019-08-16 To 2019-08-31

2019-09-01 To 2019-09-15 , 2019-09-16 To 2019-09-30

2019-10-01 To 2019-10-15 , 2019-10-16 To 2019-10-31

2019-11-01 To 2019-11-15 , 2019-11-16 To 2019-11-30

2019-12-01 To 2019-12-15 , 2019-12-16 To 2019-12-31


查看完整回答
反对 回复 2022-05-21
  • 3 回答
  • 0 关注
  • 271 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信