/**
* 计算两个日期之间相差的天数
* @param smdate 较小的时间
* @param bdate 较大的时间
* @return 相差天数
* @throws ParseException
*/
publicstaticintDaysMinus(Date startDate,Date endDate)throwsParseException
{
SimpleDateFormat sdf=newSimpleDateFormat("yyyy-MM-dd");
startDate = sdf.parse(sdf.format(startDate));
endDate = sdf.parse(sdf.format(endDate));
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
longstartTime = cal.getTimeInMillis();
cal.setTime(endDate);
longendTime = cal.getTimeInMillis();
longbetween_days=(endTime-startTime)/(1000*3600*24);
returnInteger.parseInt(String.valueOf(between_days));
}
/**
*字符串的日期格式的计算相差天数
*@param 开始日期:startDate
*@param 结束日期:endDate
*@return 相差天数
*/
publicstaticintDaysMinus(String startDate,String endDate)throwsParseException{
SimpleDateFormat sdf=newSimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(startDate));
longstartTime = cal.getTimeInMillis();
cal.setTime(sdf.parse(endDate));
longendTime = cal.getTimeInMillis();
longbetween_days = (endTime-startTime)/(1000*3600*24);
returnInteger.parseInt(String.valueOf(between_days));
}
/**
* String类型日期转Date
* @param date
* @return
* @throws ParseException
*/
publicstaticDate string2Date(String date)throwsParseException{
SimpleDateFormat sdf = newSimpleDateFormat("yyyy-MM-dd");
Date resultDate = sdf.parse(date);
returnresultDate;
}
/**
* Date类型日期转String
* @param date
* @return
* @throws ParseException
*/
publicstaticString date2String(Date date)throwsParseException{
SimpleDateFormat sdf = newSimpleDateFormat("yyyy-MM-dd");
String currentTime = sdf.format(date);
returncurrentTime;
}
/**
* 日期相加
* @param date
* @param days
* @return
*/
publicstaticDate dateAdd(Date date,intdays) {
Calendar calendar = newGregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.DATE, days);
date = calendar.getTime();
returndate;
}
/**
* String 类型日期相加
* @param date
* @param days
* @return
* @throws ParseException
*/
publicstaticDate dateAdd(String date,intdays)throwsParseException {
Date Datedate = string2Date(date);
Calendar calendar = newGregorianCalendar();
calendar.setTime(Datedate);
calendar.add(calendar.DATE, days);
Datedate = calendar.getTime();
returnDatedate;
}
/**
* 获取当前日期的周次
* 07:"星期日", 01:"星期一", 02:"星期二", 03:"星期三", 04:"星期四", 05:"星期五", 06:"星期六"
* @author 秦海
* @param dt
* @return
*/
publicstaticString DayOfWeek2MatchDict(Date dt) {
String[] weekDays = {"07","01","02","03","04","05","06"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
intdayOfWeek = cal.get(Calendar.DAY_OF_WEEK) -1;
if(dayOfWeek <0){
dayOfWeek = 0;
}
returnweekDays[dayOfWeek];
}
/**
* 获取系统当前日期 date类型
* @return
* @throws ParseException
*/
publicstaticDate getDateSysDate()throwsParseException {
Date now = newDate();
SimpleDateFormat sdf = newSimpleDateFormat("yyyy-MM-dd");
String sysDate = sdf.format(now);
Date resultDate = sdf.parse(sysDate);
returnresultDate;
}
/**
* 按照传入的格式获取系统date类型世间
* -年-月-日 时:分:秒.毫秒
* {yyyy-MM-dd HH:mm:ss.fff}:使用24小时制格式化日期
* {yyyy-MM-dd hh:mm:ss.fff}:使用12小时制格式化日期
* {yyyy-MM-dd HH:mm:ss zzz}
* {yyyy-MM-dd HH:mm:ss.ff zzz}
* {yyyy-MM-dd HH:mm:ss.fff zzz}
* {yyyy-MM-dd HH:mm:ss.ffff zzz}
* @param format
* @return
* @throws ParseException
*/
publicstaticDate getDateSysDate(String format)throwsParseException {
Date now = newDate();
SimpleDateFormat sdf = newSimpleDateFormat(format);
String sysDate = sdf.format(now);
Date resultDate = sdf.parse(sysDate);
returnresultDate;
}
}
/**
* 根据出生日期获取人的年龄
*
* @param strBirthDate(yyyy-mm-dd or yyyy/mm/dd)
* @return
*/
publicstaticString getPersonAgeByBirthDate(Date dateBirthDate){
if(dateBirthDate ==null){
return"";
}
SimpleDateFormat dateFormat = newSimpleDateFormat("yyyy-MM-dd");
String strBirthDate=dateFormat.format(dateBirthDate);
//读取当前日期
Calendar c = Calendar.getInstance();
intyear = c.get(Calendar.YEAR);
intmonth = c.get(Calendar.MONTH)+1;
intday = c.get(Calendar.DATE);
//计算年龄
intage = year - Integer.parseInt(strBirthDate.substring(0,4)) -1;
if(Integer.parseInt(strBirthDate.substring(5,7))
age++;
} elseif(Integer.parseInt(strBirthDate.substring(5,7))== month && Integer.parseInt(strBirthDate.substring(8,10)) <= day){
age++;
}
returnString.valueOf(age);
}
/**
* 根据出生日期获取人的年龄
*
* @param strBirthDate(yyyy-mm-dd or yyyy/mm/dd)
* @return
*/
publicstaticString getPersonAgeByBirthDate(String strBirthDate){
if("".equals(strBirthDate) || strBirthDate ==null){
return"";
}
//读取当前日期
Calendar c = Calendar.getInstance();
intyear = c.get(Calendar.YEAR);
intmonth = c.get(Calendar.MONTH)+1;
intday = c.get(Calendar.DATE);
//计算年龄
intage = year - Integer.parseInt(strBirthDate.substring(0,4)) -1;
if(Integer.parseInt(strBirthDate.substring(5,7))
age++;
} elseif(Integer.parseInt(strBirthDate.substring(5,7))== month && Integer.parseInt(strBirthDate.substring(8,10)) <= day){
age++;
}
returnString.valueOf(age);
}
/**
* 获取当前日期是星期几
* 0-"星期日", 1-"星期一", 2-"星期二", 3-"星期三", 4-"星期四", 5-"星期五", 6-"星期六"
* @param dt
* @return
*/
publicstaticintgetDayOfWeek(Date dt) {
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
intdayOfWeek = cal.get(Calendar.DAY_OF_WEEK) -1;
returndayOfWeek;
}
/**
* 获取当前日期是星期几
* 0-"星期日", 1-"星期一", 2-"星期二", 3-"星期三", 4-"星期四", 5-"星期五", 6-"星期六"
* @param: strDate
*
*/
publicstaticintgetDayOfWeek(String strDate){
String format="yyyy-MM-dd";
//可以方便地修改日期格式
SimpleDateFormat dateFormat = newSimpleDateFormat(format);
Date date = null;
try{
date = dateFormat.parse(strDate);
Calendar c = Calendar.getInstance();
c.setTime(date);
intdayOfWeek=c.get(Calendar.DAY_OF_WEEK)-1;;
returndayOfWeek;
} catch(ParseException e) {
e.printStackTrace();
}
return-1;
}
/**
* 获取系统当前时间
*/
publicstaticString getSysTime(){
Date now = newDate();
//可以方便地修改日期格式
SimpleDateFormat dateFormat = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sysDate = dateFormat.format(now);
returnsysDate;
}
/**
* 获取系统当前时间
*/
publicstaticString getSysDate(){
Date now = newDate();
SimpleDateFormat dateFormat = newSimpleDateFormat("yyyy-MM-dd");
String sysDate = dateFormat.format(now);
returnsysDate;
}
/**
* 按格式获取系统当前时间
*
* @param: format
*
*/
publicstaticString getSysDate(String format){
Date now = newDate();
//可以方便地修改日期格式
SimpleDateFormat dateFormat = newSimpleDateFormat(format);
String sysDate = dateFormat.format(now);
returnsysDate;
}