Return Calendar based on given Calendar object and Week Name
Hi All,
I have to return a Calendar based on a given Calendar date and a String which is a Week Name.
For example if the Calendar object is 06/12/2007 and the Week Name is say Friday, I have to return the date as 6/15/2007.
If the Calendar object is 06/12/2007 and the week name is say Tuesday, it has to return the same date.
Can someone let me know who to use the Calendar API to arrive at the logic.
thanks
[448 byte] By [
harishgopa] at [2007-11-27 7:28:13]

Have you tried creating a Calendar object, setting the day to the desired day, and then setting the day of the week to the desired day? Just using Calendar.set(), and the correct arguments (look at all the fields for the Calendar and GregorianCalendar classes).
> Have you tried creating a Calendar object, setting
> the day to the desired day, and then setting the day
> of the week to the desired day? Just using
> Calendar.set(), and the correct arguments (look at
> all the fields for the Calendar and GregorianCalendar
> classes).
Imn't sure what you meant by setting the day to the desired day.
I couldn't find an appropriate API for the same.
I had actually tried using something like this, but I know I'm missing the logic somewhere, it doesn't work all the time.
public static String getStartDate(Calendar cal, String weekDay)
{
System.out.println(cal.get(Calendar.DAY_OF_WEEK));
int weekDayInNumber = 0;
if(weekDay.equalsIgnoreCase("SUNDAY"))
weekDayInNumber = Calendar.SUNDAY;
else if(weekDay.equalsIgnoreCase("MONDAY"))
weekDayInNumber = Calendar.MONDAY;
else if(weekDay.equalsIgnoreCase("TUESDAY"))
weekDayInNumber = Calendar.TUESDAY;
else if(weekDay.equalsIgnoreCase("WEDNESDAY"))
weekDayInNumber = Calendar.WEDNESDAY;
else if(weekDay.equalsIgnoreCase("THURSDAY"))
weekDayInNumber = Calendar.THURSDAY;
else if(weekDay.equalsIgnoreCase("FRIDAY"))
weekDayInNumber = Calendar.FRIDAY;
else if(weekDay.equalsIgnoreCase("SATURDAY"))
weekDayInNumber = Calendar.SATURDAY;
cal.set(Calendar.DAY_OF_WEEK, weekDayInNumber);
return cal.getTime()+"";
}
> Imn't sure what you meant by setting the day to the> desired day. > I couldn't find an appropriate API for the same.Did you read the docs?Did you see the Calendar.set method?Did you see all those fields with names like "THURSDAY"?
Like this:
import java.util.Calendar;
import java.util.GregorianCalendar;
import static java.util.Calendar.DAY_OF_WEEK;
import static java.util.Calendar.THURSDAY;
public class TestCal {
public static void main(String[] argv) {
Calendar c = new GregorianCalendar(2007, 5, 12); // June 12, 2007
System.out.println(c.getTime());
c.set(DAY_OF_WEEK, THURSDAY);
System.out.println(c.getTime());
}
}
which prints:
Tue Jun 12 00:00:00 PDT 2007
Thu Jun 14 00:00:00 PDT 2007
Why have all those else/ifs just to set a value you already have?What are you trying to accomplish exactly?Also cal.getTime()+"" is hideous, instead do cal.getTime().toString().
Wait, are all you asking is how to turn "Thursday" into the corresponding integer declared by Calendar.THURSDAY?Why not just create a Map?Some of the classes in java.text may be helpful as well.
Hi,
yes, even I was thinking there should be some way to get rid of the if else.. I'm looking at this.
But using cal.set(Calendar.DAY_OF_WEEK,Calendar.THURSDAY) is not always helping me, because for another example I have the date as
Calendar cal = new GregorianCalendar(2007,5,12);
cal.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
System.out.println("Cal : " + cal.getTime());
It returns me
Sun Jun 10 00:00:00 EDT 2007 instead of
Sun Jun 17 00:00:00 EDT 2007
I see. Sorry I didn't get what you were saying at first.
So it sounds like there are two problems. One is just the issue of selecting the day of the week by name rather than by fields of Calendar. This should be pretty simple; you can use a Map, or perhaps better yet, you could define an enum, and one of the fields of the enum could be the ints defined by Calendar.
The other issue is the trickier one -- you want to select the day of the week within the limitations of a defined week, whereas set's behavior seems to be to choose the nearest matching day.
I'm not aware offhand of a simple way to fix that, but you could do it this way:
First, create a calendar for the given day.
Then, figure out which day is the first day of the given week. You can use Calendar.getFirstDayOfWeek, but that just gives you the day, not the date. In this step you need to get the date -- first set the calendar to the first day of the week, then do getTime.
Then, re-init the calendar to the given day.
Then, get the day of the week you're looking for.
If the date you get is before the first day of the week, then add one week to the result.
It's kind of a hack, but I think it would work.
You might want to wrap this all up in an enum. Then you could use enum methods to get the day by name, and then have a method on the object to get the date object representing the nearest such day in the same week as a date passed as an argument.
