2

Hi want to exclude weekend from month please help me.
But this is not Exclude weekend from month how to Exclude Weekend.

 <%     
    Calendar cal  = Calendar.getInstance();                                               int  day = cal.get(cal.DAY_OF_WEEK);  
    int days=day-1;
    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    Date date1 = df.parse("1/02/2014");
    Date date2 = new Date();    
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(date1);
    cal2.setTime(date2);

    int numberOfDays = 0;
    while (cal1.before(cal2)) {
        if ((Calendar.SATURDAY != cal1.get(Calendar.DAY_OF_MONTH))&&(Calendar.SUNDAY != cal1.get(Calendar.DAY_OF_MONTH))) 
        {
            numberOfDays++;
          session.setAttribute("numberOfDays",numberOfDays);
            cal1.add(Calendar.DATE,1);
        }else {
            cal1.add(Calendar.DATE,1);
        }

    }           

    %>
StoopidDonut
  • 8,547
  • 2
  • 33
  • 51
Deepak Singh
  • 460
  • 2
  • 7
  • 19
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Feb 03 '18 at 07:09

2 Answers2

1

You need to compare with Calendar.DAY_OF_WEEK instead of Calendar.DAY_OF_MONTH

So your condition becomes:

if ((Calendar.SATURDAY != cal1.get(Calendar.DAY_OF_WEEK))
                    && (Calendar.SUNDAY != cal1.get(Calendar.DAY_OF_WEEK))) {
StoopidDonut
  • 8,547
  • 2
  • 33
  • 51
0

java.time

The modern approach uses the java.time classes.

The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate ld = LocalDate.of( 2014 , Month.JANUARY , 24 ) ;

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" ) ;  
LocalDate today = LocalDate.now( z ) ;

Define a collection of the day-of-week values that we care about, that we want to include in our results. An EnumSet is a highly-optimized implementation of Set for collecting enum objects. So we can collect DayOfWeek objects to define your desired days, weekdays without Saturday & Sunday.

Set < DayOfWeek > weekDays = EnumSet.range( DayOfWeek.MONDAY , DayOfWeek.FRIDAY ) ;  // Not Saturday-Sunday.

Loop your days, increment to the following day, and test the day-of-week of each date.

List < LocalDate > dates = new ArrayList<>() ;

LocalDate target = ld ;
while( target.isBefore( today ) ) {
    DayOfWeek dow = target.getDayOfWeek() ;  // Determine the day-of-week of this date.
    if( weekDays.contains( dow ) {
        dates.add( target ) ;
    }
    target = target.plusDays( 1 ) ; // Setup for the next loop. Increment for next day.
}

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154