Skip to content

Instantly share code, notes, and snippets.

@practicalli-johnny
Last active August 29, 2015 14:14
Show Gist options
  • Save practicalli-johnny/2e187e761f8974e10496 to your computer and use it in GitHub Desktop.
Save practicalli-johnny/2e187e761f8974e10496 to your computer and use it in GitHub Desktop.
UpdateDueDate class alternate example
// Note: VerifyDate seems to be quite obscure as a class name, so have changed it to UpdateDueDate, as this is what I understand the intent of the class to be.
// If updating a due date is not the intent, then I did not understand the intent of the code.
public class UpdateDueDate {
// Examples:
// if current date is 15th Feb and due date is 22nd Feb, return 22nd
// if current date is 3rd Feb and due date is 5th March, return the last date in February
public static Date SetDueDateWithin30Days(Date currentDate, Date dueDate) {
if(CheckDueDateWithin30Days(currentDate, dueDate)) {
return dueDate;
} else {
return EndDateOfCurrentMonth(currentDate);
}
}
private static Boolean CheckDateWithin30Days(Date currentDate, Date dueDate) {
Date date30DaysFromCurrent = currentDate.addDays(30);
if( dueDate > date30DaysFromCurrent ) {
return false;
} else {
return true;
}
}
private static Date EndDateOfCurrentMonth(Date currentDate) {
;; daysInMonth() returns the number of days in the month for a specific year & month.
Integer lastDayInMonth = Date.daysInMonth(originalDate.year(), originalDate.month());
Date endDateOfCurrentMonth = Date.newInstance(originalDate.year(), originalDate.month(), lastDayInMonth);
return endDateOfCurrentMonth;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment