获取Java中的所有每两周一次的日期

获取Java中的所有每两周一次的日期,java,Java,我想计算一年中每两周的日期。所以我每个月应该有两次约会 到目前为止我已经厌倦了- 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().getValu

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

到目前为止我已经厌倦了-

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 - 2018
currentYear - 2019
nextYear - 2020
lastYearDate - 2018-12-01
currentYearFirstDate - 2019-01-01
nextYearFirstDate - 2020-01-01
lastYearLast15Days - 2018-12-17
true
2019-01-01
2019-01-15
2019-01-29
2019-02-12
2019-02-26
2019-03-12
2019-03-26
2019-04-09
2019-04-23
2019-05-07
2019-05-21
2019-06-04
2019-06-18
2019-07-02
2019-07-16
2019-07-30
2019-08-13
2019-08-27
2019-09-10
2019-09-24
2019-10-08
2019-10-22
2019-11-05
2019-11-19
2019-12-03
2019-12-17
我怎样才能得到这样的约会-

2019-01-01
2019-01-16
2019-02-01
2019-02-16
.
.
我想计算一年中每两周的日期。所以我每个月应该有两次约会

不,不一定。一年有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);
}
for(int-month=1;month
public静态列表getAllBiWeeks(int-year){
List res=新阵列列表(12*2);

对于(int month=1;month对于将来有相同要求的其他人-

用这个

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

您可以使用
temporaladjusts
中的
lastDayOfMonth()
方法。示例:

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

}

为什么你会期待这些日期?我会期待你目前得到的日期:从某个特定日期开始,然后两周后,然后两周后等等。为什么你期待1月15日后的2月1日?为什么你期待2月1日后的2月28日?(这几乎是4周后!)如果你能更清楚地描述你的目标,会更容易帮助你。更正了问题的预期答案。谢谢。我每个月需要两个日期,每个两周开始。所以2019-01-01,2019-01-16,2019-02-01,2019-02-16等等。我使用的是
date.plusWeeks(2)
但它并没有给出我所期望的。在每种情况下,听起来你都想要“月初,然后是月16日”。代码正是按照你现在的要求执行的——1月29日加上两周是2月12日——但这不是你真正想要的。你所描述的不是我所说的“双周日期”-您当前的输出是我所说的双周日期。一年内您将有24个日期,而双周日期将有26个。@NinaBingle:您所说的“并利用每个月的最后一个日期”是什么意思?目前您的要求非常不明确。
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
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);
        }
    }

}