java check if date is first Sunday of the Month
Asked Answered
R

5

5

I have been looking and searching a lot for this question. I'm really stupid when it comes to using the JAVA Calendar class.

Could anybody please help me to show a simple way of how to get the current time (on the android phone) and check whether is the first Sunday of the month or not.

// Comments in the code examples are very welcome :-)

Resendez answered 9/8, 2011 at 15:47 Comment(1)
It's java so you could just get the current time my doing System.currentTimeMillis() initialize a date or calendar with that. Or just do new java.util.Date() or even Calendar.getInstance()Pyromagnetic
D
8
Calendar cal = Calendar.getInstance();
if (Calendar.SUNDAY == cal.get(Calendar.DAY_OF_WEEK) && cal.get(Calendar.DAY_OF_MONTH) <= 7)
Deedradeeds answered 9/8, 2011 at 15:54 Comment(1)
Thank you very much! I had an idea of using <= 7, but was unclear about how to get the day name.Resendez
P
7

I thought about adding some explanation but this code is pretty self explanatory...

Calendar rightNow = Calendar.getInstance();
int weekDay = rightNow.get(Calendar.DAY_OF_WEEK);
int monthDay = rightNow.get(Calendar.DAY_OF_MONTH); 
if ( (weekDay == Calendar.SUNDAY) && (monthDay <8)) {
   // first sunday of this month
}
Paperboard answered 9/8, 2011 at 15:51 Comment(5)
You could also use Calendar.WEEK_OF_MONTH instead of Calendar.DAY_OF_MONTHPyromagnetic
@Pyromagnetic - I was not aware of that. Thank you!Paperboard
@Pyromagnetic - which is not a solution since the first day of the week depends on your locale, which means that you could have the first sunday of the month in the second week.Swanky
@Swanky Was not aware of the locale issue, thanks, I was personally in favor of the < 8 approach as well.Pyromagnetic
@Swanky + Ali - Thank you very much for our time and the discussion. Was a big help since my country use a different week system (weeks start on Monday).Resendez
P
6

Simply check if the date is a Sunday and if the date is less than 8... I'm sure there are better ways, but this would be the simplest.

Calendar cal = Calendar.getInstance();
cal.setDate(myDate);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
if(dayOfWeek == Calendar.SUNDAY && dayOfMonth < 8){

}

Also there is a cal.get(Calendar.WEEK_OF_MONTH) which you could use for comparison instead of of checking if dayOfMonth < 8 just do weekOfMonth == 0 could be 1 instead of 0.

Pyromagnetic answered 9/8, 2011 at 15:50 Comment(1)
I'd be careful with WEEK_OF_MONTH, as it looks to be influenced by WEEK_OF_YEARrules, which may cause it to produce unexpected results. That is, if there aren't enough days in the week (since the start of the month) for a complete week, it's actually part of the previous month (well, supposing that the start of the week is not sunday). Since this is region dependant, I'd stick with the day-of-the-month version.Stupa
D
1

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

Dana answered 11/10 at 23:56 Comment(1)
Excellent. Please enjoy how naturally this code expresses the concept of "the first Sunday of the month", opposite the now outdated answers using Calendar.Wormeaten
P
0

To get the current time use this:

Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minutes = now.get(Calendar.MINUTE));

To see if today is the first Sunday in a Month use

Calendar now = Calendar.getInstance();
int day = now.get(Calendar.DAY_OF_WEEK);
if (day == Calendary.Sunday) // today is a Sunday
{
    int day_num = now.get(Calendar.DAY_OF_MONTH);
    if (day_num <= 7) // the day number is within the 1st 7 days of the month
    {
         // this is the 1st sunday in the month
    }
}
Pleistocene answered 9/8, 2011 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.