如何在Java中获取当前日期并添加五个工作日 [英] How to get current date and add five working days in Java

查看:1507
本文介绍了如何在Java中获取当前日期并添加五个工作日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要两个约会。
1)当前日期,格式为MM / dd / yy
2)修改日期,该日期为当前日期起五个工作日(周一至周五)的日期,并且应采用MMM dd,yyyy格式

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.

因此,如果我的当前日期是6月9日,那么currentDate应该是2014年6月9日,而ModifyDate应该是2014年6月13日。

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

如何执行此操作?

推荐答案

这将增加工作日(星期一至星期五)并显示日期

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

已更新2020年7月6日


  1. 现在可以将自定义日期设置为用作非工作日(请参见列表NON_BUSINESS_DAYS

  2. 现在也可以计算过去的日期(将businessDays设置为负值

  1. Now custom days can be used as non working days (see the list NON_BUSINESS_DAYS)
  2. Now even the past date can be calculated as well (set businessDays as negative val)





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

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

这篇关于如何在Java中获取当前日期并添加五个工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆