如何获取当前日期并在 Java 中添加五个工作日

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24114155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:13:58  来源:igfitidea点击:

How to get current date and add five working days in Java

javadatedatetime

提问by Anup Ganatra

I want two dates. 1) Current date in MM/dd/yy format 2) Modified date which will be the adition of five business days(Mon-Fri) to current date and it should be in MMM dd, yyyy format.

我要两个约会。1) MM/dd/yy 格式的当前日期 2) 修改后的日期,即在当前日期的基础上增加五个工作日(周一至周五),它应该是 MMM dd, yyyy 格式。

So if my current is 9th june than currentDate should be 06/09/14 and modifiedDate should be Jun 13, 2014.

因此,如果我的当前日期是 6 月 9 日,则 currentDate 应该是 06/09/14,而 modifiedDate 应该是 2014 年 6 月 13 日。

How to do this?

这该怎么做?

采纳答案by Himanshu Tyagi

This will add working days (Mon-Fri) and will present dates in the required format.

这将增加工作日(周一至周五)并以所需格式显示日期。

    Date date=new Date();
    Calendar calendar = Calendar.getInstance();
    date=calendar.getTime(); 
    SimpleDateFormat s;
    s=new SimpleDateFormat("MM/dd/yy");

    System.out.println(s.format(date));

    int days = 5;
    for(int i=0;i<days;)
    {
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        //here even sat and sun are added
        //but at the end it goes to the correct week day.
        //because i is only increased if it is week day
        if(calendar.get(Calendar.DAY_OF_WEEK)<=5)
        {
            i++;
        }

    }
    date=calendar.getTime(); 
    s=new SimpleDateFormat("MMM dd, yyyy");
    System.out.println(s.format(date));

Ref : https://stackoverflow.com/a/15339851/3603806and https://stackoverflow.com/a/11356123/3603806

参考:https: //stackoverflow.com/a/15339851/3603806https://stackoverflow.com/a/11356123/3603806

回答by indika

Here is the code sample to add dates. You may modify in order to you can only add business days.

这是添加日期的代码示例。您可以修改以便您只能添加工作日。

    SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yy");
    SimpleDateFormat sdf2 = new SimpleDateFormat("MMM dd, yyyy");
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());

    System.out.println(sdf1.format(calendar.getTime()));
    calendar.add(Calendar.DATE,6);
    System.out.println(sdf2.format(calendar.getTime()));

回答by Oliv

The notion of working days is not implemented in Java, it's too subject to interpretation (for example, many international companies have their own holidays). Code below uses isWorkingDay(), which only returns false for weekends - add your holidays there.

工作日的概念在 Java 中没有实现,它太容易解释(例如,许多国际公司都有自己的假期)。下面的代码使用isWorkingDay(),它只在周末返回 false - 在那里添加你的假期。

public class Test {

    public static void main(String[] args) {
        Calendar cal = new GregorianCalendar();
        // cal now contains current date
        System.out.println(cal.getTime());

        // add the working days
        int workingDaysToAdd = 5;
        for (int i=0; i<workingDaysToAdd; i++)
            do {
                cal.add(Calendar.DAY_OF_MONTH, 1);
            } while ( ! isWorkingDay(cal));
        System.out.println(cal.getTime());
    }

    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;
    }

}