工作日计算学习

计算两个日期段之间有多少个工作日

DECLARE @bt DATETIME,@et DATETIME   
SELECT @bt='20090801',@et='20090913'  
  
;WITH t AS   
(   
  SELECT DATEDIFF(dd,@bt,@et)+1 AS [days],   
  DATEPART(w,@bt+@@DATEFIRST-1) AS [wd] --@@DATEFIRST-1 按周一为一周的第一天查询   
)   
SELECT [Workdays]=[days]/7*5+[days]%7   
    - CASE WHEN 6 BETWEEN [wd] AND [wd]+[days]%7-1 THEN 1 ELSE 0 END   
    - CASE WHEN 7 BETWEEN [wd] AND [wd]+[days]%7-1 THEN 1 ELSE 0 END   
FROM t   
/*  
Workdays  
-----------  
30  
*/  



计算工作日和自然日
用Java写一个倒计时程序,比如输入一个日期,格式为20090501,可以计算从当前日期开始到20090501有多少个工作日和多少个自然日


import java.text.ParseException;      
import java.text.SimpleDateFormat;      
import java.util.Calendar;      
import java.util.Date;      
     
public class Test1 {      
    public static void main(String[] args) {      
        String time = "20090510";      
        int day1 = 0;// 代表自然日      
        int day2 = 0;// 代表休息日,假定周六周日休息      
        Calendar ca = Calendar.getInstance();      
        Calendar now = Calendar.getInstance();      
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");      
        try {      
            Date date = sdf.parse(time);      
            ca.setTime(date);      
            System.out.println(sdf.format(ca.getTime()));      
        } catch (ParseException e) {      
            // TODO Auto-generated catch block      
            e.printStackTrace();      
        }      
     
        String ss = sdf.format(ca.getTime());      
        if (now.before(ca)) {      
            while (!sdf.format(now.getTime()).equals(ss)) {// 原本想使用这个方法ca.compareTo(now)!=0不好使      
                if (now.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY      
                        || now.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {      
                    day2++;      
                }      
                day1++;      
                now.add(Calendar.DAY_OF_YEAR, 1);      
            }      
        }      
     
        System.out.println("到" + time + "有" + day2 + "休息日");      
        System.out.println("到" + time + "有" + day1 + "自然日");      
    }      
} 
 

你可能感兴趣的:(java,工作)