工作日计算 去掉周末

随便写了个class,比较啰嗦,可以用


/*
*@(#)WorkDay.java
*Copyright (c) 2007-2008
*All right reserved
*/
package time;

import java.text.SimpleDateFormat;
import java.util.*;


/**
*Class description goes here
*
*@author Bug Liu
*@version ewms 2.0
*@time 2011-7-6
*@description
*/
public class WorkDay {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(getDay("2011-07-01","2011-07-01"));
}
/**
*
* @param time1 开始时间
* @param time2结束时间
* @return
*/
public static int getDay(String time1, String time2) {
int day = 0;
if(time1==time2){
if(!ifWeek(time1)){
day = 0;
}else{
day = 1 ;
}
}else{
String nexttime = getNextDatetostring(time1);
while(!CompareToDate(nexttime,time2)){
if(!ifWeek(nexttime)){
day++;
}
nexttime = getNextDatetostring(nexttime);
}
}
return day;
}
/**
* 是否周末
* @param str
* @return
*/
public static boolean ifWeek(String str){
if(getWeekOfDay(str)==6||getWeekOfDay(str)==7){
return true;
}else{
return false ;
}
}
/**
* 计算星期几
* @param str
* @return
*/
public static int getWeekOfDay(String str) {

String[] arr = str.split("-");
Calendar cal = Calendar.getInstance();
cal.set(Integer.parseInt(arr[0]), Integer.parseInt(arr[1]) - 1, Integer
.parseInt(arr[2]));
return cal.get(Calendar.DAY_OF_WEEK);
}
/**
* @return 系统给定日期的下一天
*/
public static String getNextDatetostring(String date) {
java.util.Date now = getDateByString(date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String xtsj = sdf.format(dateAdd(now,1,0));
return xtsj;
}
/**
* 返回给定的日期在指定时间段后的日期,指定con为0时为天,1-月,2-年
* Description
*
* @param d 日期
* @param x 给定数目
* @param con 给定操作
* @return
*/
public static Date dateAdd(Date date, int x, int con) {

Calendar cal = Calendar.getInstance();
cal.setTime(date);

if (con == 0){

int d = cal.get(Calendar.DAY_OF_MONTH);
d = d + x;
cal.set(Calendar.DAY_OF_MONTH, d);
}
if (con == 1){
int m= cal.get(Calendar.MONTH);
m= m+x;
cal.set(Calendar.MONTH, m);
}
if (con == 2){
int y = cal.get(Calendar.YEAR);
y = y +x;
cal.set(Calendar.YEAR, y);
}
return cal.getTime();
}
/**
* 根据给定的字符串生成日期,字符串的格式为yyyy-mm-dd
* Description
*
* @param str
* @return
*/
public static Date getDateByString(String str) {

String[] arr = str.split("-");
Calendar cal = Calendar.getInstance();
cal.set(Integer.parseInt(arr[0]), Integer
.parseInt(arr[1]) - 1, Integer.parseInt(arr[2]));
return cal.getTime();
}
/**@description 比较日期大小
*
* @param date_1
* @param date_2
* @return 前面日期大,返回true
*/
public static boolean CompareToDate(String date_1,String date_2){
boolean temp = false ;
int result = date_1.compareTo(date_2);
if (result > 0) {
temp = true;
}else{
temp = false;
}
return temp ;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值