tags:

views:

102

answers:

6

Using Calendar I can get the week, year and all details for the current day. How can I navigate to a particualr day in that week?

Say, calendar.get(Calendar.DAY_OF_WEEK); returns 3, which means a Tuesday. Now, I want to go to say Friday for that week or any other day in that week. How can I do that?

Thanks for your replies. I think I need to make the scenario more clear. Basically, I am trying to disable email alerts in my system during specified period. I get values like: disableStart = "FRIDAY-19:00" disableEnd = "SUNDAY-19:00"

Now, i need to verify if email should be sent at a particular time. e.g. if today = Thursday any time, send email but, if today = Saturday any time can't send as per values above.

+5  A: 

If I understand correctly you can use the Calendar.set(Field, value) method.

SimpleDateFormat f = new SimpleDateFormat("dd-MM-yyyy");
Calendar c = Calendar.getInstance();
System.out.println(c.get(Calendar.DAY_OF_WEEK));
System.out.println(f.format(c.getTime()));
c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
System.out.println(c.get(Calendar.DAY_OF_WEEK));
System.out.println(f.format(c.getTime()));

Produces the output

6
08-10-2010
3
05-10-2010
Kevin D
Edited to inlcude a SimpleDateFormat to show the day of the week changing.
Kevin D
A: 

I suggest to add the appropriate amount of days like so:

Calendar cal = Calendar.getInstance(); 

// lets point it to friday
int day = cal.get(Calendar.DAY_OF_WEEK); // e.g. 3 = tuesday
cal.add(Calendar.DAY_OF_WEEK, Calendar.FRIDAY - day) // 6 - 3 = 3, 3 days added = friday.

// and back to Monday
day = cal.get(Calendar.DAY_OF_WEEK);
cal.add(Calendar.DAY_OF_WEEK, Calendar.MONDAY - day) // 2 - 6 = -4, 4 days subtracted = monday

Hope this helps.

jek
Won't work. Your code is skipping to the next Friday, respectively previous Monday. Anubhav asked how to change the calendar to a different day in the same week.
jarnbjo
Sorry to disagree, but the code always stays within the same week but different day, which is what was asked for. And adding days to a calender doesn't mean to leave that week (obviously depends an how many days are added, which is why there is a calculation). Please try it before complaining.
jek
+1  A: 

cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);

jarnbjo
+1  A: 
    Calendar c = Calendar.getInstance();
    Date date = new Date();
    c.setTime(date);
    System.out.println("Today:  " + c.getTime());
    c.setTime(date);
    c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    System.out.println("MONDAY: " + c.getTime());
    c.setTime(date);
    c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
    System.out.println("TUESDAY: " + c.getTime());
    c.setTime(date);
    c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
    System.out.println("WEDNESDAY: " + c.getTime());
    c.setTime(date);
    c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
    System.out.println("THURSDAY: " + c.getTime());
    c.setTime(date);
    c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
    System.out.println("FRIDAY: " + c.getTime());
    c.setTime(date);
    c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
    System.out.println("SATURDAY: " + c.getTime());
    c.setTime(date);
    c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    System.out.println("SUNDAY: " + c.getTime());

Gives:

Today:  Fri Oct 08 15:45:14 CEST 2010
MONDAY: Mon Oct 04 15:45:14 CEST 2010
TUESDAY: Tue Oct 05 15:45:14 CEST 2010
WEDNESDAY: Wed Oct 06 15:45:14 CEST 2010
THURSDAY: Thu Oct 07 15:45:14 CEST 2010
FRIDAY: Fri Oct 08 15:45:14 CEST 2010
SATURDAY: Sat Oct 09 15:45:14 CEST 2010
SUNDAY: Sun Oct 10 15:45:14 CEST 2010

Which seams to mean that, at least on my system, the weeks starts on monday.

Maurice Perry
Thanks Maurice, this was exactly what i was looking for.
Anubhav Anand
+1  A: 

Thanks to Kevin and Maurice for the answers. They really gave me the start point.

I ended with this test code, in case it helps anyone.

private static Date getTimeForAnyDayInWeek(int nDay, int nHour, int nMin)
{
    Calendar c = Calendar.getInstance();
    c.setFirstDayOfWeek(Calendar.MONDAY);
    Date date = Calendar.getInstance().getTime();
    c.setTime(date);
    c.set(Calendar.DAY_OF_WEEK, nDay);
    c.set(Calendar.HOUR_OF_DAY, nHour);
    c.set(Calendar.MINUTE, nMin);
    return c.getTime();
}

public static void main(String[] args)
{
    Date start = getTimeForAnyDayInWeek(6, 19, 00);
    Date end = getTimeForAnyDayInWeek(8, 19, 00);
    Date c = new Date();

    if (start.before(c) && c.before(end))
        System.out.println("BLOCK");
    else
        System.out.println("SEND");
}

Thanks, Anubhav

Anubhav Anand
A: 

This is a perfect example of why jodatime is so good, here is my similar code

  DateTime dt = new DateTime(); //current datetime, jodatime format
  DateTime fridayLastWeek = dt.minusWeeks(1).dayOfWeek().setCopy("Friday");
  Date convertedtorubbishdateformat = fridayLastWeek.toDate();

I used to waste so much time witht he standard java date/calendar. Then i got jodatime, you wont regret, it apparently will be used as part of standard java in the future. I didn;t bother downlaoding the jar for for ages, I wish I had done, you won't regret it.

NimChimpsky