Oracle next_day function equivalent in JAVA - java

I wanted to make a method which works similar to as oracle's next_day() function, below is my code which accepts date n weekday in MONDAY,TUESDAY etc. and returns a date which falls on the next given day from the input date.
/**
* #param date
* #param weekDay
* #return Gives the date for next weekday specified in the parameter.
*/
public Date getNextDay(Date value, String weekday) {
Calendar date1 = Calendar.getInstance();
date1.setTime(value);
if (weekday.equalsIgnoreCase("MONDAY")) {
while (date1.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
date1.add(Calendar.DATE, 1);
}
} else if (weekday.equalsIgnoreCase("TUESDAY")) {
while (date1.get(Calendar.DAY_OF_WEEK) != Calendar.TUESDAY) {
date1.add(Calendar.DATE, 1);
}
} else if (weekday.equalsIgnoreCase("WEDNESDAY")) {
while (date1.get(Calendar.DAY_OF_WEEK) != Calendar.WEDNESDAY) {
date1.add(Calendar.DATE, 1);
}
} else if (weekday.equalsIgnoreCase("THURSDAY")) {
while (date1.get(Calendar.DAY_OF_WEEK) != Calendar.THURSDAY) {
date1.add(Calendar.DATE, 1);
}
} else if (weekday.equalsIgnoreCase("FRIDAY")) {
while (date1.get(Calendar.DAY_OF_WEEK) != Calendar.FRIDAY) {
date1.add(Calendar.DATE, 1);
}
} else if (weekday.equalsIgnoreCase("SATURDAY")) {
while (date1.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) {
date1.add(Calendar.DATE, 1);
}
} else {
while (date1.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
date1.add(Calendar.DATE, 1);
}
}
return date1.getTime();
}
Please suggest a better way of doing this.

I recommend you do it using the modern java.time date-time API. Learn more about the modern date-time API from Trail: Date Time. The java.util date-time API and SimpleDateFormat are outdated and error-prone. In case you are not using Java-8, you can still use Java-8 date-time API through ThreeTenABP library.
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class Main {
public static void main(String[] args) {
// Tests
// Next occurrence
System.out.println(getNextDay(LocalDate.now(), "Monday"));
System.out.println(getNextDay(LocalDate.now(), "Wednesday"));
// Same (if date falls on the given day) or next occurrence
System.out.println(getSameOrNextDay(LocalDate.now(), "Monday"));
System.out.println(getSameOrNextDay(LocalDate.now(), "Wednesday"));
}
static LocalDate getNextDay(LocalDate value, String weekday) {
return value.with(TemporalAdjusters.next(DayOfWeek.valueOf(weekday.toUpperCase())));
}
static LocalDate getSameOrNextDay(LocalDate value, String weekday) {
return value.with(TemporalAdjusters.nextOrSame(DayOfWeek.valueOf(weekday.toUpperCase())));
}
}
Output:
2020-09-28
2020-09-30
2020-09-28
2020-09-23

If you are interested in the next day I'll suggest using a java.time.LocalDate instead of a java.util.Date.
The following code accepts such a LocalDate along with a String (that needs to be a full day of week in upper case letters) and returns the LocalDate representing the nearest future date which has the given day of week:
public static LocalDate nextDay(LocalDate sourceDay, String weekday) {
// parse the day of week to an enum value
DayOfWeek dayOfWeek = DayOfWeek.valueOf(weekday);
// check if the day is the same as the one of the given LocalDate
if (sourceDay.getDayOfWeek().equals(dayOfWeek)) {
// and return the LocalDate that's a week later
return sourceDay.plusWeeks(1);
} else {
// otherwise add a day to the given date
LocalDate nextDayOfWeek = sourceDay.plusDays(1);
// and do that until the day of week of the given date is reached
while (nextDayOfWeek.getDayOfWeek() != dayOfWeek) {
nextDayOfWeek = nextDayOfWeek.plusDays(1);
}
// then return the future date
return nextDayOfWeek;
}
}
You can use it in a main like this:
public static void main(String[] args) {
System.out.println(nextDay(LocalDate.now(), "FRIDAY")
.format(DateTimeFormatter.ofPattern("uuuu-MM-dd '('EEEE')'",
Locale.ENGLISH)));
}
Output (today ⇒ 2020-09-23):
2020-09-25 (Friday)

Use the "new" java.time.DayOfWeek and java.time.LocalDate classes:
public LocalDate getNextDay(
final LocalDate value,
final DayOfWeek day
)
{
int currentDay = value.getDayOfWeek().getValue();
int expectedDay = day.getValue();
if ( currentDay >= expectedDay )
{
expectedDay += 7;
}
return value.plusDays( expectedDay - currentDay );
}

Related

JAVA - How to make custom holidays and keep track of them

Making a Calendar that allows you to add in specific holidays
You should use the java.time package. Specifically the MonthDay class to represent a holiday, as holidays are usually some fixed month + day (however you will need more complicated logic than a Set<MonthDay> if the holidays are "dynamic").
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.MonthDay;
import java.util.HashSet;
import java.util.Set;
public class CustomHolidays {
private final Set<MonthDay> holidays = new HashSet<>();
public void addHoliday(final MonthDay monthDay) {
holidays.add(monthDay);
}
public boolean isHoliday(final LocalDate localDate) {
return isWeekend(localDate) || holidays.contains(toMonthDay(localDate));
}
public int numberOfWorkdaysBetween(final LocalDate startInclusive, final LocalDate endInclusive) {
int c = 0;
for (LocalDate i = startInclusive; !i.isAfter(endInclusive); i = i.plusDays(1)) {
if (!isHoliday(i)) {
c++;
}
}
return c;
}
private boolean isWeekend(final LocalDate localDate) {
final DayOfWeek dow = localDate.getDayOfWeek();
return dow == DayOfWeek.SATURDAY || dow == DayOfWeek.SUNDAY;
}
private static MonthDay toMonthDay(final LocalDate localDate) {
return MonthDay.of(localDate.getMonth(), localDate.getDayOfMonth());
}
public static void main(String[] args) {
final CustomHolidays ch = new CustomHolidays();
ch.addHoliday(MonthDay.of(Month.MAY, 1));
ch.addHoliday(MonthDay.of(Month.MAY, 2));
ch.addHoliday(MonthDay.of(Month.MAY, 3));
System.out.println(ch.numberOfWorkdaysBetween(LocalDate.of(2018, 5, 1), LocalDate.of(2018, 5, 8)));
}
}
Output: 3
As recommended by others, you should go ahead and use java.time.LocalDate instead.
But if you are still curious as to why your current approach is not working, read ahead.
1st Problem: The following while condition will stop once the day is equal, even if months are different (because dayIndex!= yDay will evaluate to false)
while (monthIndex != yMonth && dayIndex != yDay)
Changing the while condition to below one will check for both the day and month. Also compare monthIndex to yMonth-1 since your monthIndex is initialised as xMonth-1
while (!(monthIndex == yMonth-1 && dayIndex == yDay))
2nd problem: Change int calMonth = xMonth; to int calMonth = xMonth-1; for the same reason as mentioned in problem 1.
3rd Problem: Initialise dayIndex to 0 instead of 1, because you are incrementing dayIndex after the if block. If you initialise it to 1, it will increment to 2 and hence 1st date of every month will be skipped.
if (dayIndex == numDaysInMonth+1) {
dayIndex = 0;
monthIndex++;
}
dayIndex++;
Check here for working fiddle

Java - Count Number of days except Holidays(Saturday and Sunday) [duplicate]

This question already has an answer here:
Calculate business days in java without saturdays, sunday and public holiday
(1 answer)
Closed 4 years ago.
I need to count for number of days except holidays(Saturday and sunday). For example my start date is 07/02/2018 and end date is 15/02/2018 (in dd/MM/yyyy format). I need to count number of working days between them. Can some please help me out? This is my code:
SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
//Date date12 = dateformat3.parse("17/07/1989");
String date1 = "11/07/2018";
String date2 = "20/07/2018";
// Date date2 = dateformat3.parse("15/10/2007");
Calendar startdate = Calendar.getInstance();
startdate.setTime(dateformat3.parse(date1));
Calendar enddate = Calendar.getInstance();
enddate.setTime(dateformat3.parse(date2));
while (!startdate.after(enddate)) {
int day = startdate.get(Calendar.DAY_OF_WEEK);
if ((day != Calendar.SATURDAY) && (day != Calendar.SUNDAY)) {
workingDays++;
}
}
I've tried with this code but is not showing any result.
You were close, just need to increment start date inside while loop.
public static void main(String[] args) throws Exception {
System.out.println(countDays("07/02/2018", "15/02/2018"));
}
public static int countDays(String startDate, String endDate) throws Exception {
int workingDays = 0;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar startdate = Calendar.getInstance();
startdate.setTime(sdf.parse(startDate));
Calendar enddate = Calendar.getInstance();
enddate.setTime(sdf.parse(endDate));
while (!startdate.after(enddate)) {
int day = startdate.get(Calendar.DAY_OF_WEEK);
System.out.println(day);
if ((day != Calendar.SATURDAY) && (day != Calendar.SUNDAY)) {
workingDays++;
}
// increment start date, otherwise while will give infinite loop
startdate.add(Calendar.DATE, 1);
}
return workingDays;
}
As you can see, the only difference with the code I provided from yours (besides removing hard-coded values) is startdate.add(Calendar.DATE, 1);
I think you are doing wrong with your while condition.
Try this
while (ChronoUnit.DAYS.between(startdate.toInstant(), enddate.toInstant()) > 0) {
startdate.add(Calendar.DAY_OF_MONTH, 1);
if (startdate.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && startdate.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
workingDays++;
}
}
If you are using Java 8 you can use :
//Here I change the format of date to make it parsable
String date1 = "2018-07-11";
String date2 = "2018-07-20";
//Parse the date to LocalDate
LocalDate start = LocalDate.parse(date1);
LocalDate end = LocalDate.parse(date2);
//iterate over the list of dates between start and end date,
//then filter only those who are not equal SATURDAY or SUNDAY, then count the result
long result = Stream.iterate(start, date -> date.plusDays(1))
.limit(ChronoUnit.DAYS.between(start, end))
.filter(date -> date.getDayOfWeek() != DayOfWeek.SATURDAY
&& date.getDayOfWeek() != DayOfWeek.SUNDAY
).count();
In your case it will return 7 days

Changing String type date to Day string

I'm using an API for my weather app and the JSON this API returns, gives me the 'Date' like that:"date": "2016-08-20" which is string type. But I want to write the day on screen like 'wednesday'. I look up for conversion on the internet but I couldn't find the solution for my case. Can anyone explain how to do that. Thanks in advance. By the way this is an android project.
You simply need 2 SimpleDateFormatters,
one to parse the String value of current date and get Date
object from it.
and second to get the week in String value from that Date Object.
Output
Saturday
Code
import java.util.*;
import java.text.*;
public class HelloWorld {
public static void main(String[] args) throws ParseException {
Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2016-08-20");
String dayOfWeek = new SimpleDateFormat("EEEEE").format(date);
System.out.println(dayOfWeek);
}
}
OR - One Line Code - If we ignore Readability
import java.util.*;
import java.text.*;
public class HelloWorld {
public static void main(String[] args) throws ParseException {
System.out.println(new SimpleDateFormat("EEEEE").format(new SimpleDateFormat("yyyy-MM-dd").parse("2016-08-20")));
}
}
You may use split to tokenize the string but I prefer using DateFormat because it's a standard way to do the process :
String dateString ="2016-08-20";
SimpleDateFormat formatInput = new SimpleDateFormat("yyyy-MM-dd");
Date date = formatInput.parse(dateString);
SimpleDateFormat formatOutput = new SimpleDateFormat("EEEE");
String day = formatOutput.format(date);
The idea is creating a Date instance from the date in string 2016-08-20 and then from the Date instance formatting it to render the day of the week.
Use SimpleDateFormat class, for example:
try {
String inputStr = "2016-08-20";
Date inputDate = new SimpleDateFormat("yyyy-MM-dd").parse(inputStr);
String dayName = new SimpleDateFormat("EEEE").format(inputDate);
} catch (ParseException e) {
e.printStackTrace();
}
this might help,
String weekDay = "";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(df.parse(yourDateAsStringHere));
int dayOfWeek= cal.get(Calendar.DAY_OF_WEEK);
if (Calendar.MONDAY == dayOfWeek) {
weekDay = "monday";
} else if (Calendar.TUESDAY == dayOfWeek) {
weekDay = "tuesday";
} else if (Calendar.WEDNESDAY == dayOfWeek) {
weekDay = "wednesday";
} else if (Calendar.THURSDAY == dayOfWeek) {
weekDay = "thursday";
} else if (Calendar.FRIDAY == dayOfWeek) {
weekDay = "friday";
} else if (Calendar.SATURDAY == dayOfWeek) {
weekDay = "saturday";
} else if (Calendar.SUNDAY == dayOfWeek) {
weekDay = "sunday";
}

NETWORKDAYS.INTL in Java

Excel has a function named NETWORKDAYS.INTL. It receives a start date, an end date, a "type" of weekend and a list of holidays and calculates the number of working days between the two dates. The weekend flag is important because I can consider saturday as a working day. More info here:
http://office.microsoft.com/en-nz/excel-help/networkdays-intl-function-HA010354379.aspx
Is there something like NETWORKDAYS.INTL in Java? Using ObjectLab Kit, Joda and Jollyday I can keep my national holidays and check if the day is saturday, sunday or holiday, but to calculate like the function above I need to iterate over each day and check if valid.
I found nothing so I just code my own method. In my application all dates are saved as timestamp.
public boolean isHoliday(long date) {
return holidaysCalendar.isHoliday(new LocalDate(date));
}
public boolean isSaturday(long date) {
return (new LocalDate(date).dayOfWeek().get() == 7);
}
public boolean isSunday(long date) {
return (new LocalDate(date).dayOfWeek().get() == 1);
}
public boolean isValid(long date, boolean saturday, boolean sunday) {
boolean valid = true;
if (isHoliday(date)) {
valid = false;
} else {
if (!saturday && isSaturday(date)) {
valid = false;
}
if (!sunday && isSunday(date)) {
valid = false;
}
}
return valid;
}
public long addDays(long date, int days) {
return new LocalDate(date).plusDays(days).toDateTimeAtStartOfDay()
.getMillis();
}
public int validDaysBetweenDates(long date1, long date2,
boolean saturday, boolean sunday) {
date1 = addDias(date1, 1);
int cont = 0;
for (long dateTmp = date1; dataTmp <= date2; dataTmp = addDays(dataTmp, 1)) {
if (isValid(dataTmp, saturday, sunday))
cont++;
}
return cont;
}

How can I add business days to the current date in Java?

How can I add business days to the current date in Java?
public Calendar addBusinessDate(Calendar cal, int days) {
//
// code goes over here
//
}
Note:
It should consider weekends too.
You may want to consider using ObjectLab Kit to do the heavy lifting for you.
Assuming the requirement is simply to return the next business day when the computed date falls on a non-business day:
package bizdays.example;
import java.time.LocalDate;
import java.util.HashSet;
import net.objectlab.kit.datecalc.common.DateCalculator;
import net.objectlab.kit.datecalc.common.DefaultHolidayCalendar;
import net.objectlab.kit.datecalc.common.HolidayHandlerType;
import net.objectlab.kit.datecalc.jdk8.LocalDateKitCalculatorsFactory;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.Matchers.equalTo;
public class BizDayTest {
private DateCalculator<LocalDate> dateCalculator;
private final LocalDate startDate = LocalDate.of(2009, 12, 23);
#Before
public void setUp() {
HashSet<LocalDate> holidays = new HashSet<LocalDate>();
holidays.add(LocalDate.of(2009, 12, 25)); // Friday
DefaultHolidayCalendar<LocalDate> holidayCalendar =
new DefaultHolidayCalendar<LocalDate>(holidays);
LocalDateKitCalculatorsFactory.getDefaultInstance()
.registerHolidays("example", holidayCalendar);
dateCalculator = LocalDateKitCalculatorsFactory.getDefaultInstance()
.getDateCalculator("example", HolidayHandlerType.FORWARD);
dateCalculator.setStartDate(startDate);
}
#Test
public void should_not_change_calendar_start_date_even_after_moving() {
assertThat(
dateCalculator.moveByBusinessDays(6).getStartDate(),
equalTo(startDate));
}
#Test
public void moveByBusinessDays_will_return_24_dec_2009_as_next_business_day() {
assertThat(
dateCalculator.moveByBusinessDays(1).getCurrentBusinessDate(),
equalTo(LocalDate.of(2009, 12, 24)));
}
#Test
public void moveByBusinessDays_will_return_28_dec_2009_as_two_business_days_later() {
assertThat(
dateCalculator.moveByBusinessDays(2).getCurrentBusinessDate(),
equalTo(LocalDate.of(2009, 12, 28)));
}
#Test
public void moveByDays_will_also_return_28_dec_2009_as_two_business_days_later() {
assertThat(
dateCalculator.moveByDays(2).getCurrentBusinessDate(),
equalTo(LocalDate.of(2009, 12, 28)));
}
#Test
public void moveByBusinessDays_will_exclude_25_26_and_27_dec_when_computing_business_days() {
assertThat(
dateCalculator.moveByBusinessDays(5).getCurrentBusinessDate(),
equalTo(LocalDate.of(2009, 12, 31)));
}
#Test
public void moveByDays_will_include_25_26_and_27_dec_when_computing_business_days() {
assertThat(
dateCalculator.moveByDays(5).getCurrentBusinessDate(),
equalTo(LocalDate.of(2009, 12, 28)));
}
}
The library defaults the working week to be from Monday to Friday, but you can change the defaults by supplying a custom WorkingWeek to DateCalculator's setWorkingWeek().
As shown in the last two examples, moveByDays() includes the weekends when moving the days, whereas moveByBusinessDays() excludes weekends.
The library also allows you to use java.util.Calendar or Joda Time's LocalDate. The examples use JDK8's java.time.LocalDate because it is the preferred way since JDK8.
Edit: Updated examples to use java.time.LocalDate
Use:
public Calendar addBusinessDate(Calendar cal, int numBusinessDays) {
int numNonBusinessDays = 0;
for(int i = 0; i < numBusinessDays; i++) {
cal.add(Calendar.DATE, 1);
/*
It's a Canadian/American custom to get the Monday (sometimes Friday) off
when a holiday falls on a weekend.
*/
for(int j = 0; j < holidays; j++) { //holidays is list of dates
if(cal.getTime() == (Date)holidays.get(j)) {
numNonBusinessDays++;
}
}
if(cal.get(Calendar.DAY_OF_WEEK) == 1 ||
cal.get(Calendar.DAY_OF_WEEK) == 7) {
numNonBusinessDays++;
}
}
if(numNonBusinessDays > 0) {
cal.add(Calendar.DATE, numNonBusinessDays);
}
return cal;
}
You'd have to populate a list of dates in order to handle holidays. There's common ones like New Years, but Thanksgiving is different between Canada & the US for instance. Also mind that holidays can fall on a weekend, so the weekend becomes a 3 day weekend.
Reference:
Calendar
Calendar Constant Values
PS: There isn't really a need to return the Calendar instance if you are updating the value as in the example. But it is valid if you want to create a separate Calendar instance, use:
public Calendar addBusinessDate(Calendar cal, int numBusinessDays) {
Calendar cal2 = Calendar.getInstance();
cal2.setTime(cal.getTime());
int numNonBusinessDays = 0;
for(int i = 0; i < numBusinessDays; i++) {
cal2.add(Calendar.DATE, 1);
/*
It's a Canadian/American custom to get the Monday (sometimes Friday) off
when a holiday falls on a weekend.
*/
for(int j = 0; j < holidays; j++) { //holidays is list of dates
if(cal2.getTime() == (Date)holidays.get(j)) {
numNonBusinessDays++;
}
}
if(cal2.get(Calendar.DAY_OF_WEEK) == 1 ||
cal2.get(Calendar.DAY_OF_WEEK) == 7) {
numNonBusinessDays++;
}
}
if(numNonBusinessDays > 0) {
cal2.add(Calendar.DATE, numNonBusinessDays);
}
return cal2;
}
Here is the modified version to find date calculation.
public Calendar algorithm2(int businessDays){
Calendar cal2 = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
int totalDays= businessDays/5*7;
int remainder = businessDays % 5;
cal2.add(cal2.DATE, totalDays);
switch(cal.get(Calendar.DAY_OF_WEEK)){
case 1:
break;
case 2:
break;
case 3:
if(remainder >3)
cal2.add(cal2.DATE,2);
break;
case 4:
if(remainder >2)
cal2.add(cal2.DATE,2);
break;
case 5:
if(remainder >1)
cal2.add(cal2.DATE,2);
break;
case 6:
if(remainder >1)
cal2.add(cal2.DATE,2);
break;
case 7:
if(remainder >1)
cal2.add(cal2.DATE,1);
break;
}
cal2.add(cal2.DATE, remainder);
return cal2;
}
//supports negative numbers too.
private Calendar addBusinessDay(final Calendar cal, final Integer numBusinessDays)
{
if (cal == null || numBusinessDays == null || numBusinessDays.intValue() == 0)
{
return cal;
}
final int numDays = Math.abs(numBusinessDays.intValue());
final int dateAddition = numBusinessDays.intValue() < 0 ? -1 : 1;//if numBusinessDays is negative
int businessDayCount = 0;
while (businessDayCount < numDays)
{
cal.add(Calendar.DATE, dateAddition);
//check weekend
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
{
continue;//adds another day
}
//check holiday
if (isHoliday(cal))//implement isHoliday yourself
{
continue;//adds another day
}
businessDayCount++;
}
return cal;
}
public static Date addBusinessDays(Date date, int days) {
DateTime result = new DateTime(date);
result = isWeekEnd(result)
? getPreviousBusinessDate(result)
: result;
for (int i = 0; i < days; i++) {
if (isWeekEnd(result)) {
i--;
}
result = result.plusDays(1);
}
return result.toDate();
}
private static boolean isWeekEnd(DateTime dateTime) {
int dayOfWeek = dateTime.getDayOfWeek();
return dayOfWeek == DateTimeConstants.SATURDAY || dayOfWeek == DateTimeConstants.SUNDAY;
}
private static DateTime getPreviousBusinessDate(DateTime result) {
while (isWeekEnd(result)) {
result = result.minusDays(1);
}
return result;
}
Will this work? Of course, this is not handling holidays.
public static Date
addBusinessDays(Date baseDate, int
numberOfDays){
if(baseDate == null){
baseDate = new Date();
}
Calendar baseDateCal = Calendar.getInstance();
baseDateCal.setTime(baseDate);
for(int i = 0; i < numberOfDays; i++){
baseDateCal.add(Calendar.DATE,1);
if(baseDateCal.get(Calendar.DAY_OF_WEEK)
== Calendar.SATURDAY){
baseDateCal.add(Calendar.DATE,2);
}
}
return baseDateCal.getTime();
}
tl;dr
Going forward.
myLocalDate.with(
org.threeten.extra.Temporals.nextWorkingDay()
)
Going backward.
myLocalDate.with(
org.threeten.extra.Temporals.previousWorkingDay()
)
Using java.time
The Question and other Answers use the troublesome old date-time classes, now legacy, supplanted by the java.time classes.
Also, see my Answer to a similar Question.
TemporalAdjuster
In java.time, the TemporalAdjuster interface provides for classes to manipulate date-time values. Using immutable objects, a new instance is created with values based on the original.
nextWorkingDay
The ThreeTen-Extra project extend java.time with additional functionality. That includes a nextWorkingDay adjuster that skips over Saturday and Sunday days. So we can loop, incrementing a date one day at a time, and skip over any weekend days.
The LocalDate class represents a date-only value without time-of-day and without time zone.
LocalDate start = LocalDate.now( ZoneId.of( "America/Montreal" ) ) ;
int businessDaysToAdd = 13 ;
// … ensure that: ( businessDaysToAdd >= 0 )
int daysLeft = businessDaysToAdd ;
LocalDate localDate = start ;
while ( daysLeft > 0 ) {
localDate = localDate.with( Temporals.nextWorkingDay() );
daysLeft = ( daysLeft - 1 ) ; // Decrement as we go.
}
return localDate ;
Holidays
Holidays are an entirely different matter. Obviously there is no simple solution. You must either supply a list of your honored holidays, or obtain a list with which you agree.
Once you have such a list, I suggest writing your own implementation of TemporalAdjuster similar to nextWorkingDay.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
Java SE 8 and SE 9 and later
Built-in.
Part of the standard Java API with a bundled implementation.
Java 9 adds some minor features and fixes.
Java SE 6 and SE 7
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
This algorithm calculates the next business date for a given date
(business days are from monday to friday in my country), you can adapt it to iterate the number of days you need to add.
public Calendar nextBusinessDate(Calendar cal) {
List<Calendar> holidays = ********
// Here get list of holidays from DB or some other service...
GregorianCalendar calCp = new GregorianCalendar();
calCp.setTime(cal.getTime());
calCp.add(Calendar.DAY_OF_MONTH, 1);
boolean isSaturday = (calCp.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY);
boolean isSunday = (calCp.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY);
boolean isHoliday = holidays.contains(calCp);
while (isSaturday || isSunday || isHoliday) {
if (isSaturday) {
calCp.add(Calendar.DAY_OF_MONTH, +2); // is saturday, make it monday
} else {
if (isSunday) {
calCp.add(Calendar.DAY_OF_MONTH, +1); // is sunday, make it monday
} else {
if (isHoliday) {
calCp.add(Calendar.DAY_OF_MONTH, +1); // is holiday, make it next day
}
}
}
calCp = new GregorianCalendar();
calCp.setTime(cal.getTime());
isSaturday = (calCp.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY);
isSunday = (calCp.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY);
isHoliday = holidays.contains(calCp);
} // end while
return calCp;
}
O(1) version that works and supports different weekend patterns and negative days:
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class DateUtil {
//Weekend patterns
public static final int WEEKEND_SAT_SUN = 0;
public static final int WEEKEND_FRI_SAT = 1;
public static final int WEEKEND_THU_FRI = 2;
public static final int WEEKEND_FRI_SUN = 3;
public static final int WEEKEND_FRI = 4;
public static final int WEEKEND_SAT = 5;
public static final int WEEKEND_SUN = 6;
//Weekend pattern by country
//#see https://en.wikipedia.org/wiki/Workweek_and_weekend
public static Map<String,Integer> weekendPatternByCountry = new HashMap<>();
static {
weekendPatternByCountry.put("CO",WEEKEND_SUN); //Colombia
weekendPatternByCountry.put("GQ",WEEKEND_SUN); //Equatorial Guinea
weekendPatternByCountry.put("IN",WEEKEND_SUN); //India
weekendPatternByCountry.put("MX",WEEKEND_SUN); //Mexico
weekendPatternByCountry.put("KP",WEEKEND_SUN); //North Korea
weekendPatternByCountry.put("UG",WEEKEND_SUN); //Uganda
weekendPatternByCountry.put("BN",WEEKEND_FRI_SUN); //Brunei Darussalam
weekendPatternByCountry.put("DJ",WEEKEND_FRI); //Djibouti
weekendPatternByCountry.put("IR",WEEKEND_FRI); //Iran
weekendPatternByCountry.put("AF",WEEKEND_THU_FRI); //Afghanistan
weekendPatternByCountry.put("NP",WEEKEND_SAT); //Nepal
weekendPatternByCountry.put("DZ",WEEKEND_FRI_SAT); //Algeria
weekendPatternByCountry.put("BH",WEEKEND_FRI_SAT); //Bahrain
weekendPatternByCountry.put("BD",WEEKEND_FRI_SAT); //Bangladesh
weekendPatternByCountry.put("EG",WEEKEND_FRI_SAT); //Egypt
weekendPatternByCountry.put("IQ",WEEKEND_FRI_SAT); //Iraq
weekendPatternByCountry.put("IL",WEEKEND_FRI_SAT); //Israel
weekendPatternByCountry.put("JO",WEEKEND_FRI_SAT); //Jordan
weekendPatternByCountry.put("KW",WEEKEND_FRI_SAT); //Kuwait
weekendPatternByCountry.put("LY",WEEKEND_FRI_SAT); //Libya
weekendPatternByCountry.put("MV",WEEKEND_FRI_SAT); //Maldives
weekendPatternByCountry.put("MR",WEEKEND_FRI_SAT); //Mauritania
weekendPatternByCountry.put("MY",WEEKEND_FRI_SAT); //Malaysia
weekendPatternByCountry.put("OM",WEEKEND_FRI_SAT); //Oman
weekendPatternByCountry.put("PS",WEEKEND_FRI_SAT); //Palestine
weekendPatternByCountry.put("QA",WEEKEND_FRI_SAT); //Qatar
weekendPatternByCountry.put("SA",WEEKEND_FRI_SAT); //Saudi Arabia
weekendPatternByCountry.put("SD",WEEKEND_FRI_SAT); //Sudan
weekendPatternByCountry.put("SY",WEEKEND_FRI_SAT); //Syria
weekendPatternByCountry.put("AE",WEEKEND_FRI_SAT); //United Arab Emirates
weekendPatternByCountry.put("YE",WEEKEND_FRI_SAT); //Yemen
}
//Adjustment vectors - precomputed adjustment
static int[][][] adjVector = new int[][][]{
{//WEEKEND_SAT_SUN
//Positive number of days
{1,0,-1,-2,-3,1,1},
{0,0},
{0,0,0,0,0,2,1},
//Negative number of days
{-1,3,2,1,0,-1,-1},
{0,0},
{-1,1,1,1,1,1,0}
},
{//WEEKEND_FRI_SAT
//Positive number of days
{0,-1,-2,-3,1,1,1},
{0,0},
{0,0,0,0,2,1,0},
//Negative number of days
{3,2,1,0,-1,-1,-1},
{0,0},
{1,1,1,1,1,0,-1}
},
{//WEEKEND_THU_FRI
//Positive number of days
{-1,-2,-3,1,1,1,0},
{0,0},
{0,0,0,2,1,0,0},
//Negative number of days
{2,1,0,-1,-1,-1,3},
{0,0},
{1,1,1,1,0,-1,1}
},
{//WEEKEND_FRI_SUN
//Positive number of days
{0,-1,-2,-3,-4,-4,0},
{1,0},
{0,0,0,0,0,-1,1},
//Negative number of days
{4,3,2,1,0,0,4},
{0,-1},
{1,1,1,1,1,0,2}
},
{//WEEKEND_FRI
//Positive number of days
{-1,-2,-3,-4,1,1,0},
{0},
{0,0,0,0,1,0,0},
//Negative number of days
{3,2,1,0,-1,-1,4},
{0},
{1,1,1,1,1,0,1}
},
{//WEEKEND_SAT
//Positive number of days
{0,-1,-2,-3,-4,1,1},
{0},
{0,0,0,0,0,1,0},
//Negative number of days
{4,3,2,1,0,-1,-1},
{0},
{1,1,1,1,1,1,0}
},
{//WEEKEND_SUN
//Positive number of days
{1,0,-1,-2,-3,-4,1},
{0},
{0,0,0,0,0,0,1},
//Negative number of days
{-1,4,3,2,1,0,-1},
{0},
{0,1,1,1,1,1,1}
}
};
//O(1) algorithm to add business days.
public static Date addBusinessDays(Date day, int days,int weekendPattern){
Calendar ret = Calendar.getInstance();
if(day != null) {
ret.setTime(day);
}
if(days != 0) {
int startDayofWeek = ret.get(Calendar.DAY_OF_WEEK)-1; //Zero based to use the vectors bellow.
int idx = days > 0 ? 0 : 3;
int howManyWeekendDays = 0;
int[][] adjV = adjVector[weekendPattern];
int numWeekendDaysInOneWeek = adjV[idx+1].length;
for(int i = 0; i < numWeekendDaysInOneWeek;i++){
int adjustmentA = adjV[idx][startDayofWeek]; //pattern shift
int adjustmentB = adjV[idx+1][i]; //day shift
howManyWeekendDays += (days-adjustmentA-adjustmentB)/(7-numWeekendDaysInOneWeek);
}
int adjustmentC = adjV[idx+2][startDayofWeek]; //f(0) adjustment
howManyWeekendDays += adjustmentC;
ret.add(Calendar.DATE,days + howManyWeekendDays);
//TODO: Extend to support holidays using recursion
// int numHolidays = getNumHolidaysInInterval(day,ret.getTime());
// if(numHolidays > 0) return addBusinessDays(ret.getTime,numHolidays);
}
return ret.getTime();
}
public static Date addBusinessDays(Date day, int days,String country){
Integer weekpat = weekendPatternByCountry.get(country);
return weekpat != null ? addBusinessDays(day,days,weekpat) : addBusinessDays(day,days,WEEKEND_SAT_SUN);
}
}
This is the method I came up with:
private Date addLaborDays(Integer days, Date date){
Collection<Date> holidaysList = getHolidays();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 1);
Date dateTemp = cal.getTime();
if(days == 1) return dateTemp;
if(holidaysList.contains(dateTemp) || DateUtil.isWeekend(dateTemp)){
return addLaborDays(days, dateTemp);
} else {
return addLaborDays(days-1, dateTemp);
}
}
Method getHolidays() queries a custom holidays database table, and method DateUtil.isWeekend(dateTemp) returns true if dateTemp is Saturday or Sunday.
/* To Calculate 10 business days ahead of today's date
*/
public class DueDate {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DueDate d = new DueDate();
String dueDate = d.getDueDate(10);
System.out.println("due Date " + dueDate);
}
public String getDueDate(int bday){
Calendar cal = new GregorianCalendar();
SimpleDateFormat fdate = new SimpleDateFormat("MM/dd/yyyy");
while(bday > 0){
cal.add(Calendar.DAY_OF_MONTH, 1);
if(noWeekendsorHolidays(cal)){
bday--;
}
}
return fdate.format(cal.getTime());
}
public boolean noWeekendsorHolidays(Calendar cal){
int day = cal.get(Calendar.DAY_OF_WEEK);
if(day == 1 || day == 7){
return false;
}
return true;
}
}
This one works for me, short and simple:
public static Date getBusinessDay(final Date date, final int businessDaysFromDate) {
final int max = 60;
if (date == null) {
return getBusinessDay(new Date(), businessDaysFromDate);
} else if (date != null && (businessDaysFromDate < 0 || businessDaysFromDate > max)) {
return getBusinessDay(date, 0);
} else {
final Calendar baseDateCal = Calendar.getInstance();
baseDateCal.setTime(date);
for (int i = 1; i <= businessDaysFromDate; i++) {
baseDateCal.add(Calendar.DATE, 1);
while (baseDateCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || baseDateCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
baseDateCal.add(Calendar.DATE, 1);
}
}
return baseDateCal.getTime();
}
}
Adding two business days to current date:
Date today = new Date();
Calendar cal1 = Calendar.getInstance();
cal1.setTime(today);
switch(cal1.get(Calendar.DAY_OF_WEEK)){
case 1:
cal1.add(Calendar.DATE, 2);
break;
case 2:
cal1.add(Calendar.DATE, 2);
break;
case 3:
cal1.add(Calendar.DATE, 2);
break;
case 4:
cal1.add(Calendar.DATE, 2);
break;
case 5:
cal1.add(Calendar.DATE, 4);
break;
case 6:
cal1.add(Calendar.DATE, 4);
break;
case 7:
cal1.add(Calendar.DATE, 3);
break;
}
// You may also set the time to meet your purpose:
cal1.set(Calendar.HOUR_OF_DAY, 23);
cal1.set(Calendar.MINUTE, 59);
cal1.set(Calendar.SECOND, 59);
cal1.set(Calendar.MILLISECOND, 00);
Date twoWeekdaysAhead = cal1.getTime();
Most of the answer I've found online didn't work as expected, so I tweaked an example on this thread, How to get current date and add five working days in Java. The code below appears to work better.
public static Date addWorkingDays(Date date, int days) {
if (days > 0) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int daysAdded = 0;
do {
cal.add(Calendar.DATE, 1);
if (isWorkingDay(cal)) {
daysAdded++;
}
} while (daysAdded < days);
return cal.getTime();;
} else {
return date;
}
}
private static boolean isWorkingDay(Calendar cal) {
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.SUNDAY || dayOfWeek == Calendar.SATURDAY)
return false;
// tests for other holidays here
return true;
}

Categories

Resources