Anubhav Anand Anubhav Anand - 4 years ago 97
Java Question

Java : Get any day in a week from Calendar

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.

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download