0

I want to point to the next working day for a given day using java. BUT the conditions goes on increasing when i am using own calendar api to manipulate dates since I had to consider the usual weekends and the pointing to the next day and also i had to consider the regional holidays in my region.

Because I have to pass the date to calendar api to check the day is working or not.If the day the working then fine . If the day is a holiday how can i get the next working day(Because i have to pass date to api to check whether it is holiday or not). Do i have to use infinite loop and keep on hitting the api by incrementing the day each time. and check the api response everytime

The Restapi is I am using is external api which we have to use to check the holidays. I can't save holidays in Database.

If 27th,28th is holiday and 29th is working ,how can i pass the dates to this api.

API Request :

 {
    "Data": {

        "Date": "27/04/2020"
    }
 }

API Response :

{
  "Data": {
    "Holidaystatus":"Is Holiday"
  }
}

Please help!

shiv tej
  • 13
  • 6
  • I would suggest maintaining the holiday list table in the database. https://blogs.oracle.com/sql/how-to-find-the-next-business-day-and-add-or-subtract-n-working-days-with-sql https://stackoverflow.com/a/9208532/6821936 – Uday Chauhan Apr 27 '20 at 05:40
  • Hi Uday chauhan ,thanks for commenting but i have to consume api. – shiv tej Apr 27 '20 at 05:52

2 Answers2

0

You need to create a data structure (Map or Database table) that holds pairs: <holiday start day, holiday end day> Remember to include weekend.

So in your example you will keep a entry

<27/04/2020,28/04/2020>

You don't need to iterate over days, just. keep the whole period as single entry.

Now looking for next working day can be described in simple steps:

  1. get input date
  2. if day does not exist in data structure. - it's a working day
  3. if day exists in structure, get value for given day (holiday end) and add single day, because next day after a holiday is a working day

With this approach you need to do more operations, when adding new holiday. It's quite similar:

  1. Check if holiday already exists, if yes, then do nothing.
  2. Check day after and before - if a holiday exists there, then add this day to this holiday (merge).

You could create another REST API to add holidays.

If you are using Jdk8+, you could use the support of LocalDate:

    LocalDate now = LocalDate.now();
    boolean isWeekend = now.getDayOfWeek() == DayOfWeek.SUNDAY || now.getDayOfWeek() == DayOfWeek.SATURDAY;
Beri
  • 11,470
  • 4
  • 35
  • 57
  • Thanks Beri,Thanks for response but I am not sure,how to do it because I have keep on adding dates in check them?..it will be great if you could help with code. – shiv tej Apr 27 '20 at 06:16
  • You can find more in this thread https://stackoverflow.com/questions/3388673/holidays-is-there-a-java-implementation – Beri Apr 27 '20 at 06:37
  • The Restapi is I am using is external which we have to use to check the holidays,The data-structure which i am going to create,I should save somewhere in database to check everytime whether is date exists , I dont access to database and If i re-run the application then?Please,Correct me if I am wrong – shiv tej Apr 27 '20 at 06:38
  • Your data structure -persisting holidays - should not be in memory only (you will lost all data after app restarts). You could work with holidays in a in-memory map, but keep their copy in a local file. Persist the file after each holiday is added/removed and read file when app starts. – Beri Apr 27 '20 at 06:42
  • Thanks Beri, I am excluding the saturday and sunday now,but not able to find next working day,but I don't have permission as to store in file in server , I can only write logic in code with rest api response. – shiv tej Apr 27 '20 at 07:02
0

After trying everything , I Found this.

public static String incrementDaysExcludingWeekends(String format,String datetoincrement,String daystoincrement) throws ParseException, IOException {
    // format of date is passed as an argument
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    // base date which will be incremented
    String dateStr = datetoincrement;
    Date date = sdf.parse(dateStr);
    Calendar c = Calendar.getInstance();
    // set calendar time with given date
    c.setTime(date);
    // number of days to increment
    int maxIncrement = Integer.parseInt(daystoincrement);
    // add days to date 
    c.add(Calendar.DAY_OF_WEEK, maxIncrement);
    // check if the date after addition is a working day. 
    // If not then keep on incrementing it till it is a working day
    while(!isWorkingDay(c.getTime(), c)) {
        c.add(Calendar.DAY_OF_WEEK, 1);
    }
      return sdf.format(c.getTime());
}

private static boolean isWorkingDay(Date date, Calendar calendar) throws IOException {
    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

    // set calendar time with given date
    HolidayApiImpl holiday = new HolidayApiImpl();
    JSONObject insidejson = new JSONObject();
    insidejson.put("BankingDate", df.format(date));
    JSONObject datajson = new JSONObject();
    datajson.put("Data", insidejson);
    String holidayapi = holiday.holidayapi(datajson.toString());

    JSONObject holidayjson = new JSONObject(holidayapi);
    String holidaydata = holidayjson.getJSONObject("Data").getString("HolidayStatus");
    calendar.setTime(date);


    int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

    if(holidaydata.equals("HoliDay")) {

        return false;


    }
    return true;
 }

Thanks Beri,for the suggestions.

shiv tej
  • 13
  • 6
  • I recommend you don’t use `SimpleDateFormat`, `DateFormat`, `Date` and `Calendar`. Those classes are poorly designed and long outdated, the first two in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Apr 27 '20 at 18:07
  • 1
    Sure Ole,will do that. – shiv tej Apr 28 '20 at 05:53