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).
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;
}
Use the Calendar
class to get the YEAR and WEEK_OF_YEAR fields and see if both are the same.
Note that the result will be different depending on the locale, since different cultures have different opinions about which day is the first day of the week.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With