To check if two dates in Java represent the same day, you can use the getDayOfYear
and getYear
methods of the Calendar
class to get the day of the year and year for each date, and then compare the values.
Here's an example of how to check if two dates are the same day using the Calendar
class:
import java.util.Calendar; import java.util.Date; public class Main { public static void main(String[] args) { Calendar cal1 = Calendar.getInstance(); cal1.setTime(new Date(1)); // set to January 1, 1970 int day1 = cal1.get(Calendar.DAY_OF_YEAR); int year1 = cal1.get(Calendar.YEAR); Calendar cal2 = Calendar.getInstance(); cal2.setTime(new Date(86400000)); // set to January 2, 1970 int day2 = cal2.get(Calendar.DAY_OF_YEAR); int year2 = cal2.get(Calendar.YEAR); if (day1 == day2 && year1 == year2) { // dates are the same day } else { // dates are not the same day } } }
In this example, the Calendar
objects cal1
and cal2
are used to represent two dates. The getDayOfYear
and getYear
methods are used to get the day of the year and year for each date.