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

问题描述

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

  1. 现在,自定义日可以用作非工作日( 请参阅列表NON_BUSINESS_DAYS
  2. 现在甚至可以计算过去的日期( 将businessDays设置为负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));

解决方法

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

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

这该怎么做?

猜你在找的技术问答相关文章

如何检查配对的蓝牙设备是打印机还是扫描仪(Android)
是否允许实体正文进行HTTP DELETE请求?
如何将ZipInputStream转换为InputStream?
java.util.logging Java 8中的变量
PowerMockito.doReturn返回null
Java中的RESTful调用
Swing / Java:如何正确使用getText和setText字符串
特殊字符和重音字符
Android Studio中的ndk.dir错误
错误“找不到主类”