时间处理实用类
/**
* File: DateUtilEx.java
* All right reserved.
* Author qiuren
* Date 2010/12/24
*
*/
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
*
* 各种日期,时间处理实用类
*
*/
public class DateUtil
{
public static void printTime(String hint){
System.out.println(getYMDHMSS() + ":" + hint);
}
/**
* 得到系统当前年,格式 yyyy
* @return 日期
*/
public static String getYear(){
String strYear="";
Date currentDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy");
strYear= formatter.format(currentDate);
return strYear;
}
/**
* 得到系统当前年月,格式 yyyy-MM
* @return
*/
public static String getYearMonth(){
String strYearMonth="";
Date currentDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM");
strYearMonth= formatter.format(currentDate);
return strYearMonth;
}
/**
* 得到系统当前年月日,格式 yyyy-MM-dd
* @return
*/
public static String getDate(){
String strCurrentDate="";
Date currentDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
strCurrentDate= formatter.format(currentDate);
return strCurrentDate;
}
/**
* 得到系统当前年月日,格式 HH:mm:ss
* @return
*/
public static String getTime(){
String strCurrentTime="";
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat ("HH:mm:ss");
strCurrentTime= formatter.format(currentTime);
return strCurrentTime;
}
/**
* 得到系统当前年月日,格式 HHmmss
* @return
*/
public static String getNumberTime(){
String strCurrentTime="";
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat ("HHmmss");
strCurrentTime= formatter.format(currentTime);
return strCurrentTime;
}
/**
* 时间格式转换 格式 HHmmss 转为:HH:mm:ss
* @return
*/
public static String numberToStringTime(String time){
String strTime="";
if(time.length()==6){
String hh = time.substring(0,2);
String mm = time.substring(2,4);
String ss = time.substring(4,6);
strTime=hh+":"+mm+":"+ss;
}
return strTime;
}
/**
* 得到系统当前年月日小时分秒,格式 yyyy-MM-dd HH:mm:ss
* @return
*/
public static String getDateTime(){
String strCurrentDateTime="";
Date currentDateTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
strCurrentDateTime= formatter.format(currentDateTime);
return strCurrentDateTime;
}
/**
* 得到系统当前年月日,格式 yyyyMMdd
* @return
*/
public static String getYMD(){
String strYMD="";
Date currentDateTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMdd");
strYMD= formatter.format(currentDateTime);
return strYMD;
}
/**
* 得到系统当前年月日小时分秒,格式 yyyyMMddHHmmss
* @return
*/
public static String getYMDHMS(){
String strYMDHMS="";
Date currentDateTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmss");
strYMDHMS= formatter.format(currentDateTime);
return strYMDHMS;
}
/**
* 得到系统当前年月日小时分秒毫秒,格式 yyyyMMddHHmmssSSS
* @return
*/
public static String getYMDHMSS(){
String strYMDHMSS="";
Date currentDateTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmssSSS");
strYMDHMSS= formatter.format(currentDateTime);
return strYMDHMSS;
}
/**
* 日期格式转换从yyyy-MM-dd HH:mm:ss到 yyyyMMddHHmmss
* @param strDateTime
* @return
*/
public static String lengthConvesion(String strDateTime){
String strYMDHMS="";
if(strDateTime==null){
return "" ;
}
if(strDateTime.length()==10) {
strYMDHMS = " 23:59:59";
}
return strDateTime+strYMDHMS;
}
/**
* 日期格式转换从yyyy-MM-dd HH:mm:ss到 yyyyMMddHHmmss
* @param strDateTime
* @return
*/
public static String stringToNumber(String strDateTime){
String strYMDHMS="";
if(strDateTime==null){
return "" ;
}
strDateTime = strDateTime.trim();
if(strDateTime.length()==10) {
strYMDHMS=strDateTime.substring(0,4)+strDateTime.substring(5,7)+strDateTime.substring(8,10);
strYMDHMS+="000000";
return strYMDHMS ;
}
if(strDateTime.length()==18) {
int i = strDateTime.indexOf(" ");
int ii = strDateTime.indexOf(":");
String tempStr = strDateTime.substring(i+1,ii);
tempStr = "0"+tempStr;
strYMDHMS=strDateTime.substring(0,4)+strDateTime.substring(5,7)+strDateTime.substring(8,10)
+tempStr+strDateTime.substring(13,15)+strDateTime.substring(16,18);
return strYMDHMS ;
}
if(strDateTime.length()==19){
int i = strDateTime.indexOf(" ");
int j = strDateTime.lastIndexOf(" ");
int l = strDateTime.length();
String temp = strDateTime ;
if(i!=j){
temp = strDateTime.substring(0,j)+"0"+strDateTime.substring(j+1,l);
}
strYMDHMS=temp.substring(0,4)+temp.substring(5,7)+temp.substring(8,10)
+temp.substring(11,13)+temp.substring(14,16)+temp.substring(17,19);
return strYMDHMS ;
}
if(strDateTime.length()>19){
//去毫秒2008-06-15 23:21:04.157
strDateTime = strDateTime.substring(0,19);
int i = strDateTime.indexOf(" ");
int j = strDateTime.lastIndexOf(" ");
int l = strDateTime.length();
String temp = strDateTime ;
if(i!=j){
temp = strDateTime.substring(0,j)+"0"+strDateTime.substring(j+1,l);
}
strYMDHMS=temp.substring(0,4)+temp.substring(5,7)+temp.substring(8,10)
+temp.substring(11,13)+temp.substring(14,16)+temp.substring(17,19);
return strYMDHMS ;
}
return "";
}
/**
* 日期格式转换从yyyyMMddHHmmss到yyyy-MM-dd HH:mm:ss
* @param strYMDHMS
* @return
*/
public static String numberToString(String strYMDHMS){
String strDateTime="";
if(strYMDHMS==null||"".equals(strYMDHMS)){
return "" ;
}
strYMDHMS = strYMDHMS.trim();
if(strYMDHMS.length()==8){
strDateTime=strYMDHMS.substring(0,4)+"-"+strYMDHMS.substring(4,6)+"-"+strYMDHMS.substring(6,8);
return strDateTime;
}
if(strYMDHMS.length()==14){
if( strYMDHMS.endsWith("000000") ){
strDateTime=strYMDHMS.substring(0,4)+"-"+strYMDHMS.substring(4,6)+"-"+strYMDHMS.substring(6,8);
}else{
strDateTime=strYMDHMS.substring(0,4)+"-"+strYMDHMS.substring(4,6)+"-"+strYMDHMS.substring(6,8)
+" "+strYMDHMS.substring(8,10)+":"+strYMDHMS.substring(10,12)+":"+strYMDHMS.substring(12,14);
}
return strDateTime;
}
return strYMDHMS;
}
/**
* 日期yyyy-mm-dd转换yyyymmdd000000
* @param strYMDHMS
* @return
*/
public static String startDatetoString(String strYMD){
if(strYMD==null||"".equals(strYMD)){
return "";
}
String strYMDHMS = "" ;
if(strYMD.length()==10){
strYMDHMS=strYMD.substring(0,4)+strYMD.substring(5,7)+strYMD.substring(8,10);
strYMDHMS+="000000";
}
if(strYMD.length()==19){
return stringToNumber(strYMD);
}
return strYMDHMS;
}
/**
* 日期yyyy-mm-dd转换yyyymmdd235959
* @param strYMDHMS
* @return
*/
public static String endDatetoString(String strYMD){
if(strYMD==null||"".equals(strYMD)){
return "";
}
String strYMDHMS = "" ;
if(strYMD.length()==10){
strYMDHMS=strYMD.substring(0,4)+strYMD.substring(5,7)+strYMD.substring(8,10);
strYMDHMS+="235959";
}
if(strYMD.length()==19){
return stringToNumber(strYMD);
}
return strYMDHMS;
}
/**
* 得到系统当前日期是星期几,格式 "星期一"
* @return
*/
public static String getWeek(){
String strCurrentWeek="";
Date currentWeek = new Date();
SimpleDateFormat formatter = new SimpleDateFormat ("E");
strCurrentWeek= formatter.format(currentWeek);
return strCurrentWeek;
}
/**
* 得到任意输入的一个日期的星期数,格式 "星期一"
* @param strDate
* @return
*/
public static String getDateToWeek(String strDate){
String strWeek="";
int iYear = Integer.parseInt(strDate.substring(0, 4));
int iMonth = Integer.parseInt(strDate.substring(5, 7));
int iDay = Integer.parseInt(strDate.substring(8, 10));
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, iYear);
cal.set(Calendar.MONTH, iMonth-1);
cal.set(Calendar.DAY_OF_MONTH, iDay);
Date currentDate = cal.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("E");
strWeek= formatter.format(currentDate);
return strWeek;
}
}
=======================================================================
时间特殊处理实用类
/**
* File: DateUtilEx.java
* All right reserved.
* Author qiuren
* Date 2010/12/24
*
*/
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
/**
*
* 各种日期,时间特殊处理实用类
*
*/
public class DateUtilEx extends DateUtil{
/**
* 得到系统昨天年月日,格式 yyyyMMdd
* @return
*/
public String getLastYMD(){
String strYYMD="";
String strYesterdayDateTime=getDateChange(getDate(),-1);
String strYear = strYesterdayDateTime.substring(0, 4);
String strMonth = strYesterdayDateTime.substring(5, 7);
String strDay = strYesterdayDateTime.substring(8, 10);
strYYMD= strYear+strMonth+strDay;
return strYYMD;
}
/**
* 得到系统昨天年月日,格式 yyyy-MM-dd
* @return
*/
public String getLastDate(){
String strYDate="";
strYDate=getDateChange(getDate(),-1);
return strYDate;
}
/**
* 得到系统上个月的年月数据,格式 yyyyMM
* @return
*/
public String getLastYM(){
String strYYM="";
String strYearMonth = getYearMonth();
strYYM = getMonthChange(strYearMonth,-1);
strYYM = strYYM.substring(0, 4)+strYYM.substring(5, 7);
return strYYM;
}
/**
* 得到系统上个月的年月数据,格式 yyyy-MM
* @return
*/
public String getLastYearMonth(){
String strYYearMonth="";
String strYearMonth = getYearMonth();
strYYearMonth = getMonthChange(strYearMonth,-1);
return strYYearMonth;
}
/**
* 得到系统当前日期是一年中的第几个星期
* @return
*/
public String getWeekInYear(){
String strCurrentWeekInYear="";
Date currentDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat ("w");
strCurrentWeekInYear= formatter.format(currentDate);
return strCurrentWeekInYear;
}
/**
* 得到当前星期日的日期
* @return
*/
public Calendar getWeekStar(){
Calendar begin=Calendar.getInstance();
int iCurrentWeek = begin.get(Calendar.DAY_OF_WEEK);
switch (iCurrentWeek)
{
case 1:
return begin;
case 2:
begin.add(Calendar.DATE, -1);
return begin;
case 3:
begin.add(Calendar.DATE, -2);
return begin;
case 4:
begin.add(Calendar.DATE, -3);
return begin;
case 5:
begin.add(Calendar.DATE, -4);
return begin;
case 6:
begin.add(Calendar.DATE, -5);
return begin;
case 7:
begin.add(Calendar.DATE, -6);
return begin;
default:
return begin;
}
}
/**
* 得到当前星期六的日期
* @return
*/
public Calendar getWeekEnd(){
Calendar end=Calendar.getInstance();
int iCurrentWeek = end.get(Calendar.DAY_OF_WEEK);
switch (iCurrentWeek) {
case 1:
end.add(Calendar.DATE, 6);
return end;
case 2:
end.add(Calendar.DATE, 5);
return end;
case 3:
end.add(Calendar.DATE, 4);
return end;
case 4:
end.add(Calendar.DATE, 3);
return end;
case 5:
end.add(Calendar.DATE, 2);
return end;
case 6:
end.add(Calendar.DATE, 1);
return end;
case 7:
return end;
default:
return end;
}
}
/**
* 得到系统当前星期周日到今天的日期数据
* @return
*/
public LinkedList getSundayToToday(){
String strBeginDate="";
String strEndDate="";
Calendar begin=getWeekStar();
Date beginDate = begin.getTime();
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
strBeginDate= formatter.format(beginDate);
Date currentDate = new Date();
strEndDate= formatter.format(currentDate);
LinkedList datelist= dateToDate(strBeginDate,strEndDate);
return datelist;
}
/**
* 得到当前星期的日期区间,格式 "2003-11-23 to 2003-11-29"
* @return
*/
public String getWeekArea(){
String strTargetData="";
String strBeginDate="";
String strEndDate="";
Calendar begin=getWeekStar();
Calendar end=getWeekEnd();
Date beginDate = begin.getTime();
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
strBeginDate= formatter.format(beginDate);
Date endDate = end.getTime();
strEndDate= formatter.format(endDate);
strTargetData=strBeginDate+" to "+strEndDate;
return strTargetData;
}
/**
* 得到一年中的某个星期的日期区间,格式 "2003-11-23 to 2003-11-29"
* @param iYear - 年
* @param strWeekInYear - 第几个星期
* @return
*/
public String getWeekArea(int iYear,String strWeekInYear){
String strTargetData="";
HashMap hashm = getAllWeekArea(iYear);
strTargetData=(String)hashm.get(strWeekInYear);
return strTargetData;
}
/**
* 得到一年中从开始到当前以星期为间隔的日期区间,格式 1 "2000-01-01 to 2003-01-04"
* @param iYear
* @return
*/
public HashMap getAllWeekArea(int iYear){
HashMap hashm = new HashMap();
String strTargetData="";
String strBeginDate="";
String strEndDate="";
String strWeekNum="1";
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, iYear);
cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 1);
Date currentDate = cal.getTime();
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
strBeginDate= formatter.format(currentDate);
int iOne = cal.get(Calendar.DAY_OF_WEEK);
switch (iOne) {
case 1:
cal.add(Calendar.DATE, 6);
break;
case 2:
cal.add(Calendar.DATE, 5);
break;
case 3:
cal.add(Calendar.DATE, 4);
break;
case 4:
cal.add(Calendar.DATE, 3);
break;
case 5:
cal.add(Calendar.DATE, 2);
break;
case 6:
cal.add(Calendar.DATE, 1);
break;
}
currentDate = cal.getTime();
strEndDate= formatter.format(currentDate);
strTargetData=strBeginDate+" to "+strEndDate;
hashm.put(strWeekNum, strTargetData);
for(int i=2;i<=52;i++){
cal.add(Calendar.DATE, 1);
currentDate = cal.getTime();
strBeginDate= formatter.format(currentDate);
cal.add(Calendar.DATE, 6);
currentDate = cal.getTime();
strEndDate= formatter.format(currentDate);
strTargetData=strBeginDate+" to "+strEndDate;
strWeekNum=String.valueOf(i);
hashm.put(strWeekNum, strTargetData);
}
return hashm;
}
/**
* 返回两段日期之间的所有日期数据
* 用法举例:
* DateTime dateTime=new DateTime();
* LinkedList datelist= dateTime.dateToDate("2001-12-12","2002-03-20");
* while(!datelist.isEmpty())
* {
*
* System.out.println(datelist.removeLast());
* }
* 或
* while(!datelist.isEmpty())
* {
*
* System.out.println(datelist.removeFirst());
* }
*/
public LinkedList dateToDate(String strBeginDate,String strEndDate){
if(strEndDate.compareTo(strBeginDate)<0){
return null;
}
LinkedList datelist = new LinkedList();
int iBeginYear =Integer.parseInt(strBeginDate.substring(0,4));
int iBeginMonth =Integer.parseInt(strBeginDate.substring(5,7));
int iBeginDay =Integer.parseInt(strBeginDate.substring(8,10));
int iEndYear =Integer.parseInt(strEndDate.substring(0,4));
int iEndMonth =Integer.parseInt(strEndDate.substring(5,7));
int iEndDay =Integer.parseInt(strEndDate.substring(8,10));
String strYear ="";
String strMonth ="";
String strDay ="";
String strDate ="";
int iMonthBeginNumber=0;
int iMonthEndNumber=0;
int iDayBeginNumber=0;
//int iDayEndNumber=0;
for(int i=iBeginYear;i<=iEndYear;i++){
if(i==iBeginYear){
iMonthBeginNumber=iBeginMonth;
}
if(i>iBeginYear){
iMonthBeginNumber=1;
}
if(i<iEndYear){
iMonthEndNumber=12;
}
if(i==iEndYear){
iMonthEndNumber=iEndMonth;
}
//统一年份内日期数据的输出开始
if(iBeginYear==iEndYear){
strYear = String.valueOf(i);
for(int j=iMonthBeginNumber;j<=iMonthEndNumber;j++){
if(j==iMonthBeginNumber){
iDayBeginNumber=iBeginDay;
}
if(j>iMonthBeginNumber){
iDayBeginNumber=1;
}
//统一年份内尾月以前的日期数据的输出开始
if(j<iMonthEndNumber){
switch(j){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
for(int k=iDayBeginNumber;k<=31;k++){
if(j<10){
strMonth="0"+ j ;
}
else{
strMonth=String.valueOf(j);
}
if(k<10){
strDay="0"+ k ;
}
else{
strDay=String.valueOf(k);
}
strDate=strYear+"-"+strMonth+"-"+strDay;
datelist.add(strDate);
}
break;
case 2:
int iFebNumber=(i%4==0 && i%100 != 0)||(i%400 ==0)?29:28;
for(int k=iDayBeginNumber;k<=iFebNumber;k++){
if(j<10){
strMonth="0"+ j ;
}
else{
strMonth=String.valueOf(j);
}
if(k<10){
strDay="0"+ k ;
}
else{
strDay=String.valueOf(k);
}
strDate=strYear+"-"+strMonth+"-"+strDay;
datelist.add(strDate);
}
break;
case 4:
case 6:
case 9:
case 11:
for(int k=iDayBeginNumber;k<=30;k++){
if(j<10){
strMonth="0"+ j ;
}
else{
strMonth=String.valueOf(j);
}
if(k<10){
strDay="0"+ k ;
}
else{
strDay=String.valueOf(k);
}
strDate=strYear+"-"+strMonth+"-"+strDay;
datelist.add(strDate);
}
break;
}//switch end
}
//统一年份内尾月以前的日期数据的输出结束
//统一年份内尾月日期数据的输出开始
if(j==iMonthEndNumber){
for(int k=iDayBeginNumber;k<=iEndDay;k++){
if(j<10){
strMonth="0"+ j ;
}
else{
strMonth=String.valueOf(j);
}
if(k<10){
strDay="0"+ k ;
}
else{
strDay=String.valueOf(k);
}
strDate=strYear+"-"+strMonth+"-"+strDay;
datelist.add(strDate);
}
}
//统一年份内尾月日期数据的输出结束
}//for end
//统一年份内日期数据的输出结束
}
else{
//不同年份内日期数据的输出开始
//最后年份以前的日期数据的输出开始
if(i<iEndYear){
strYear = String.valueOf(i);
for(int j=iMonthBeginNumber;j<=iMonthEndNumber;j++){
if(j==iMonthBeginNumber){
iDayBeginNumber=iBeginDay;
}
if(j>iMonthBeginNumber){
iDayBeginNumber=1;
}
switch(j){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
for(int k=iDayBeginNumber;k<=31;k++){
if(j<10){
strMonth="0"+ j ;
}
else {
strMonth=String.valueOf(j);
}
if(k<10){
strDay="0"+ k ;
}
else{
strDay=String.valueOf(k);
}
strDate=strYear+"-"+strMonth+"-"+strDay;
datelist.add(strDate);
}
break;
case 2:
int iFebNumber=(i%4==0 && i%100 != 0)||(i%400 ==0)?29:28;
for(int k=iDayBeginNumber;k<=iFebNumber;k++) {
if(j<10) {
strMonth="0"+ j ;
}
else{
strMonth=String.valueOf(j);
}
if(k<10){
strDay="0"+ k ;
}
else{
strDay=String.valueOf(k);
}
strDate=strYear+"-"+strMonth+"-"+strDay;
datelist.add(strDate);
}
break;
case 4:
case 6:
case 9:
case 11:
for(int k=iDayBeginNumber;k<=30;k++){
if(j<10){
strMonth="0"+ j ;
}
else{
strMonth=String.valueOf(j);
}
if(k<10){
strDay="0"+ k ;
}
else{
strDay=String.valueOf(k);
}
strDate=strYear+"-"+strMonth+"-"+strDay;
datelist.add(strDate);
}
break;
}//switch end
}//for end
}//if(i<iEndYear) end
if(i==iEndYear){
strYear = String.valueOf(i);
for(int j=1;j<=iEndMonth;j++){
if(j<iEndMonth){
switch(j){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
for(int k=1;k<=31;k++){
if(j<10){
strMonth="0"+ j ;
}
else{
strMonth=String.valueOf(j);
}
if(k<10){
strDay="0"+ k ;
}
else{
strDay=String.valueOf(k);
}
strDate=strYear+"-"+strMonth+"-"+strDay;
datelist.add(strDate);
}
break;
case 2:
int iFebNumber=(i%4==0 && i%100 != 0)||(i%400 ==0)?29:28;
for(int k=1;k<=iFebNumber;k++){
if(j<10){
strMonth="0"+ j ;
}
else{
strMonth=String.valueOf(j);
}
if(k<10){
strDay="0"+ k ;
}
else{
strDay=String.valueOf(k);
}
strDate=strYear+"-"+strMonth+"-"+strDay;
datelist.add(strDate);
}
break;
case 4:
case 6:
case 9:
case 11:
for(int k=1;k<=30;k++){
if(j<10){
strMonth="0"+ j ;
}
else{
strMonth=String.valueOf(j);
}
if(k<10){
strDay="0"+ k ;
}
else{
strDay=String.valueOf(k);
}
strDate=strYear+"-"+strMonth+"-"+strDay;
datelist.add(strDate);
}
break;
}//switch end
}
if(j==iEndMonth){
for(int k=1;k<=iEndDay;k++){
if(j<10){
strMonth="0"+ j ;
}
else{
strMonth=String.valueOf(j);
}
if(k<10){
strDay="0"+ k ;
}
else{
strDay=String.valueOf(k);
}
strDate=strYear+"-"+strMonth+"-"+strDay;
datelist.add(strDate);
}
}
}//for end
}//if end
//不同年份内日期数据的输出结束
}
}//for(int i=iBeginYear;i<=iEndYear;i++) end
return datelist;
}//dateToDate end
/**
* 得到对比日期变化的目标日期
* getDateChange("2003-10-15",1)="2003-10-16";
* getDateChange("2003-10-15",-2)="2003-10-13";
* @param strCurrentDate
* @param iQuantity
* @return
*/
public String getDateChange(String strCurrentDate,int iQuantity){
String strTarget="";
int iYear = Integer.parseInt(strCurrentDate.substring(0, 4));
int iMonth = Integer.parseInt(strCurrentDate.substring(5, 7));
int iDay = Integer.parseInt(strCurrentDate.substring(8, 10));
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, iYear);
cal.set(Calendar.MONTH, iMonth-1);
cal.set(Calendar.DAY_OF_MONTH, iDay);
cal.add(Calendar.DATE, iQuantity);
Date currentDate = cal.getTime();
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
strTarget= formatter.format(currentDate);
return strTarget;
}
/**
* 得到对比日期变化的目标工作日,考虑双休日和节假日
* getWorkingDayChange("2003-10-15 15:23:12",1)="2003-10-16 15:23:12"
* getWorkingDayChange("2003-10-15 15:23:12",-2)="2003-10-13 15:23:12";
* @param strCurrentTime
* @param iQuantity
* @return
*/
public String getWorkingDayChange(String strCurrentTime,int iQuantity){
String strTarget="";
int iYear = Integer.parseInt(strCurrentTime.substring(0, 4));
int iMonth = Integer.parseInt(strCurrentTime.substring(5, 7));
int iDay = Integer.parseInt(strCurrentTime.substring(8, 10));
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, iYear);
cal.set(Calendar.MONTH, iMonth-1);
cal.set(Calendar.DAY_OF_MONTH, iDay);
if(iQuantity > 0){
for(int i=0;i<iQuantity;i++){
//取后一天
cal.add(Calendar.DATE, 1);
//取到的后一天日期为节假日则跳过
if(holidayList(formatter.format(cal.getTime()))){
while(holidayList(formatter.format(cal.getTime()))){
cal.add(Calendar.DATE, 1);
}
}else{
//判断是否周末,星期六再加2天,星期天再加1天,跳到下星期一
switch (cal.get(Calendar.DAY_OF_WEEK)) {
case Calendar.SATURDAY:
//cal.add(Calendar.DATE, 2);
//判断这个周六是否周末调为工作日,是则停留,否则加一跳过
if(!noholidayList(formatter.format(cal.getTime()))){
cal.add(Calendar.DATE, 1);
}
//接着判断
if(!noholidayList(formatter.format(cal.getTime()))){
cal.add(Calendar.DATE, 1);
}
break;
case Calendar.SUNDAY:
//cal.add(Calendar.DATE, 1);
//判断这个周日是否周末调为工作日,是则停留,否则加一跳过
if(!noholidayList(formatter.format(cal.getTime()))){
cal.add(Calendar.DATE, 1);
}
break;
}
}
}
}else if(iQuantity < 0){
for(int i=0;i<-iQuantity;i++){
//取前一天
cal.add(Calendar.DATE, -1);
//取到的后一天日期为节假日则直接减1天跳过
if(holidayList(formatter.format(cal.getTime()))){
cal.add(Calendar.DATE, -1);
}else{
//判断是否周末,星期天再减2天,星期六再减1天,跳到上星期五
switch (cal.get(Calendar.DAY_OF_WEEK)) {
case Calendar.SUNDAY:
//cal.add(Calendar.DATE, -2);
//判断这个周日是否周末调为工作日,是则停留,否则减一跳过
if(!noholidayList(formatter.format(cal.getTime()))){
cal.add(Calendar.DATE, -1);
}
//接着判断
if(!noholidayList(formatter.format(cal.getTime()))){
cal.add(Calendar.DATE, -1);
}
break;
case Calendar.SATURDAY:
//cal.add(Calendar.DATE, -1);
//判断这个周六是否周末调为工作日,是则停留,否则减一跳过
if(!noholidayList(formatter.format(cal.getTime()))){
cal.add(Calendar.DATE, -1);
}
break;
}
}
}
}
Date currentDate = cal.getTime();
strTarget= formatter.format(currentDate);
if(strCurrentTime.length() > 10){
strTarget=strTarget+strCurrentTime.substring(10, 19);
}
return strTarget;
}
/**
* 得到对比日期变化的目标日期
* getDateTimeChange("2003-10-15 15:23:12",1)="2003-10-16 15:23:12"
* getDateTimeChange("2003-10-15 15:23:12",-2)="2003-10-13 15:23:12";
* @param strCurrentTime
* @param iQuantity
* @return
*/
public String getDateTimeChange(String strCurrentTime,int iQuantity){
String strTarget="";
String strHHMMSS=strCurrentTime.substring(10, 19);
int iYear = Integer.parseInt(strCurrentTime.substring(0, 4));
int iMonth = Integer.parseInt(strCurrentTime.substring(5, 7));
int iDay = Integer.parseInt(strCurrentTime.substring(8, 10));
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, iYear);
cal.set(Calendar.MONTH, iMonth-1);
cal.set(Calendar.DAY_OF_MONTH, iDay);
cal.add(Calendar.DATE, iQuantity);
Date currentDate = cal.getTime();
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
strTarget= formatter.format(currentDate);
strTarget=strTarget+strHHMMSS;
return strTarget;
}
/**
* 得到对比月变化的目标年月
* getMonthChange("2003-10",1)="2003-11";
* getMonthChange("2003-10",-2)="2003-08";
* @param strCurrentTime
* @param iQuantity
* @return
*/
public String getMonthChange(String strCurrentTime,int iQuantity){
String strTarget="";
int iYear = Integer.parseInt(strCurrentTime.substring(0, 4));
int iMonth = Integer.parseInt(strCurrentTime.substring(5, 7));
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, iYear);
cal.set(Calendar.MONTH, iMonth-1);
cal.add(Calendar.MONTH, iQuantity);
Date currentDate = cal.getTime();
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM");
strTarget= formatter.format(currentDate);
return strTarget;
}
/**
* 得到某一月最后一天的年月日数据
* getMonthEndDay("2003-10")="2003-10-31"
* @param strYearMonth
* @return
*/
public String getMonthEndDay(String strYearMonth){
String strDay="31";
int iYear = Integer.parseInt(strYearMonth.substring(0, 4));
int iMonth = Integer.parseInt(strYearMonth.substring(5, 7));
switch(iMonth){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
strDay="31";
break;
case 2:
strDay=(iYear%4==0 && iYear%100 !=0)||(iYear%400 ==0)?"29":"28";
break;
case 4:
case 6:
case 9:
case 11:
strDay="30";
break;
}
String strMonthEndDay = strYearMonth +"-"+strDay;
return strMonthEndDay;
}
/**
* 返回两段年月之间的所有年月数据
* 用法举例:
* DateTime dateTime=new DateTime();
* LinkedList datelist= dateTime.monthToMonth("2001-12","2002-03");
* while(!datelist.isEmpty())
* {
*
* System.out.println(datelist.removeLast());
* }
* 或
* while(!datelist.isEmpty())
* {
*
* System.out.println(datelist.removeFirst());
* }
*/
public LinkedList monthToMonth(String strBeginMonth,String strEndMonth){
if(strEndMonth.compareTo(strBeginMonth)<0){
return null;
}
LinkedList datelist = new LinkedList();
int iBeginYear =Integer.parseInt(strBeginMonth.substring(0,4));
int iBeginMonth =Integer.parseInt(strBeginMonth.substring(5,7));
int iEndYear =Integer.parseInt(strEndMonth.substring(0,4));
int iEndMonth =Integer.parseInt(strEndMonth.substring(5,7));
String strYear ="";
String strMonth ="";
String strYearMonth ="";
int iMonthBeginNumber=0;
int iMonthEndNumber=0;
for(int i=iBeginYear;i<=iEndYear;i++){
if(i==iBeginYear){
iMonthBeginNumber=iBeginMonth;
}
if(i>iBeginYear){
iMonthBeginNumber=1;
}
if(i<iEndYear){
iMonthEndNumber=12;
}
if(i==iEndYear){
iMonthEndNumber=iEndMonth;
}
//统一年份内年月数据的输出开始
if(iBeginYear==iEndYear){
strYear = String.valueOf(i);
for(int j=iMonthBeginNumber;j<=iMonthEndNumber;j++){
if(j<10){
strMonth="0"+ j ;
}
else{
strMonth=String.valueOf(j);
}
strYearMonth=strYear+"-"+strMonth;
datelist.add(strYearMonth);
}
//统一年份内年月数据的输出结束
}
else
{
//不同年份内年月数据的输出开始
if(i<iEndYear){
strYear = String.valueOf(i);
for(int j=iMonthBeginNumber;j<=iMonthEndNumber;j++){
if(j<10){
strMonth="0"+ j ;
}
else{
strMonth=String.valueOf(j);
}
strYearMonth=strYear+"-"+strMonth;
datelist.add(strYearMonth);
}
}//if(i<iEndYear) end
if(i==iEndYear){
strYear = String.valueOf(i);
for(int j=1;j<=iEndMonth;j++){
if(j<10){
strMonth="0"+ j ;
}
else{
strMonth=String.valueOf(j);
}
strYearMonth=strYear+"-"+strMonth;
datelist.add(strYearMonth);
}
}
//不同年份内年月数据的输出结束
}//if end
}//for(int i=iBeginYear;i<=iEndYear;i++) end
return datelist;
}//dateToDate end
/**
* 两天日期相减得到时间差,根据类型不同得到的差值含义不一样!
* @param date1"2010-10-12 23:00:00"
* @param date2"2010-10-13 01:30:00"
* @param Type D:天 H:小时 M:分钟 S:秒
* @return
*/
public long getDateMinusDate(String date1,String date2,String Type){
try{
Date dt1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(date1);
Date dt2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(date2);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
long seconds = (dt1.getTime()- dt2.getTime())/1000;
long date = seconds/(24*60*60); //相差的天数
long hour = (seconds-date*24*60*60)/(60*60);//相差的小时数
long minut = (seconds-date*24*60*60-hour*60*60)/(60);//相差的分钟数
long second = (seconds-date*24*60*60-hour*60*60-minut*60);//相差的秒数
if(Type.equalsIgnoreCase("D")){
return date;
}else if(Type.equalsIgnoreCase("H")){
return hour;
}else if(Type.equalsIgnoreCase("M")){
return minut;
}else if(Type.equalsIgnoreCase("S")){
return second;
}
return 0;
}
catch(Exception e){
e.printStackTrace();
return 0;
}
}
//判断节假日
public boolean holidayList(String findDate){
List ls = new ArrayList();
ls = dateToDate("2008-09-29", "2008-10-05"); //2008年十一
if(ls.contains(findDate))
return true;
return false;
}
//判断周末调为工作日
public boolean noholidayList(String findDate){
//String[] noholiday = {"2008-05-04","2008-09-27","2008-09-28"};
List ls = new ArrayList();
ls.add("2008-05-04");
ls.add("2008-09-27");
ls.add("2008-09-28");
if(ls.contains(findDate))
return true;
return false;
}
/**
* 得到给定日期时间段内的秒数,出去周六、日
*
* @param startDateTime yyyy-MM-dd hh:mm:ss
* @param endDateTime yyyy-MM-dd hh:mm:ss
* @return
*/
public long getPrdSecDateToDate(String startDateTime, String endDateTime) {
try {
Date dt1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
.parse(startDateTime);
Date dt2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
.parse(endDateTime);
int flag = 1;
if(dt1.after(dt2)){
flag = -1;
Date dtTemp = dt1;
dt1 = dt2;
dt2 = dtTemp;
}
// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd
// hh:mm:ss");
long totalSec = (dt2.getTime() - dt1.getTime()) / 1000;
//System.out.println( "xxxxx---totalSec"+ totalSec );
LinkedList datelist = dateToDate(startDateTime, endDateTime);
String startDate = startDateTime.substring(0, 10);
String endDate = endDateTime.substring(0, 10);
String tempDate = "";
long tempSec=0;
int size = datelist==null?0:datelist.size();
for (int i = 0; i < size; i++) {
tempDate = datelist.removeFirst().toString();
//System.out.println( "tempDate"+ tempDate );
if (isWeekend(tempDate)) {
if( !tempDate.equals( startDate ) && !tempDate.equals( endDate ) )
totalSec -= 24*60*60;
else if( tempDate.equals( startDate ) && tempDate.equals( endDate ) )
totalSec = 0;
else if( tempDate.equals( startDate ) && !tempDate.equals( endDate ) )
{
Date tempdt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
.parse(startDate+" 23:59:59");
tempSec = (tempdt.getTime() - dt1.getTime()+1) / 1000;
//System.out.println( "yyyy---tempSec="+ tempSec );
totalSec -= tempSec;
}
else if( !tempDate.equals( startDate ) && tempDate.equals( endDate ) )
{
Date tempdt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
.parse(endDate+" 00:00:00");
tempSec = ( dt2.getTime() - tempdt.getTime() ) / 1000;
//System.out.println( "zzzz---tempSec="+ tempSec );
totalSec -= tempSec;
}
}
//System.out.println( i+" ---totalSec"+ totalSec );
}
//System.out.println( "totalSec"+ totalSec );
return totalSec*flag;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 判断给定日期是否是周六、日
*
* @param startDateTime
* @param endDateTime
* @return
*/
public boolean isWeekend(String sDateTime){
try{
int iYear = Integer.parseInt(sDateTime.substring(0, 4));
int iMonth = Integer.parseInt(sDateTime.substring(5, 7));
int iDay = Integer.parseInt(sDateTime.substring(8, 10));
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, iYear);
cal.set(Calendar.MONTH, iMonth-1);
cal.set(Calendar.DAY_OF_MONTH, iDay);
if( cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY)
return true;
return false;
}
catch(Exception e){
e.printStackTrace();
return false;
}
}
/**
* 测试
* @param args
*/
public static void main(String[] args) {
try{
DateUtilEx de = new DateUtilEx();
System.out.println(de.getDateStr() );
//de.getPrdSecDateToDate( "20091107103030".toString(), "20091109103030".toString() );
de.getPrdSecDateToDate( "2009-11-07 10:30:30".toString(), "2009-11-09 10:30:30".toString() );
//de.getPrdSecDateToDate( "2009-11-06 10:30:30".toString(), "2009-11-09 11:30:30".toString() );
//de.getPrdSecDateToDate( "2009-11-06".toString(), "2009-11-09".toString() );
System.out.println(de.getDateStr() );
System.out.println( new DateEntity().getDayOfWeek() );
}catch(Exception e){}
}
/**
* 两天日期相减得到时间差
* @param date1"2010-10-12"
* @param date2"2010-10-13"
* @return
*/
public static long getDateBalance(String date1,String date2){
try{
Date dt1 = new SimpleDateFormat("yyyy-MM-dd").parse(date1);
Date dt2 = new SimpleDateFormat("yyyy-MM-dd").parse(date2);
long seconds = (dt2.getTime()-dt1.getTime())/1000;
long date = seconds/(24*60*60); //相差的天数
return date;
}
catch(Exception e){
e.printStackTrace();
return 0;
}
}
/**
* 比较两个时间大小
* @param date1"2010-10-12 23:00:00"
* @param date2"2010-10-13 01:30:00"
* @return
*/
public static boolean getDateMinus(String date1,String date2){
try{
if(date1.length()==10){
date1=date1+" 00:00:00";
}
if(date2.length()==10){
date2=date2+" 00:00:00";
}
Date dt1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date1);
Date dt2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date2);
long seconds = (dt1.getTime()- dt2.getTime())/1000;
if(seconds>0){
return true;
}else{
return false;
}
}
catch(Exception e){
e.printStackTrace();
return false;
}
}
}