INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Problem finding specific days and dates

Problem finding specific days and dates

(OP)
I'm working on a program for a local group of guys I play basketball with. Basically something to help us know who's committing to playing then next two dates, instead of sending emails around. Anyway, I'm having trouble getting my program to automatically determine the Friday and Monday correctly.

For example. If it's Tuesday, Wednesday, or Thursday, I want to know the Date of the upcoming Friday and Monday. If today is TOMORROW, something like:
Friday, April 1st 2011 and Monday, April 4th 2011

If it's a Friday I want to know the date of that Friday and following Monday. So if's Friday April 1st I want to know:
Friday, April 1st 2011 and Monday, April 4th 2011

If it's Saturday, Sunday, or Monday, I want to know the LAST Friday and Monday. So if it's Monday April 4th, I want to know:
Friday, April 1st 2011 and Monday, April 4th 2011

I've been messing around with Calendar and Date, and seemed to get close with the Date class, but my results seemed kind of screwy. One day it would seem to work, the next I would be a day off.

Can anyone help with any sample code?

Thanks,
James

RE: Problem finding specific days and dates

What did you try so far? What is working and what is not? What is the logic behind the examples (if any)? I cannot see any way to do that but a bunch of ifs

Cheers,
Dian

RE: Problem finding specific days and dates

(OP)
I was able to get this working. Like you said, it took case statements and if statements and just dealing with the logic.

Thanks,
James

RE: Problem finding specific days and dates

It's a good practice to post you solution so other people can benefit from it

Cheers,
Dian

RE: Problem finding specific days and dates

(OP)
Agreed. Here's my code for finding the the upcoming Friday and Monday. (Or previous Friday if the current day is Saturday, Sunday, or Monday)

James

Call with:
String fridayDateString = getDate("FRIDAY");
String mondayDateString = getDate("MONDAY");

private String getDate(String requestedDay)
{                    
    int fridayInt = 0;                         
    int mondayInt = 0;
    Calendar now = Calendar.getInstance();  
    int today = now.get(Calendar.DAY_OF_YEAR);
    int todayDAY = now.get(Calendar.DAY_OF_WEEK);
    int todayWEEK = now.get(Calendar.WEEK_OF_YEAR);
    int todayDAYOFMONTH = now.get(Calendar.DAY_OF_MONTH);
    int todayYEAR = now.get(Calendar.YEAR);

    String dayString;
    switch (todayDAY)
    {
        case 1:  
            dayString = "Sunday";
            mondayInt = today + 1;
            fridayInt = today - 2;      
            break;
        case 2:  
            dayString = "Monday";
            mondayInt = today;
            fridayInt = today - 3;
            break;
        case 3:  
            dayString = "Tuesday";
            mondayInt = today + 6;
            fridayInt = today + 3;         
            break;
        case 4:  
            dayString = "Wednesday";  
            mondayInt = today + 5;
            fridayInt = today + 2;       
            break;
        case 5:      
            dayString = "Thursday";
            mondayInt = today + 4;
            fridayInt = today + 1;           
            break;
        case 6:  
            dayString = "Friday";
            mondayInt = today + 3;          
            fridayInt = today + 0;
            break;
        case 7:  
            dayString = "Saturday";
            mondayInt = today + 2;        
            fridayInt = today - 1;  
            break;
        default:
            dayString = "Invalid day";
            break;
    }


    now.clear();
    now = Calendar.getInstance();

    //deal with negative Friday meaning Friday is in the previous year, like December 30th
    if ((requestedDay.equals("FRIDAY")) && fridayInt < 1)
    {
        now.set(Calendar.YEAR, todayYEAR - 1);
        fridayInt = fridayInt + now.getActualMaximum(Calendar.DAY_OF_YEAR);
    }

    //deal with Monday going into next year, like Monday is January 2nd
    if ((requestedDay.equals("MONDAY")) &&  now.getActualMaximum(Calendar.DAY_OF_YEAR) < mondayInt)
    {
        int maxDaysInCurrentYear = now.getActualMaximum(Calendar.DAY_OF_YEAR);
        now.set(Calendar.YEAR, todayYEAR + 1);
        mondayInt = maxDaysInCurrentYear - mondayInt;

    }

    //set calendar based on upcoming Monday's day of the year
    if (requestedDay.equals("MONDAY"))
        now.set(Calendar.DAY_OF_YEAR, mondayInt);
    else if (requestedDay.equals("FRIDAY"))
        now.set(Calendar.DAY_OF_YEAR, fridayInt);
    int todayMONTH = now.get(Calendar.MONTH);

    String monthString;
    switch (todayMONTH)
    {
        case 0:  
            monthString = "January";       
            break;
        case 1:  
            monthString = "February";
            break;
        case 2:  
            monthString = "March";         
            break;
        case 3:  
            monthString = "April";         
            break;
        case 4:      
            monthString = "May";           
            break;
        case 5:  
            monthString = "June";          
            break;
        case 6:  
            monthString = "July";          
            break;
        case 7:  
            monthString = "August";       
            break;
        case 8:  
            monthString = "September";
            break;
        case 9:  
            monthString = "October";         
            break;
        case 10:  
            monthString = "November";         
            break;
        case 11:      
            monthString = "December";           
            break;
        default:
            monthString = "Invalid month";
            break;
    }



    Calendar calendar = Calendar.getInstance();

    if (requestedDay.equals("MONDAY"))
        calendar.set(Calendar.DAY_OF_YEAR, mondayInt);
    else if (requestedDay.equals("FRIDAY"))
        calendar.set(Calendar.DAY_OF_YEAR, fridayInt);


    if (requestedDay.equals("MONDAY"))
        return "Monday" + ", " + monthString + " " + calendar.get(Calendar.DATE) + ", " + calendar.get(Calendar.YEAR);
    else if (requestedDay.equals("FRIDAY"))
        return "Friday" + ", " + monthString + " " + calendar.get(Calendar.DATE) + ", " + calendar.get(Calendar.YEAR);

    return "";
 

}

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close