码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA 取五个工作日后的日期(仅排除周六周日)

时间:2021-05-24 06:45:07      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:chm   需要   str   form   vat   gettime   ken   end   sim   

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class test{
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private static Date getTomorrow(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, +1);
        date = calendar.getTime();
        return date;
    }
    private static boolean isWeekend(String sdate) throws ParseException {
        Date date = sdf.parse(sdate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
            return true;
        } else{
            return false;
        }

    }
    public static void main(String[] arg) throws ParseException{
        Date today = new Date();
        Date tomorrow = null;
        int delay = 1;
        int num = 5;//业务需要的n个工作日
        while(delay <= num){
            tomorrow = getTomorrow(today);
            if (!isWeekend(sdf.format(tomorrow))){
                delay++;
            }
            today = tomorrow;
        }
        LocalDateTime fDate = Instant.ofEpochMilli(today.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
        //或LocalDateTime fDate = today.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime()
    }


}

 

JAVA 取五个工作日后的日期(仅排除周六周日)

标签:chm   需要   str   form   vat   gettime   ken   end   sim   

原文地址:https://www.cnblogs.com/xiexiaoxia/p/14556235.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!