Skip to content

Instantly share code, notes, and snippets.

@Velmm
Created December 29, 2017 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Velmm/0e613c885ca635a1b2f7f7728e509f2b to your computer and use it in GitHub Desktop.
Save Velmm/0e613c885ca635a1b2f7f7728e509f2b to your computer and use it in GitHub Desktop.
import java.util.Calendar;
import java.util.Date;
public class NextSaturdayMain {
public static void main(String[] args) {
System.out.println(getSaturday(new Date()));
}
public static long getSaturday(Date today) {
Calendar cal = Calendar.getInstance();
cal.setTime(today);
int dow = cal.get(Calendar.DAY_OF_WEEK);
while (dow != Calendar.SATURDAY) {
int date = cal.get(Calendar.DATE);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
if (date == getMonthLastDate(month, year)) {
if (month == Calendar.DECEMBER) {
month = Calendar.JANUARY;
cal.set(Calendar.YEAR, year + 1);
} else {
month++;
}
cal.set(Calendar.MONTH, month);
date = 1;
} else {
date++;
}
cal.set(Calendar.DATE, date);
dow = cal.get(Calendar.DAY_OF_WEEK);
}
System.out.println(cal.getTime());
return cal.getTimeInMillis();
}
private static int getMonthLastDate(int month, int year) {
switch (month) {
case Calendar.JANUARY:
case Calendar.MARCH:
case Calendar.MAY:
case Calendar.JULY:
case Calendar.AUGUST:
case Calendar.OCTOBER:
case Calendar.DECEMBER:
return 31;
case Calendar.APRIL:
case Calendar.JUNE:
case Calendar.SEPTEMBER:
case Calendar.NOVEMBER:
return 30;
default: // Calendar.FEBRUARY
return year % 4 == 0 ? 29 : 28;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment