Ninad Pingale
Ninad Pingale

Reputation: 7079

Get All Bi-Weekly dates of year in Java

I want to calculate bi weekly dates of a year. So I should get 2 dates per month.

I have tired it so far -

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

But it is not giving correct dates, it gives following output -

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

How can I get dates like this -

2019-01-01
2019-01-16
2019-02-01
2019-02-16
.
.

Upvotes: 2

Views: 1038

Answers (4)

Eritrean
Eritrean

Reputation: 16498

You can use lastDayOfMonth() method from TemporalAdjusters. Example:

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

}

Upvotes: 0

Ninad Pingale
Ninad Pingale

Reputation: 7079

For others having same requirement in future-

Using this

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

I got what I needed -

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

Upvotes: 0

Oleg Cherednik
Oleg Cherednik

Reputation: 18255

public static List<LocalDate> getAllBiWeeks(int year) {
    List<LocalDate> res = new ArrayList<>(12 * 2);

    for (int month = 1; month <= 12; month++) {
        res.add(LocalDate.of(year, month, 1));
        res.add(LocalDate.of(year, month, 16));
    }

    return res;
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502016

I want to calculate bi weekly dates of a year. So I should get 2 dates per month.

No, not necessarily. There are 52 weeks in a year (plus a bit) but only 12 months. Biweekly - every other week - would give you 26 or 27 dates per year, whereas "2 dates per month" would give you 24 dates per year.

If you want the 1st and 16th (not 15th?) for each month of the year, I'd suggest just doing that in a loop:

for (int month = 1; month <= 12; month++) {
    System.out.println(LocalDate.of(year, month, 1);
    System.out.println(LocalDate.of(year, month, 16);
}

It sounds like that will give you the output that you're looking for - but you should be aware that's not what most people would understand to be "biweekly".

Upvotes: 2

Related Questions