BÀI 96 - SO SÁNH NGÀY TRONG ANDROID
Đầu tiên, tạo Class này và code như này
public class DateComparison {
public static boolean InMonth(String date) {
@SuppressLint("SimpleDateFormat")
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss - dd/MM/yyyy");
Date today = new Date();
boolean equalsMonth = false;
String formatToday = dateFormat.format(today);
try {
Date dateToday = dateFormat.parse(formatToday);
Date dateInput = dateFormat.parse(date);
if (dateInput.getMonth() == dateToday.getMonth()) equalsMonth = true;
} catch (ParseException e) {
e.printStackTrace();
}
return equalsMonth;
}
public static boolean InWeek(String date) {
@SuppressLint("SimpleDateFormat")
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss - dd/MM/yyyy");
Date today = new Date();
boolean SameWeek = false;
String formatToday = dateFormat.format(today);
try {
Date dateToday = dateFormat.parse(formatToday);
Date dateInput = dateFormat.parse(date);
Calendar calendarToday = Calendar.getInstance();
Calendar calendarInput = Calendar.getInstance();
calendarToday.setTime(dateToday);
calendarInput.setTime(dateInput);
int year1 = calendarToday.get(Calendar.YEAR);
int week1 = calendarToday.get(Calendar.WEEK_OF_YEAR);
int year2 = calendarInput.get(Calendar.YEAR);
int week2 = calendarInput.get(Calendar.WEEK_OF_YEAR);
if (year1 == year2 && week1 == week2){
SameWeek = true;
}
} catch (ParseException e) {
e.printStackTrace();
}
return SameWeek;
}
}