0

my problem is that I need to get the business days between two dates that the user will input. I dont know what to use to compare and get the days, but there is my code.

    // Scanner
    Scanner input = new Scanner(System.in);
    System.out.println("First date: ");
    String date1 = input.nextLine();
    System.out.println("Second date: ");
    String date2 = input.nextLine();
    input.close();
    
    // The format for the Date
    DateFormat formatDate = new SimpleDateFormat("dd/MM/yyyy");
    
    // Conver String to Date
    Date date_1 = formatDate.parse(date1);
    Date date_2 = formatDate.parse(date2);
    
    // Calendar
    Calendar calendar = Calendar.getInstance();
    
    // Date to Calendar
    calendar.setTime(date_1);
    calendar.setTime(date_2);

Now I need to do something to get the business day between that dates and I dont know how

pon4ik1
  • 31
  • 5
  • 2
    [Date Time](https://docs.oracle.com/javase/tutorial/datetime/index.html) tutorial – Abra Dec 16 '20 at 19:50
  • How do you define a "business day"? What is considered a business day can based on (at least) the calendar in use, the physical location (all the way down to town borders) and the specific year. You would need a large, continuously updated database to track this. – Jim Garrison Dec 16 '20 at 19:55
  • Business days are those to work, from Monday to Friday without counting Saturday and Sunday, btw thanks for the response :) – pon4ik1 Dec 16 '20 at 19:58
  • If you are using this for anything in the real world, be sure that your definition of "business day" matches the users' expectations. An error here could have legal and/or financial implications leading to liability for you or your company. You wouldn't want to get sued because your code caused a financial error by including or excluding the wrong days in the calculation. Side note: This question exemplifies the huge difference between "coding" and "software engineering". – Jim Garrison Dec 16 '20 at 20:03
  • I recommend that you use none of the classes `DateFormat`, `SimpleDateFormat`, `Date` and `Calendar`. They are all 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. Dec 16 '20 at 20:39

2 Answers2

5

You are better off using classes from the java.time package. They are way better designed than Date, Calendar and SimpleDateFormat. Here's what's wrong with those classes.

You could create a Stream over all dates between two given dates, and then filter (retain) the business days.

LocalDate startDate = LocalDate.of(2012, 3, 7);
LocalDate endDate = LocalDate.of(2012, 6, 7);

// Static imports from java.time.DayOfWeek
final Set<DayOfWeek> businessDays = Set.of(
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY
);

List<LocalDate> allDates =

    // Generate a stream with all dates between startDate
    // and endDate (the latter is exclusive)
    startDate.datesUntil(endDate)

        // Retain all business days.
        .filter(t -> businessDays.contains(t.getDayOfWeek()))

         // Collect them into a List.
        .collect(Collectors.toList());

Note that the datesUntil method is available since Java ≥ 9. If you happen to still use Java 8, you must take a few extra steps in the place of startDate.datesUntil(endDate): calculate the number of days between those two dates, and then generate a stream limiting it to those number of days.

long numOfDaysBetween = ChronoUnit.DAYS.between(startDate, endDate);
Stream.iterate(startDate, d -> d.plusDays(1))
    .limit(numOfDaysBetween)
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
1

You can parse the input dates into LocalDate and the create a loop from the start date to the end date with a step of one day. Inside the loop, add the dates, which do not fall on Sun or Sat, to a list.

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("First date: ");
        String strDate1 = input.nextLine();
        System.out.print("Second date: ");
        String strDate2 = input.nextLine();

        LocalDate date1 = LocalDate.parse(strDate1);
        LocalDate date2 = LocalDate.parse(strDate2);

        List<LocalDate> listBusinessDates = new ArrayList<>();
        for (LocalDate date = date1; !date.isAfter(date2); date = date.plusDays(1)) {
            DayOfWeek dayOfWeek = date.getDayOfWeek();
            if (!(dayOfWeek.equals(DayOfWeek.SATURDAY) || dayOfWeek.equals(DayOfWeek.SUNDAY))) {
                listBusinessDates.add(date);
            }
        }

        System.out.println(listBusinessDates);
    }
}

A sample run:

First date: 2020-11-23
Second date: 2020-12-13
[2020-11-23, 2020-11-24, 2020-11-25, 2020-11-26, 2020-11-27, 2020-11-30, 2020-12-01, 2020-12-02, 2020-12-03, 2020-12-04, 2020-12-07, 2020-12-08, 2020-12-09, 2020-12-10, 2020-12-11]

Note that the date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API. Learn more about the modern date-time API at Trail: Date Time.

Note: For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7.

If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110