-2

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

Nimantha
  • 6,405
  • 6
  • 28
  • 69
developer
  • 105
  • 1
  • 3
  • 11
  • It's a bit hard to understand what you want to do. Do you want to get the years since epoch? If so, why? There are libraries like Joda or even `Calendar` which might already do what you want, no need create those functions yourself. So, can you elaborate more what you want to do? – hamena314 May 03 '17 at 10:33
  • http://stackoverflow.com/questions/6158053/get-the-number-of-days-weeks-and-months-since-epoch-in-java – hamena314 May 03 '17 at 10:33
  • no i want to do this.. . 1970 , 1971, 1972, 1973 so on to 2017 and for months january - december list ( i hardcoded this anyways) then calculating week – developer May 03 '17 at 10:48
  • i solved years list and months list problem. the only one left now is for calculating weeks – developer May 03 '17 at 10:56
  • `date is 4-4-2016 ... then i should get 3-4-2016, 4-4-2016`. Could you explain, why you should get `3-4-2016, 4-4-2016` because I still dont understand what you are trying to accomplish? – hamena314 May 03 '17 at 11:23
  • becuase these two are last days of month and they are working days as well i.e. between monday-friday are working days.. next month will have wednesday-friday working days so these three days should be considered in next month – developer May 03 '17 at 12:49
  • So you want the work weeks from the first of the month up to the date given, is that it? Excuse me, it’s still unclear. I’m getting confused because 4-4-2016 is not the last day of the month, there are 30 days in April. – Ole V.V. May 03 '17 at 13:37
  • 1
    oh man Sorry, although i solved ALL OF it, but i was wondrin why i was getting so many down votes i messed up the dates while writing here. – developer May 03 '17 at 17:04

1 Answers1

2

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.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161