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

如何在Java中获取当前日期并添加五个工作日,java,date,datetime,Java,Date,Datetime,我想要两次约会。 1) 当前日期(MM/dd/yy格式) 2) 修改后的日期,将是当前日期的五个工作日(周一至周五),且应为MMM dd,yyyy格式 因此,如果我的当前日期是6月9日,那么当前日期应该是2014年9月6日,修改日期应该是2014年6月13日 如何做到这一点?以下是添加日期的代码示例。您可以修改,以便只添加工作日 SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yy"); SimpleDateFormat s

我想要两次约会。 1) 当前日期(MM/dd/yy格式) 2) 修改后的日期,将是当前日期的五个工作日(周一至周五),且应为MMM dd,yyyy格式

因此,如果我的当前日期是6月9日,那么当前日期应该是2014年9月6日,修改日期应该是2014年6月13日


如何做到这一点?

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

    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()));

工作日的概念没有在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;
    }

}
公共类测试{
公共静态void main(字符串[]args){
Calendar cal=新的Gregorianalendar();
//cal现在包含当前日期
System.out.println(cal.getTime());
//添加工作日
int workingDaysToAdd=5;

对于(int i=0;i这将添加工作日(周一至周五),并以所需格式显示日期

于2020年7月6日更新

  • 现在,自定义日可以用作非工作日(请参阅非工作日列表)
  • 现在甚至可以计算过去的日期(将businessDays设置为负val)

  • import java.text.simpleDataFormat;
    导入java.util.array;
    导入java.util.Calendar;
    导入java.util.Date;
    导入java.util.List;
    公共类示例{
    私有静态最终列表非工作日=Arrays.asList(
    日历,星期六,
    日历,星期日
    );
    /**
    *返回过去或未来的业务日期
    *@param日期开始日期
    *@param businessDays要加/减的工作日数
    *
    注意:将其设置为负值以获取过期日期 *@按“工作日数”值返回过去或未来的工作日期 */ public static Date businessDays from(日期日期,int businessDays){ 日历=Calendar.getInstance(); 日历。设置时间(日期); 对于(int i=0;i0?1:-1); //但最后它会转到正确的工作日。 //因为我只有在一周的时候才会增加 如果(!NON_BUSINESS_DAYS.contains(calendar.get(calendar.DAY OF_WEEK))){ i++; } } 返回calendar.getTime(); } 公共静态void main(字符串…字符串){ SimpleDataFormat s=新的SimpleDataFormat(“MM/dd/yy(MMM dd,yyyy)”; 日期=新日期(); int个工作日=5天; System.out.println(s.format(date)); 系统输出打印(“+”+工作日+“工作日=”); System.out.println(s.format(businessDaysFrom(date,businessDays)); 系统输出打印(“-”+工作日+”工作日=”); System.out.println(s.format(businessDaysFrom(date,-1*businessDays)); } }

    Date-Date=新日期();
    日历=Calendar.getInstance();
    date=calendar.getTime();
    简化格式s;
    s=新的简化格式(“MM/dd/yy”);
    System.out.println(s.format(date));
    整数天=5天;
    
    对于(int i=0;如果问题的格式部分:你应该明白,当你需要代码方面的帮助时,这个网站会帮助你。但他们不能代表你做研究?所以,尝试一些东西,并寻求代码方面的帮助。尝试使用JODA framwork@Meno重新打开它,你的链接可能比我最初标记的更近,尽管工作日可能瓦里认为你有一个小小的错误:他想增加5天而不是6天……在这里,你只需增加6个日历日,不管它们是否是工作日。因此,这并不能解决实际问题。我省略了格式,请参阅其他答案一些国家在星期日星期四有工作周。作为一个天真的解决方案ion这将起作用,但请注意日期-时间日历边缘情况。参考:此解决方案将星期五和星期六视为周末。calendar.DAY\u of_WEEK从星期日开始(星期日的值为1)。方法
    businessDaysFrom()
    包含错误。第二行应为
    calendar.setTime(日期);
    @Hassenboy你说得对,我从以前的解决方案中带了这个,错过了更新!更新!谢谢
    import java.text.SimpleDateFormat;
    import java.util.Arrays;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.List;
    
    public class BusinessDateExamples {
    
        private static final List<Integer> NON_BUSINESS_DAYS = Arrays.asList(
                    Calendar.SATURDAY,
                    Calendar.SUNDAY
                );
        
        /**
         * Returns past or future business date
         * @param date starting date
         * @param businessDays number of business days to add/subtract
         *          <br/>note: set this as negative value to get past date
         * @return past or future business date by the number of businessDays value
         */
        public static Date businessDaysFrom(Date date, int businessDays) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            for (int i = 0; i < Math.abs(businessDays);) {
                // here, all days are added/subtracted
                calendar.add(Calendar.DAY_OF_MONTH, businessDays > 0 ? 1 : -1);
                
                // but at the end it goes to the correct week day.
                // because i is only increased if it is a week day
                if (!NON_BUSINESS_DAYS.contains(calendar.get(Calendar.DAY_OF_WEEK))){
                    i++;
                }
            }
            return calendar.getTime();
        }
        
        public static void main(String...strings) {
            SimpleDateFormat s = new SimpleDateFormat("MM/dd/yy ( MMM dd, yyyy )");
            
            Date date = new Date();
            int businessDays = 5;
            
            System.out.println(s.format(date));
            
            System.out.print("+ " + businessDays + " Business Days = ");
            System.out.println(s.format(businessDaysFrom(date, businessDays)));
            
            System.out.print("- " + businessDays + " Business Days = ");
            System.out.println(s.format(businessDaysFrom(date, -1 * businessDays)));
        }
    }
    
        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));