Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,888 questions

51,815 answers

573 users

How to calculate the number of weekdays in the current year with Java

2 Answers

0 votes
import java.time.LocalDate;
import java.time.DayOfWeek;

public class Main {
    public static void main(String[] args) {
        int year = LocalDate.now().getYear();
        int weekdayCount = countWeekdaysInYear(year);
        
        System.out.println("Number of weekdays in " + year + ": " + weekdayCount);
    }

    static int countWeekdaysInYear(int year) {
        int weekdayCount = 0;

        LocalDate date = LocalDate.of(year, 1, 1);
        while (date.getYear() == year) {
            if (date.getDayOfWeek() != DayOfWeek.SATURDAY && date.getDayOfWeek() != DayOfWeek.SUNDAY) {
                weekdayCount++;
            }
            date = date.plusDays(1);
        }

        return weekdayCount;
    }
}

   
   
/*
run:
   
Number of weekdays in 2025: 261
  
*/

 



answered Feb 18, 2025 by avibootz
0 votes
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Year;

public class Main {
    public static void main(String[] args) {
        int currentYear = Year.now().getValue();
        LocalDate startDate = LocalDate.of(currentYear, 1, 1);
        LocalDate endDate = LocalDate.of(currentYear, 12, 31);

        int weekdayCount = 0;

        for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
            DayOfWeek dayOfWeek = date.getDayOfWeek();
            if (dayOfWeek != DayOfWeek.SATURDAY && dayOfWeek != DayOfWeek.SUNDAY) {
                weekdayCount++;
            }
        }

        System.out.println("Number of weekdays in " + currentYear + ": " + weekdayCount);
    }
}

   
   
/*
run:
   
Number of weekdays in 2025: 261
  
*/


 



answered Feb 18, 2025 by avibootz

Related questions

1 answer 109 views
1 answer 66 views
1 answer 68 views
2 answers 62 views
1 answer 76 views
1 answer 68 views
...