当前位置: 首页 > 知识库问答 >
问题:

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

贺文彬
2023-03-14

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

这怎么做?

共有1个答案

吕霖
2023-03-14

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

更新于2020年7月6日

  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日。 这该怎么做? 问题答案: 这将增加工作日(周一至周五),并以所需格式显示日期。 2020年7月6日更新

  • 问题内容: 如何在JavaScript中获取当前日期? 问题答案: 使用生成一个新的包含当前日期和时间对象。 这将以mm / dd / yyyy的格式给您今天的日期。 只需更改为所需的任何格式。

  • 问题内容: 要插入到列中。我正在使用 数据库。我的问题是: SQL中有可用的功能吗?或者 它取决于实现,因此每个数据库对此都有自己的功能? MySQL中可用的功能是什么? 问题答案: 完整答案: 1. SQL中有可用的功能吗? 是的,SQL 92规范,97年10月,第pg。171第6.16节指定了以下功能: 2.它是依赖于实现的,因此每个数据库对此都有自己的功能吗? 每个数据库都有其自己的实现,但

  • 问题内容: 过去几个小时以来,我一直在网上搜索以获取系统时区中的日期时间。 当我使用calendar.getTimezone.getDefaultName时,它总是返回我格林尼治标准时间。(理想情况下,它应该返回我当前的时区,即IST)。我正在尝试将此字符串(“格林尼治标准时间”)“ 2014-02-14T06:04:00:00”转换为我的时区datetime。它总是在格林尼治标准时间同时返回我。

  • 我想根据月份对值进行分类,以便第一次过滤这些值 例如:-

  • 问题内容: 我有一个在工作日(周一至周五)运行的sis软件包。如果我在星期二(background(DB))收到文件,则它需要前一个工作日的日期并进行一些交易。如果我在星期五运行工作,则必须获取星期一日期并处理交易。 我已使用以下查询获取之前的营业日期 如果我执行此查询,我将获得昨天的Transactios的结果。如果我在星期一运行相同的查询,则必须获取星期五的交易而不是星期日。 问题答案: 我更