packagecom.JUtils.date;importjava.text.DateFormat;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;/*** @desc:时间处理工具类*/
public classDateUtils {private static final String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};/*** 根据指定格式获取当前时间
*@paramformat
*@returnString*/
public staticString getCurrentTime(String format){
SimpleDateFormat sdf=DateFormatUtils.getFormat(format);
Date date= newDate();returnsdf.format(date);
}/*** 获取当前时间,格式为:yyyy-MM-dd HH:mm:ss
*@returnString*/
public staticString getCurrentTime(){returngetCurrentTime(DateFormatUtils.DATE_FORMAT2);
}/*** 获取指定格式的当前时间:为空时格式为yyyy-mm-dd HH:mm:ss
*@paramformat
*@returnDate*/
public staticDate getCurrentDate(String format){
SimpleDateFormat sdf=DateFormatUtils.getFormat(format);
String dateS=getCurrentTime(format);
Date date= null;try{
date=sdf.parse(dateS);
}catch(ParseException e) {
e.printStackTrace();
}returndate;
}/*** 获取当前时间,格式为yyyy-MM-dd HH:mm:ss
*@returnDate*/
public staticDate getCurrentDate(){returngetCurrentDate(DateFormatUtils.DATE_FORMAT2);
}/*** 给指定日期加入年份,为空时默认当前时间
*@paramyear 年份 正数相加、负数相减
*@paramdate 为空时,默认为当前时间
*@paramformat 默认格式为:yyyy-MM-dd HH:mm:ss
*@returnString*/
public static String addYearToDate(intyear,Date date,String format){
Calendar calender=getCalendar(date,format);
SimpleDateFormat sdf=DateFormatUtils.getFormat(format);
calender.add(Calendar.YEAR, year);returnsdf.format(calender.getTime());
}/*** 给指定日期加入年份,为空时默认当前时间
*@paramyear 年份 正数相加、负数相减
*@paramdate 为空时,默认为当前时间
*@paramformat 默认格式为:yyyy-MM-dd HH:mm:ss
*@returnString*/
public static String addYearToDate(intyear,String date,String format){
Date newDate= newDate();if(null != date && !"".equals(date)){
newDate=string2Date(date, format);
}returnaddYearToDate(year, newDate, format);
}/*** 给指定日期增加月份 为空时默认当前时间
*@parammonth 增加月份 正数相加、负数相减
*@paramdate 指定时间
*@paramformat 指定格式 为空默认 yyyy-mm-dd HH:mm:ss
*@returnString*/
public static String addMothToDate(intmonth,Date date,String format) {
Calendar calender=getCalendar(date,format);
SimpleDateFormat sdf=DateFormatUtils.getFormat(format);
calender.add(Calendar.MONTH, month);returnsdf.format(calender.getTime());
}/*** 给指定日期增加月份 为空时默认当前时间
*@parammonth 增加月份 正数相加、负数相减
*@paramdate 指定时间
*@paramformat 指定格式 为空默认 yyyy-mm-dd HH:mm:ss
*@returnString*/
public static String addMothToDate(intmonth,String date,String format) {
Date newDate= newDate();if(null != date && !"".equals(date)){
newDate=string2Date(date, format);
}returnaddMothToDate(month, newDate, format);
}/*** 给指定日期增加天数,为空时默认当前时间
*@paramday 增加天数 正数相加、负数相减
*@paramdate 指定日期
*@paramformat 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
*@returnString*/
public static String addDayToDate(intday,Date date,String format) {
Calendar calendar=getCalendar(date, format);
SimpleDateFormat sdf=DateFormatUtils.getFormat(format);
calendar.add(Calendar.DATE, day);returnsdf.format(calendar.getTime());
}/*** 给指定日期增加天数,为空时默认当前时间
*@paramday 增加天数 正数相加、负数相减
*@paramdate 指定日期
*@paramformat 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
*@returnString*/
public static String addDayToDate(intday,String date,String format) {
Date newDate= newDate();if(null != date && !"".equals(date)){
newDate=string2Date(date, format);
}returnaddDayToDate(day, newDate, format);
}/*** 给指定日期增加小时,为空时默认当前时间
*@paramhour 增加小时 正数相加、负数相减
*@paramdate 指定日期
*@paramformat 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
*@returnString*/
public static String addHourToDate(inthour,Date date,String format) {
Calendar calendar=getCalendar(date, format);
SimpleDateFormat sdf=DateFormatUtils.getFormat(format);
calendar.add(Calendar.HOUR, hour);returnsdf.format(calendar.getTime());
}/*** 给指定日期增加小时,为空时默认当前时间
*@paramhour 增加小时 正数相加、负数相减
*@paramdate 指定日期
*@paramformat 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
*@returnString*/
public static String addHourToDate(inthour,String date,String format) {
Date newDate= newDate();if(null != date && !"".equals(date)){
newDate=string2Date(date, format);
}returnaddHourToDate(hour, newDate, format);
}/*** 给指定的日期增加分钟,为空时默认当前时间
*@paramminute 增加分钟 正数相加、负数相减
*@paramdate 指定日期
*@paramformat 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
*@returnString*/
public static String addMinuteToDate(intminute,Date date,String format) {
Calendar calendar=getCalendar(date, format);
SimpleDateFormat sdf=DateFormatUtils.getFormat(format);
calendar.add(Calendar.MINUTE, minute);returnsdf.format(calendar.getTime());
}/*** 给指定的日期增加分钟,为空时默认当前时间
*@paramminute 增加分钟 正数相加、负数相减
*@paramdate 指定日期
*@paramformat 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
*@returnString*/
public static String addMinuteToDate(intminute,String date,String format){
Date newDate= newDate();if(null != date && !"".equals(date)){
newDate=string2Date(date, format);
}returnaddMinuteToDate(minute, newDate, format);
}/*** 给指定日期增加秒,为空时默认当前时间
*@paramsecond 增加秒 正数相加、负数相减
*@paramdate 指定日期
*@paramformat 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
*@returnString*/
public static String addSecondToDate(intsecond,Date date,String format){
Calendar calendar=getCalendar(date, format);
SimpleDateFormat sdf=DateFormatUtils.getFormat(format);
calendar.add(Calendar.SECOND, second);returnsdf.format(calendar.getTime());
}/*** 给指定日期增加秒,为空时默认当前时间
*@paramsecond 增加秒 正数相加、负数相减
*@paramdate 指定日期
*@paramformat 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
*@returnString
*@throwsException*/
public static String addSecondToDate(intsecond,String date,String format){
Date newDate= newDate();if(null != date && !"".equals(date)){
newDate=string2Date(date, format);
}returnaddSecondToDate(second, newDate, format);
}/*** 获取指定格式指定时间的日历
*@paramdate 时间
*@paramformat 格式
*@returnCalendar*/
public staticCalendar getCalendar(Date date,String format){if(date == null){
date=getCurrentDate(format);
}
Calendar calender=Calendar.getInstance();
calender.setTime(date);returncalender;
}/*** 字符串转换为日期,日期格式为
*@paramvalue
*@return
*/
public staticDate string2Date(String value){if(value == null || "".equals(value)){return null;
}
SimpleDateFormat sdf=DateFormatUtils.getFormat(DateFormatUtils.DATE_FORMAT2);
Date date= null;try{
value=DateFormatUtils.formatDate(value, DateFormatUtils.DATE_FORMAT2);
date=sdf.parse(value);
}catch(Exception e) {
e.printStackTrace();
}returndate;
}/*** 将字符串(格式符合规范)转换成Date
*@paramvalue 需要转换的字符串
*@paramformat 日期格式
*@returnDate*/
public staticDate string2Date(String value,String format){if(value == null || "".equals(value)){return null;
}
SimpleDateFormat sdf=DateFormatUtils.getFormat(format);
Date date= null;try{
value=DateFormatUtils.formatDate(value, format);
date=sdf.parse(value);
}catch(Exception e) {
e.printStackTrace();
}returndate;
}/*** 将日期格式转换成String
*@paramvalue 需要转换的日期
*@paramformat 日期格式
*@returnString*/
public staticString date2String(Date value,String format){if(value == null){return null;
}
SimpleDateFormat sdf=DateFormatUtils.getFormat(format);returnsdf.format(value);
}/*** 日期转换为字符串
*
*@paramvalue
*@return
*/
public staticString date2String(Date value){if(value == null){return null;
}
SimpleDateFormat sdf=DateFormatUtils.getFormat(DateFormatUtils.DATE_FORMAT2);returnsdf.format(value);
}/*** 获取指定日期的年份
*@paramvalue 日期
*@returnint*/
public static intgetCurrentYear(Date value){
String date=date2String(value, DateFormatUtils.DATE_YEAR);returnInteger.valueOf(date);
}/*** 获取指定日期的年份
*@paramvalue 日期
*@returnint*/
public static intgetCurrentYear(String value) {
Date date=string2Date(value, DateFormatUtils.DATE_YEAR);
Calendar calendar=getCalendar(date, DateFormatUtils.DATE_YEAR);returncalendar.get(Calendar.YEAR);
}/*** 获取指定日期的月份
*@paramvalue 日期
*@returnint*/
public static intgetCurrentMonth(Date value){
String date=date2String(value, DateFormatUtils.DATE_MONTH);returnInteger.valueOf(date);
}/*** 获取指定日期的月份
*@paramvalue 日期
*@returnint*/
public static intgetCurrentMonth(String value) {
Date date=string2Date(value, DateFormatUtils.DATE_MONTH);
Calendar calendar=getCalendar(date, DateFormatUtils.DATE_MONTH);returncalendar.get(Calendar.MONTH);
}/*** 获取指定日期的天份
*@paramvalue 日期
*@returnint*/
public static intgetCurrentDay(Date value){
String date=date2String(value, DateFormatUtils.DATE_DAY);returnInteger.valueOf(date);
}/*** 获取指定日期的天份
*@paramvalue 日期
*@returnint*/
public static intgetCurrentDay(String value){
Date date=string2Date(value, DateFormatUtils.DATE_DAY);
Calendar calendar=getCalendar(date, DateFormatUtils.DATE_DAY);returncalendar.get(Calendar.DATE);
}/*** 获取当前日期为星期几
*@paramvalue 日期
*@returnString*/
public staticString getCurrentWeek(Date value) {
Calendar calendar=getCalendar(value, DateFormatUtils.DATE_FORMAT1);int weekIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1 < 0 ? 0 : calendar.get(Calendar.DAY_OF_WEEK) - 1;returnweeks[weekIndex];
}/*** 获取当前日期为星期几
*@paramvalue 日期
*@returnString*/
public staticString getCurrentWeek(String value) {
Date date=string2Date(value, DateFormatUtils.DATE_FORMAT1);returngetCurrentWeek(date);
}/*** 获取指定日期的小时
*@paramvalue 日期
*@returnint*/
public static intgetCurrentHour(Date value){
String date=date2String(value, DateFormatUtils.DATE_HOUR);returnInteger.valueOf(date);
}/*** 获取指定日期的小时
*@paramvalue 日期
*@return*@returnint*/
public static intgetCurrentHour(String value) {
Date date=string2Date(value, DateFormatUtils.DATE_HOUR);
Calendar calendar=getCalendar(date, DateFormatUtils.DATE_HOUR);returncalendar.get(Calendar.DATE);
}/*** 获取指定日期的分钟
*@paramvalue 日期
*@returnint*/
public static intgetCurrentMinute(Date value){
String date=date2String(value, DateFormatUtils.DATE_MINUTE);returnInteger.valueOf(date);
}/*** 获取指定日期的分钟
*@paramvalue 日期
*@returnint*/
public static intgetCurrentMinute(String value){
Date date=string2Date(value, DateFormatUtils.DATE_MINUTE);
Calendar calendar=getCalendar(date, DateFormatUtils.DATE_MINUTE);returncalendar.get(Calendar.MINUTE);
}/*** 比较两个日期相隔多少天(月、年)
* 例:
* compareDate("2009-09-12", null, 0);//比较天
* compareDate("2009-09-12", null, 1);//比较月
* compareDate("2009-09-12", null, 2);//比较年
*@paramstartDay 需要比较的时间 不能为空(null),需要正确的日期格式 ,如:2009-09-12
*@paramendDay 被比较的时间 为空(null)则为当前时间
*@paramstype 返回值类型 0为多少天,1为多少个月,2为多少年
*@returnint*/
public static int compareDate(String startDay,String endDay,intstype) {int n = 0;
startDay= DateFormatUtils.formatDate(startDay, "yyyy-MM-dd");
endDay= DateFormatUtils.formatDate(endDay, "yyyy-MM-dd");
String formatStyle= "yyyy-MM-dd";if(1 ==stype){
formatStyle= "yyyy-MM";
}else if(2 ==stype){
formatStyle= "yyyy";
}
endDay= endDay==null ? getCurrentTime("yyyy-MM-dd") : endDay;
DateFormat df= newSimpleDateFormat(formatStyle);
Calendar c1=Calendar.getInstance();
Calendar c2=Calendar.getInstance();try{
c1.setTime(df.parse(startDay));
c2.setTime(df.parse(endDay));
}catch(Exception e) {
e.printStackTrace();
}while (!c1.after(c2)) { //循环对比,直到相等,n 就是所要的结果
n++;if(stype==1){
c1.add(Calendar.MONTH,1); //比较月份,月份+1
}else{
c1.add(Calendar.DATE,1); //比较天数,日期+1
}
}
n= n-1;if(stype==2){
n= (int)n/365;
}returnn;
}/*** 比较两个时间相差多少小时(分钟、秒)
*@paramstartTime 需要比较的时间 不能为空,且必须符合正确格式:2012-12-12 12:12:
*@paramendTime 需要被比较的时间 若为空则默认当前时间
*@paramtype 1:小时 2:分钟 3:秒
*@returnint*/
public static int compareTime(String startTime , String endTime , inttype) {//endTime是否为空,为空默认当前时间
if(endTime == null || "".equals(endTime)){
endTime=getCurrentTime();
}
SimpleDateFormat sdf= DateFormatUtils.getFormat("");int value = 0;try{
Date begin=sdf.parse(startTime);
Date end=sdf.parse(endTime);long between = (end.getTime() - begin.getTime()) / 1000; //除以1000转换成豪秒
if(type == 1){ //小时
value = (int) (between % (24 * 36000) / 3600);
}else if(type == 2){
value= (int) (between % 3600 / 60);
}else if(type == 3){
value= (int) (between % 60 / 60);
}
}catch(ParseException e) {
e.printStackTrace();
}returnvalue;
}/*** 比较两个日期的大小。
* 若date1 > date2 则返回 1
* 若date1 = date2 则返回 0
* 若date1 < date2 则返回-1
*
*@paramdate1
*@paramdate2
*@paramformat 待转换的格式
*@return比较结果*/
public static intcompare(String date1, String date2,String format) {
DateFormat df=DateFormatUtils.getFormat(format);try{
Date dt1=df.parse(date1);
Date dt2=df.parse(date2);if (dt1.getTime() >dt2.getTime()) {return 1;
}else if (dt1.getTime()
}else{return 0;
}
}catch(Exception exception) {
exception.printStackTrace();
}return 0;
}/*** 获取指定月份的第一天
*
*@paramdate
*@return
*/
public staticString getMonthFirstDate(String date){
date=DateFormatUtils.formatDate(date);return DateFormatUtils.formatDate(date, "yyyy-MM") + "-01";
}/*** 获取指定月份的最后一天
*
*
*@paramstrdate
*@return
*/
public staticString getMonthLastDate(String date) {
Date strDate=DateUtils.string2Date(getMonthFirstDate(date));
Calendar calendar=Calendar.getInstance();
calendar.setTime(strDate);
calendar.add(Calendar.MONTH,1);
calendar.add(Calendar.DAY_OF_YEAR,-1);returnDateFormatUtils.formatDate(calendar.getTime());
}/*** 获取所在星期的第一天
*
*@paramdate
*@return
*/@SuppressWarnings("static-access")public staticDate getWeekFirstDate(Date date) {
Calendar now=Calendar.getInstance();
now.setTime(date);int today =now.get(Calendar.DAY_OF_WEEK);int first_day_of_week = now.get(Calendar.DATE) + 2 - today; //星期一
now.set(now.DATE, first_day_of_week);returnnow.getTime();
}/*** 获取所在星期的最后一天
*
*
*@paramdate
*@return
*/@SuppressWarnings("static-access")public staticDate geWeektLastDate(Date date) {
Calendar now=Calendar.getInstance();
now.setTime(date);int today =now.get(Calendar.DAY_OF_WEEK);int first_day_of_week = now.get(Calendar.DATE) + 2 - today; //星期一
int last_day_of_week = first_day_of_week + 6; //星期日
now.set(now.DATE, last_day_of_week);returnnow.getTime();
}
}