How to check a day is in the current week in Java?

date, java

Solution

You definitely want to use the Calendar class: http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html

Here's one way to do it:

public static boolean isDateInCurrentWeek(Date date) {
  Calendar currentCalendar = Calendar.getInstance();
  int week = currentCalendar.get(Calendar.WEEK_OF_YEAR);
  int year = currentCalendar.get(Calendar.YEAR);
  Calendar targetCalendar = Calendar.getInstance();
  targetCalendar.setTime(date);
  int targetWeek = targetCalendar.get(Calendar.WEEK_OF_YEAR);
  int targetYear = targetCalendar.get(Calendar.YEAR);
  return week == targetWeek && year == targetYear;
}

Problem

Is there easiest way to find any day is in the current week? (this function returns true or false, related to given day is in current week or not).

Original source

Related problems