tl;dr
today.with ( TemporalAdjusters.firstInMonth( DayOfWeek.SUNDAY ) )
Avoid legacy date-time classes
I'm really stupid when it comes to using the JAVA Calendar class
No, you are not stupid. The Calendar
class is stupid. As is its brethren, such as Date
, SimpleDateFormat
, Timestamp
, etc. Never use these terribly-flawed legacy classes.
java.time
Use only the modern java.time classes defined in JSR 310, built into Java 8+.
current time
Apparently you meant the current moment.
To get the current moment as seen in a particular time zone, use ZonedDateTime
. We need to specify a time zone. For any given moment, the time and the date both vary around the globe by time zone.
ZoneId z = ZoneId.systemDefault() ; // Or ZoneId.of( "America/Edmonton" )
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
Extract the date-only.
LocalDate today = zdt.toLocalDate() ;
Adjust that date to the first Sunday of the month.
LocalDate firstSunday = today.with ( TemporalAdjusters.firstInMonth( DayOfWeek.SUNDAY ) ) ;
Compare.
boolean sameDay = today.isEqual( firstSunday ) ;
See this code run live at Ideone.com.
today.toString() = 2024-10-11
firstSunday.toString() = 2024-10-06
sameDay = false
System.currentTimeMillis()
initialize a date or calendar with that. Or just donew java.util.Date()
or evenCalendar.getInstance()
– Pyromagnetic