Search code examples
javacalendarjava-8

How can I get list of years, months and weeks in Java?


I am trying to get list of years since epoch i.e 1970 , 1971, 1972, 1973 so on to 2017 , and list of months January - December after this according to month and year generate weeks (based on working days Monday-Friday).

If a month started on Thursday i.e. 2-2-2016

then it should return 2-2-2016, 3-2-2016

and a use case is suppose if a month end day is Tuesday and its date is 30-4-2016

then I should get 29-4-2016, 30-4-2016

so far I am only able to get last working day of same week from input date the code is below:

String Date = "06-04-2012";
DateTimeFormatter f = DateTimeFormatter.ofPattern("YYYY-MM-DD");
LocalDate ld = LocalDate.parse(Date, f);

LocalDate nextOrSameFriday = ld.with(TemporalAdjusters
        .nextOrSame(DayOfWeek.FRIDAY));
String output = nextOrSameFriday.toString();
//String output = ld.format( f );

if (nextOrSameFriday.getMonthValue() != ld.getMonthValue()) {
    nextOrSameFriday = ld.with(TemporalAdjusters
            .nextOrSame(DayOfWeek.THURSDAY));
    if (nextOrSameFriday.getMonthValue() == ld.getMonthValue())
        return nextOrSameFriday.toString();

    else if (nextOrSameFriday.getMonthValue() != ld.getMonthValue()) {
        nextOrSameFriday = ld.with(TemporalAdjusters
                .nextOrSame(DayOfWeek.WEDNESDAY));

        if (nextOrSameFriday.getMonthValue() == ld.getMonthValue())
            return nextOrSameFriday.toString();

        else if (nextOrSameFriday.getMonthValue() != ld.getMonthValue()) {
            nextOrSameFriday = ld.with(TemporalAdjusters
                    .nextOrSame(DayOfWeek.TUESDAY));
            if (nextOrSameFriday.getMonthValue() == ld.getMonthValue())
                return nextOrSameFriday.toString();
            else
                return Date;

        }

    }

}

After the month and year is selected I want following things to do:

  1. Find first day of the Month

  2. Find first Friday of MONTH

  3. Check if its Saturday or Sunday , add 1 or 2 in DATE

  4. Add 2 days in first Friday to get first day of 2nd WEEK.

  5. Add 5 days to get 2nd week Friday

  6. Similarly go on until the end of month


Solution

  • Here's a code snippet:

    private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("d-M-uuuu");
    
    public static void printWorkWeeksOfMonth(String date) {
        LocalDate ld = LocalDate.parse(date, DATE_FORMAT);
        LocalDate lastDayOfMonth = ld.with(TemporalAdjusters.lastDayOfMonth());
        
        // find first working day of month; also first working day of first work week
        LocalDate firstWorkingDay = ld.withDayOfMonth(1);
        if (ld.getDayOfWeek() == DayOfWeek.SATURDAY || ld.getDayOfWeek() == DayOfWeek.SUNDAY) {
            firstWorkingDay = firstWorkingDay.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
        }
        while (! firstWorkingDay.isAfter(lastDayOfMonth)) {
            // find last working day of week beginning on firstWorkingDay
            LocalDate lastWorkingDay = firstWorkingDay.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY));
            if (lastWorkingDay.isAfter(lastDayOfMonth)) {
                // end work week at end of month
                lastWorkingDay = lastDayOfMonth;
            }
            System.out.println(firstWorkingDay.format(DATE_FORMAT)
                    + " through " + lastWorkingDay.format(DATE_FORMAT));
            
            // find beginning of next work week
            firstWorkingDay = firstWorkingDay.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
        }
    }
    

    If I feed 2-2-2016 to the method above, it prints:

    1-2-2016 through 5-2-2016
    8-2-2016 through 12-2-2016
    15-2-2016 through 19-2-2016
    22-2-2016 through 26-2-2016
    29-2-2016 through 29-2-2016
    

    Given 29-4-2016 it prints:

    1-4-2016 through 1-4-2016
    4-4-2016 through 8-4-2016
    11-4-2016 through 15-4-2016
    18-4-2016 through 22-4-2016
    25-4-2016 through 29-4-2016
    

    In other words, the method prints to standard output the work weeks of the month in which date falls. You may want to change the method to accept a YearMonth rather than a String since it doesn’t use the exact date. Or you may want to change it so it uses the exact date somehow.