0

How can I display the dates in a month or year for which the name of the day is Friday, in Java or JavaScript?

For example, for the month of December 2011, the code would display:

  • 2/12/2011
  • 9/12/2011
  • 16/12/2011
  • 23/12/2011
  • 30/12/2011
Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
Raja Seth
  • 27
  • 2
  • 5
    [Java](http://stackoverflow.com/tags/java/info) **or** [JavaScript](http://stackoverflow.com/tags/javascript/info)? They're both *very* different things. – Matt Dec 20 '11 at 10:18
  • @Matt: sure, but the chap might be doing a web app with Java on the server, and not mind whether the display happens on the server or the client. – Paul D. Waite Dec 20 '11 at 10:21
  • Do you know how to display in java? Please let me know. – Raja Seth Dec 20 '11 at 10:22
  • Possible duplicate: http://stackoverflow.com/questions/4547768/joda-time-how-to-get-dates-of-weekdays-on-some-date-interval – Abhinav Sarkar Dec 20 '11 at 10:25
  • 4
    Java to Javascript is same as Car as to Carpet. – Ajinkya Dec 20 '11 at 10:29

8 Answers8

4

Here's my optimal Javascript attempt.

It works by determining algorithmically what the date of the first Friday of the month is, then simply goes forward 7 days at a time until the month has ended.

// find all dates that fall on a particular day of the week
// for the given year and month

function getDaysOfMonth(year, month, dow) {
    --month;                                    // to correct for JS date functions
    var d = new Date(year, month, 1);           // get the first of the month
    var dow_first = d.getDay();                 // find out what DoW that was 
    var date = (7 + dow - dow_first) % 7 + 1;   // and the first day matching dow

    var dates = [];
    d.setDate(date);
    do {
        dates.push(new Date(d));      // store a copy of that date
        date += 7;                    // go forward a week
        d.setDate(date);            
    } while (d.getMonth() === month); // until the end of the month

    return dates;
}

Demo, for Fridays (which is day 5 in JS format):

document.write(getDaysOfMonth(2011, 12, 5).join("<br>"));

See http://jsfiddle.net/wLFmM/ for full working demo.

To get the dates for a whole year, just call it 12 times, and merge the results ;-)

Alnitak
  • 334,560
  • 70
  • 407
  • 495
1

In Java:

for (int i = 1; i <= 31; i++) {
    Calendar cal = new GregorianCalendar(2011, Calendar.DECEMBER, i);
    if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
         // process date
    }
}

In Javascript:

for (i = 1; i <= 31; i++) {
    var date = new Date(2011, 11, i);
    if (date.getDay() == 5) {
         // process date
    }
}

Edit:

A simple logic of jumping in response to comments:

int step = 1;
for (int i = 1; i <= 31; i += step) {
    // ...
    if (.....) {
        step = 7;
        // .....
    }
}
Jomoos
  • 12,823
  • 10
  • 55
  • 92
1

In Javascript,

function getFridays(year,month){ 
    var fridays=new Array();
    var i=0;
        var tdays=new Date(year, month, 0).getDate();
        for(date=0;date<=tdays;date++)     {
            sdate=(date<10)?"0"+date:date; 
            dd=(month+1)+"/"+sdate+"/"+year;  
            var day=new Date(year,month,date); 
            if(day.getDay() == 5 )  
            {  
                fridays[i++]=dd;
            }    
        }   
        return fridays; 
    } 
Selvakumar Ponnusamy
  • 5,363
  • 7
  • 40
  • 78
  • oh dear - `a = new Array()` _and_ `a[i++] = n`?! It should be `a = []` and `a.push(n)` ... Just sayin' – Alnitak Dec 20 '11 at 13:59
0

Something like the following:

new SimpleDateFormat("yyyy-MM-dd EEEE", Locale.US).format(date));

for example for Dec, 19 it prints: 2011-12-19 Monday

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 3
    If I'm not mistaken he'd like to know all fridays of december for example. Not the day of a specific date. – Termi Dec 20 '11 at 10:26
0

In java you could use java.util.Calendar class.

Calendar c = Calendar.newInstance();
c.setTime(System.currentTimeInMills());
if (c.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
  .. do your stuff here
}

Also you can iterate over the days:

Calendar c = Calendar.newInstance();
c.add(1, Calendar.DAY);
Valchev
  • 1,490
  • 14
  • 17
0

Here’s my JavaScript attempt. I suspect it’s horribly inefficient, and you have to supply the month the way JavaScript expects it, i.e. January is 0, December is 11.

function showFridays(year, month) {
    var dates = [];

    var months = [];

    if(!month){ 
        for(var i=0; i<12; i++) {
            months.push(i);
        }
    }
    else {
        months.push(month);
    }

    for(var i=0; i<months.length; i++){
        var month_number = months[i];

        for(var j=1; j<32; j++) {
            var date = new Date(year, month_number, j)

                // Because e.g. new Date(2011,2,31) will evaluate to 3rd March, check that date.getMonth() returns the same number as we passed in, so that we reject duplicate dates.

            if(date.getDay() == 5 && date.getMonth() == month_number) {
                dates.push(date);
            }
        }
    }

    for(var i=0; i<dates.length; i++) {
        console.log(dates[i].toLocaleString());
    }
}
Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
0

Define this function:

function getFridays(month, year){
    var ret = [];
    for(var i = 1; i <= 31; i++){
        var date = new Date();
        date.setDate(i);
        date.setMonth(month - 1);
        date.setFullYear(year);
        if(date.getDay() === 5){
            var today = date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear();
            ret.push(today);

        }
    }
    return ret;
}

And then call it, say for December, like this:

document.write(getFridays(12, 2011));

There you have a JavaScript solution.

Demo.


Edit: Updated logic according to a comment on another solution:

function getFridays(month, year){
    var ret = [];
    for(var i = 1; i <= 7; i++){
        var date = new Date();
        date.setDate(i);
        date.setMonth(month - 1);
        date.setFullYear(year);
        if(date.getDay() === 5){
            var today = date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear();
            ret.push(today);
            for(var j = 1; j < 5; j++){
                var d = date.getDate() + j * 7;
                if(d <= 31){
                    ret.push((d) + '/' + (date.getMonth() + 1) + '/' + date.getFullYear());
                }
            }
        }
    }
    return ret;
}

Basically, what that does is, it looks for the first Friday of the month, and then simply adds the other Fridays by adding 7 to them, till you're out for the month. Note that I haven't added any logic to check if anything less than 31 days is valid or not (for like half the months). You may want to improve on the second function if you do use it.

Some Guy
  • 15,854
  • 10
  • 58
  • 67
0

Here is a example in Java, works for the month of December 2011. Change the dates to suit your use case:

Calendar beginCalendar = Calendar.getInstance();        
//initialize the date to the first day the desired year and month
//Below example initializes to 1st of December 2011
beginCalendar.set(2011,Calendar.DECEMBER,1);            
Calendar endCalendar = Calendar.getInstance();
//set the end date to the end of the month using the begin date
endCalendar.set(beginCalendar.get(Calendar.YEAR),beginCalendar.get(Calendar.MONTH),beginCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));

//loop through till we hit the first friday of the month
while(beginCalendar.get(Calendar.DAY_OF_WEEK) != Calendar.FRIDAY){
    beginCalendar.add(Calendar.DATE,1);             
}

//loop from the first friday of the month till the end of month and add 1 week in each iteration. 
while (beginCalendar.compareTo(endCalendar) <= 0) {             
    System.out.println(beginCalendar.getTime().toString());//this prints all the fridays in the given month
    beginCalendar.add(Calendar.WEEK_OF_YEAR, 1);                
}
Suresh Kumar
  • 11,241
  • 9
  • 44
  • 54