I am working on a project where the requirement is to have a date calculated as being the last Friday of a given month. I think I have a solution that only uses standard Ja
Using java.time library built into Java 8 and later, you may use TemporalAdjusters.lastInMonth:
val now = LocalDate.now()
val lastInMonth = now.with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY))
You may choose any day from the DayOfWeek enum.
If you need to add time information, you may use any available LocalDate to LocalDateTime conversion like
lastFriday.atStartOfDay() // e.g. 2015-11-27T00:00
Hope this helps..
public static void getSundaysInThisMonth(int monthNumber, int yearNumber){
//int year =2009;
//int dayOfWeek = Calendar.SUNDAY;
// instantiate Calender and set to first Sunday of 2009
Calendar cal = new GregorianCalendar();
cal.set(Calendar.MONTH, monthNumber-1);
cal.set(Calendar.YEAR, yearNumber);
cal.set(Calendar.DATE, 1);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int dateOfWeek = cal.get(Calendar.DATE);
while (dayOfWeek != Calendar.SUNDAY) {
cal.set(Calendar.DATE, ++dateOfWeek);
dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
}
cal.set(Calendar.DATE, dateOfWeek);
int i = 1;
while (cal.get(Calendar.YEAR) == yearNumber && cal.get(Calendar.MONTH)==monthNumber-1)
{
System.out.println("Sunday " + " " + i + ": " + cal.get(Calendar.DAY_OF_MONTH));
cal.add(Calendar.DAY_OF_MONTH, 7);
i++;
}
}
public static void main(String args[]){
getSundaysInThisMonth(1,2009);
}
You need to know two things - the number of days in the month, and the weekday the first of the month falls on.
If the first day of the month is a
There are only three special cases. A single switch statement and three if statements (or ternary operators if you like every case to have a single line...)
Work it out on paper. Don't need any special libraries, functions, julian conversions, etc (well, except to get the weekday the 1st falls on, and maybe the number of days that month... )
Aaron implemented it in Java.
-Adam
In Java 8, we can do it simply as:
LocalDate lastFridayOfMonth = LocalDate
.now()
.with(lastDayOfMonth())
.with(previous(DayOfWeek.FRIDAY));
code for Adam Davis's algorithm
public static int getLastFriday(int month, int year)
{
Calendar cal = Calendar.getInstance();
cal.set(year, month, 1, 0, 0, 0); // set to first day of the month
cal.set(Calendar.MILLISECOND, 0);
int firstDay = cal.get(Calendar.DAY_OF_WEEK);
int daysOfMonth = cal.getMaximum(Calendar.DAY_OF_MONTH);
switch (firstDay)
{
case Calendar.SUNDAY :
return 27;
case Calendar.MONDAY :
return 26;
case Calendar.TUESDAY :
return 25;
case Calendar.WEDNESDAY :
if (daysOfMonth == 31) return 31;
return 24;
case Calendar.THURSDAY :
if (daysOfMonth >= 30) return 30;
return 23;
case Calendar.FRIDAY :
if (daysOfMonth >= 29) return 29;
return 22;
case Calendar.SATURDAY :
return 28;
}
throw new RuntimeException("what day of the month?");
}}
That looks like a perfectly acceptable solution. If that works, use it. That is minimal code and there's no reason to optimize it unless you have to.