JSP: How to display a row of seven days and be able to step up/down?
Hi fellow coders and Java-gurus :-)
I'm developing a JSP-based TV-guide, based upon XMLtv data, stored in a (MS) SQL database.
Currently I'm only displaying tv listings for todays date, but I would like for the user to be able to select from 7 dates displayed in a row on the page, and able to step down and up thru dates in the past and future.
Looking something like this:
[ < ] [Yesterday] [Today] [Tomorrow] [5/10 Thu] [5/11 Fri] [5/12 Sat] [5/13 Sun] [ > ]
The < > arrows are used to step down/up one date, so if the user pressed the '>' arrow the dates displayed would be:
[ < ] [Today] [Tomorrow] [5/10 Thu] [5/11 Fri] [5/12 Sat] [5/13 Sun] [5/14 Mon] [ > ]
(and the other way around if pressed the '<' arrow.)
I really have no clue as to how I can accomplish this. I know of Calendar, Date and GregorianCalendar, and are using them in other areas, but this got me beaten.
Any help is greatly appreciated!
Thank you!
[1020 byte] By [
kaderuda] at [2007-11-27 3:45:43]

# 1
Do you want to be able to just 'scroll' the dates till the user clicks on one?
Or do you want the page to submit and display the data for the next-in-line date?
If it's the first, and I personally would prefer that and also think that you do mean that, then you would need to look into JavaScript since that'll be at the client side....
# 2
I hadn't actually thought of using JavaScript, although it might be a good idea, but I'm not really sure I want to rely too much on client-side scripts.
I was actually thinking of submitting the page to itself and reload with the next (or previous) date if the user clicked on any of the < > arrows, or just display the tv listing for the date if the user clicked on any of the dates in the rowlist, with a normal <a href>-tag.
# 3
But in that case, if you wanted to see the listing for say, next sunday, you'd have to first click on one of the weekdays before the option would be available for use; that would be irritating.
But if you do want to submit the page to itself, then it should be easy. You can generate the links ( your < and > arrow's are links right? And also the days? ) with the corresponding parameters so that when the user click's on say, >, then the link would have the parameters "?startdate=10-May-2007" as part of the query string ( which would basically be one more than the first date, [Yesterday], that you'd printed the last time.
And the day could have the date coded into the query string in a similar way "?showdetailsfordate=10-May-2007"
In your JSP, you'd try to request.getParameter("startdate"), if that is null, you'd check for request.getParameter("showdetailsfordate") and if that too is null, then you'd know that the page was being called by some other way and not with by clicking any of these links; so you could serve your default page.
If you did get startdate, you'd start printing the dates from that particular date onwards.
If you got showdetails for date, you'd show the listing for that day ( and perhaps set that date as [Today] and so recalculate all the other links? )
Let me know if you need more clarification; but it seems simple enough to me.
# 4
I'm afraid you've misunderstood me, or I wasn't clear enough as to what my problem is.
I know how to handle the request-objects and check for null or empty values.
My problem is with how to implement the logic for the retrieval of the dates.
I have no problem doing a for()-loop and getting the next days, but I run into problems when I want to find and print yesterday, and the day before that.
To make it easy for me I could just allow todays date and dates in the future, using something like this (example not in jsp):
Locale loc = new Locale("en", "US");
GregorianCalendar gCal = new GregorianCalendar();
Calendar myCal = gCal.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd EEE",loc);
for (int i=1; i<=7; i++) {
System.out.println("("+sdf.format(myCal.getTime())+")");
myCal.add(Calendar.DAY_OF_MONTH, 1);
}
But I for one know of times where I'd like to check a tv-show that was aired in the past, so I'd really like to get this working :-)
# 5
To find yesterday you want to subtract one day from today. The Calendar class doesn't have a subtract method but you can add -1. Was that what was holding you up?
# 6
No, that wasn't my problem.
Could you give me an example that would for example output the following strings with dates formatted:
Yesterday (09/05/2007)
Today (10/05/2007)
Tomorrow (11/05/2007)
Sat (12/05/2007)
Sun (13/05/2007)
Mon (14/05/2007)
Tue (15/05/2007)
I tried adding/subtracting Calendar.DAY_OF_MONTH in my for() loop but when it came to dates in the past, it got real screwed up since it would also print Todays date (using Calendar after/before/equals).
# 7
I'm not really sure what you want, but
Could you give me an example that would for example output the following strings with dates formatted:
Yesterday (09/05/2007)
Today (10/05/2007)
Tomorrow (11/05/2007)
Sat (12/05/2007)
Sun (13/05/2007)
Mon (14/05/2007)
Tue (15/05/2007)
This should do what you asked:
public class DateTest
{
public static void main(String [] args)
{
Locale loc = new Locale("en", "US");
GregorianCalendar gCal = new GregorianCalendar();
Calendar myCal = gCal.getInstance();
//clear the time part so that we can compare only dates.
myCal.set(Calendar.HOUR_OF_DAY, 0);
myCal.set(Calendar.MINUTE, 0);
myCal.set(Calendar.SECOND, 0);
myCal.set(Calendar.MILLISECOND, 0);
Calendar today = gCal.getInstance();
//clear the time part in this too so we can compare when it is today
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
//Set myCal to start off with yesterday, for use in the loop
myCal.add(Calendar.DAY_OF_MONTH, -1);
//formatter for the date in the form 31/12/2007
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyy",loc);
//formatter to print out the day of the week in case it's not Today or Yesterday
SimpleDateFormat dayOfTheWeek = new SimpleDateFormat("EEE", loc);
String finalOutput = ""; //to store the partial string and build it up till we get the complete outupt
for (int i=1; i<=7; i++)
{
if ( myCal.before(today)) //since you're starting with today and subtracting 1 from myCal, the only date before would be Yesterday
finalOutput = "Yesterday ( ";
else if ( myCal.equals(today)) //if it is equal, then we print out Today
finalOutput = "Today (";
else //else, we format the date to get the Day Of The Week
finalOutput = dayOfTheWeek.format(myCal.getTime()) + " (";
finalOutput += sdf.format(myCal.getTime()) + ")"; //finally, printout the formatted string
System.out.println(finalOutput);
myCal.add(Calendar.DAY_OF_MONTH, 1); //move on to the next day
}
}
}
We need to clear the time part of the calendar since getting two instances of gCal would return a time difference in milliseconds at the very least and your comparsions using before() and equals() would always give you before() as true since you'd get myCal before today.
# 8
Replace the Calendar init part byCalendar myCal = gCal.getInstance();
Calendar today = myCal.clone();
Clearing the times isn't that necessary.
This also prevent you from problems if the 'today' is created on the next day while the 'myCal' is created 1ms earlier, accidently yet on the previous day. Which should occur very rare ;)
# 9
Thank you very much for your code, this example will surely give me enough help in adapting it to my wishes and what I had in mind for the jsp page.The dukes are yours :-)
# 10
@BalusC, of course! That's what the .clone() method is for! I always wondered where I would use it!Thanks!@kaderud, all the best!