1

我对java很陌生,发现以下代码来获取不包括周六和周日的工作日数。

我需要知道如何调用这个函数main()来显示workDays当前月份。

谢谢。

package mypkg;

import java.sql.Date;
import java.util.Calendar;

public class workrays {

    public static int getWorkingDaysBetweenTwoDates(Date startDate, Date endDate) {
        Calendar startCal;
        Calendar endCal;
        startCal = Calendar.getInstance();
        startCal.setTime(startDate);
        endCal = Calendar.getInstance();
        endCal.setTime(endDate);
        int workDays = 0;

        //Return 0 if start and end are the same
        if (startCal.getTimeInMillis() == endCal.getTimeInMillis()) {
            return 0;
        }

        if (startCal.getTimeInMillis() > endCal.getTimeInMillis()) {
            startCal.setTime(endDate);
            endCal.setTime(startDate);
        }

        do {
            startCal.add(Calendar.DAY_OF_MONTH, 1);
            if (startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY
            && startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
                ++workDays;
            }
        } while (startCal.getTimeInMillis() < endCal.getTimeInMillis());

        return workDays;
    }

    public static void main(String args[]) {

        getWorkingDaysBetweenTwoDates(Date startDate, Date endDate);
        System.out.println(workdays);       
    }
}
4

2 回答 2

2

首先,您应该使用java.util.Date而不是java.sql.Date. 其次,您需要先创建Dates 实例,然后再将其发送到您的getWorkingDaysBetweenTwoDates函数。第三,您需要将getWorkingDaysBetweenTwoDates函数的结果存储在局部变量中,然后使用它。为了创建Dates,您可以使用一个SimpleDateFormat对象:

public static void main(String[] args) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    try {
        Date startDate = sdf.parse("2013-02-20");
        Date endDate = sdf.parse("2013-02-22");
        int workdays = getWorkingDaysBetweenTwoDates(startDate, endDate);
        System.out.println(workdays);
    } catch (Exception e) {
        System.out.println("There's an error somewhere. Check the stacktrace to find it");
        e.printStackTrace(System.out);
    }
}

根据您的评论,您可以创建另一个函数,该函数Date通过发送以下内容返回具有第一个日期值的实例Date

public static Date getFirstDateOfMonth(Date aDate) {
    Calendar c = Calendar.getInstance();
    c.setTime(aDate);
    c.set(Calendar.DATE, 1);
    return c.getTime();
}

你可以这样称呼它:

public static void main(String[] args) {
   Date endDate = new Date(); //current date/time
   Date startDate = getFirstDateOfMonth(endDate);
   int workdays = getWorkingDaysBetweenTwoDates(startDate, endDate);
   System.out.println(workdays);
}
于 2013-02-23T00:00:28.507 回答
0

您需要传递如下引用:

Date startDate = new Date();
Date endDate = new Date();
getWorkingDaysBetweenTwoDates(startDate, endDate);

或一个班轮:

getWorkingDaysBetweenTwoDates(new Date(), new Date());

当然,您必须在创建它们时提供开始和结束日期的值,通常使用java.util.Calendar. 您也可以使用 Date 方法来实现它,但它们已被弃用。

编辑:

这就是您java.util.Date使用来自的方法进行操作的方式java.util.Calendar

Date startDate = new Date();//current date in milliseconds
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
cal.set(Calendar.DATE, 1);
于 2013-02-22T23:54:50.340 回答