0

I have a code that manually calls the last Friday of the month for the year 2020. It is necessary for the code to recognize and call the last Friday in the month of November every year, can anyone help, thank you.

    Calendar lastday = Calendar.getInstance();
    int month = lastday.get(Calendar.MONTH);
    int dayOfMonth = lastday.get(Calendar.DAY_OF_MONTH);
    if(month == Calendar.NOVEMBER && dayOfMonth == 27){
        .....
    }
Cule
  • 117
  • 8

3 Answers3

1

your answer:

Calendar lastday = Calendar.getInstance();
int month = lastday.get(Calendar.MONTH);
int dayOfMonth = lastday.get(Calendar.DAY_OF_MONTH);
int day = lastday.get(Calendar.DAY_OF_WEEK); 
if(month == Calendar.NOVEMBER && dayOfMonth >= 24 && day == Calendar.FRIDAY){
    .....
}
Ali Najafi
  • 118
  • 1
  • 3
0

You can use following solution. By changing calendar.set(Calendar.MONTH, Calendar.NOVEMBER), it should be usable for any month:

public int getLastFridayOfNovember() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.MONTH, Calendar.NOVEMBER);

    int totalDaysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    calendar.set(Calendar.DAY_OF_MONTH, totalDaysInMonth);

    int daysToRollBack = (calendar.get(Calendar.DAY_OF_WEEK) + 1) % 7;
    return totalDaysInMonth - daysToRollBack;
}

Usage is simple:

...
int lastDayOfNovember = getLastFridayOfNovember();

If you use Kotlin, it can be extension function as well:

fun Calendar.getLastFridayOfMonth(): Int {
    val totalDaysInMonth = getActualMaximum(Calendar.DAY_OF_MONTH)
    this[Calendar.DAY_OF_MONTH] = totalDaysInMonth
    val daysToRollBack = (this[Calendar.DAY_OF_WEEK] + 1) % 7
    return totalDaysInMonth - daysToRollBack
}

Usage:

val calendar = Calendar.getInstance()
// set month
calendar.set(Calendar.MONTH, Calendar.NOVEMBER);
// get last Friday of given month
val lastFridayOfNovember = calendar.getLastFridayOfMonth()
Natig Babayev
  • 3,128
  • 15
  • 23
0

java.time either through desugaring or through ThreeTenABP

Always consider using java.time, the modern Java date and time API, for your date work. And even more so for queries that are a bit more complex like yours here.

    LocalDate today = LocalDate.now(ZoneId.systemDefault());
    boolean isLastFridayOfNovember = today.getMonth().equals(Month.NOVEMBER)
            && today.equals(today.with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY)));
    System.out.println("Is it the last Friday in November? " + isLastFridayOfNovember);

Output when I ran today (Thursday November 12):

Is it the last Friday in November? false

The expression today.with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY)) calculates the last Friday of the current month. If today is equal to the calculated date and we are in November, then we’re on the last Friday of November.

To calculate the last Friday in November for a given year:

    int year = 2021;
    LocalDate lastFridayInNovember = LocalDate.of(year, Month.NOVEMBER, 30)
            .with(TemporalAdjusters.previousOrSame(DayOfWeek.FRIDAY));
    
    System.out.println(lastFridayInNovember);

2021-11-26

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161