Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem finding specific days and dates 1

Status
Not open for further replies.

jamescpp

IS-IT--Management
Aug 29, 2001
70
US
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
 
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
 
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
 
It's a good practice to post you solution so other people can benefit from it

Cheers,
Dian
 
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 "";


}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top