Java Date Processing

Given an instance of java.util.Date , which is Sunday. If the month in question is 4 Sundays, I need to find out if the date is selected

  • 1st Sunday of the month
  • 2nd Sunday of the month
  • Second sunday of the month
  • Last Sunday of the month

If the month in question is 5 Sundays, I need to find out if the date is selected

  • 1st Sunday of the month
  • 2nd Sunday of the month
  • 3rd Sunday of the month
  • Second sunday of the month
  • Last Sunday of the month

I looked in JodaTime, but have not yet found the corresponding functions there. The project is actually a Groovy project, so I can use Java or Groovy to solve this problem.

Thanks Donal

+4
source share
6 answers

The solutions given so far will tell you 1, 2, 3, 4 or 5. But the question said that he wants to know the "last" and "the second to continue." If the questionnaire really needs to know the "last", etc., there is a bit more complicated. You must find out how many Sundays this month are to find out if the fourth Sunday is the last. The first way to understand what is happening to me is:

 GregorianCalendar gc=new GregorianCalendar(); gc.setTime(mydate); gc.set(Calendar.DAY_OF_MONTH,1); // Roll back to first of month int weekday=gc.get(Calendar.DAY_OF_WEEK); // day of week of first day // Find day of month of first Sunday int firstSunday; if (weekday==Calendar.SUNDAY) firstSunday=1; else firstSunday=weekday-Calendar.SUNDAY+6; // Day of month of fifth Sunday, if it exists int fifthSunday=firstSunday+7*4; // Find last day of month gc.add(Calendar.MONTH,1); gc.add(Calendar.DATE,-1); int lastDay=gc.get(DAY_OF_MONTH); int sundays; if (fifthSunday<lastDay) sundays=4; else sundays=5; 

This seems like a lot of work. Does anyone see an easier way?

+4
source

DAY_OF_WEEK_IN_MONTH

 import java.sql.Date; import java.util.Calendar; public class Dow { public static void main(String[] args) { Date date = new Date( 2010, 4, 1 ); Calendar cal = Calendar.getInstance(); cal.setTime( date ); for ( int week = 0 ; week < 5 ; week++ ) { System.out.println( cal.getTime() + " week:" + cal.get( Calendar.DAY_OF_WEEK_IN_MONTH ) ); cal.add( Calendar.WEEK_OF_MONTH, 1 ); } } } > Sun May 01 00:00:00 CEST 3910 week:1 > Sun May 08 00:00:00 CEST 3910 week:2 > Sun May 15 00:00:00 CEST 3910 week:3 > Sun May 22 00:00:00 CEST 3910 week:4 > Sun May 29 00:00:00 CEST 3910 week:5 
+2
source
 return dayOfMonth / 7 + 1; 

MORE EDITING

Ok, considering Jay's comment, try this:

 int sundayId = dayOfMonth / 7 + 1; if (sundayId == 1) { return "1st"; } else if (sundayId == 2) { return "2nd"; } else if (sundayId == 3) { return ((dayOfMonth + 14 <= daysInMonth) ? "3rd" : "2nd Last"); } else if (sundayId == 4) { return ((dayOfMonth + 7 <= daysInMonth) ? "2nd Last" : "Last"); } else { return "Last"; } 

whereas daysInMonth calculated as follows:

 if (month == 2) { if (year % 100) { return ((year % 400 == 0) ? 29 : 28); } else { return ((year % 4 == 0) ? 29 : 28); } } else if (month == 4 || month == 6 || month == 9 || month == 11) { return 30; } else { return 31; } 

... or some other way.

+2
source

int sundayOfMonth = dayOfMonth / 7 + 1;

+1
source

You can simply use java.util.Calendar to do calculations by date:

 import java.util.Calendar as CAL def cal = CAL.getInstance(date) def isFirst= cal.get(CAL.WEEK_OF_MONTH) == 1 def isSecond = cal.get(CAL.WEEK_OF_MONTH) == cal.getMaximum(CAL.WEEK_OF_MONTH) 

Also, do not assume that there is only five weeks per month with the Calendar object. For example, the maximum weeks of 05/01/2010 will be 6.

+1
source

EDIT: Here is my final version:

 today = Calendar.instance dayOfWeekInMonth = today.get(Calendar.DAY_OF_WEEK_IN_MONTH) // move to last day of week occurrence in current month today.set(Calendar.DAY_OF_WEEK_IN_MONTH, -1) maxDayOfWeekInMonth = today.get(Calendar.DAY_OF_WEEK_IN_MONTH) dayOfWeekCount = ["1st", "2nd", "3rd", "2nd last", "Last"] turningPoint = maxDayOfWeekInMonth - 2 if (dayOfWeekInMonth <= turningPoint) { println dayOfWeekCount[dayOfWeekInMonth - 1] } else { // use negative index to access list from last to first println dayOfWeekCount[dayOfWeekInMonth - maxDayOfWeekInMonth - 1] } 
+1
source

Source: https://habr.com/ru/post/1310526/


All Articles