Java常用日期封装
; ????} ????return ""; ??} ?????/** ???* 判断当前日期是否在两个日期之间 ???* @param startDate 开始时间 ???* @param endDate 结束时间 ???* @return ???*/ ??public static boolean betweenStartDateAndEndDate(Date startDate,Date endDate){ ????boolean bool=false; ????Date curDate=new Date(); ????if (curDate.after(startDate) && curDate.before(DateUtil.dateAdd( INTERVAL_DAY ,endDate,1)) ){ ??????bool=true; ????} ????return bool; ???????} ?????/** ???* 判断当前时间是否在在两个时间之间 ???* @param startDate 开始时间 ???* @param endDate 结束时间 ???* @return ???*/ ??public static boolean nowDateBetweenStartDateAndEndDate(Date startDate,Date endDate){ ????boolean bool=false; ????Date curDate=new Date(); ????if (curDate.after(startDate) && curDate.before(endDate)){ ??????bool=true; ????} ????return bool; ??} ?????/** ???* 判断当前时间是否在date之后 ???* @param date ???* @return ???*/ ??public static boolean nowDateAfterDate(Date date){ ????boolean bool=false; ????Date curDate=new Date(); ????if (curDate.after(date)){ ??????bool=true; ????} ????return bool; ??} ????????/** ???* 判断二个日期相隔的天数,结束时间为null时,,取当前时间 ???* @param startDate 开始时间 ???* @param endDate 结束时间 ???* @return ???*/ ??public static int getBetweenTodaysStartDateAndEndDate(Date startDate,Date endDate){ ????int betweentoday = 0; ????if(startDate==null){ ??????return betweentoday; ????} ????if(endDate==null){ ??????Calendar calendar = Calendar.getInstance(); ??????String year = new Integer(calendar.get(Calendar.YEAR)).toString(); ??????String month = new Integer((calendar.get(calendar.MONTH)+1)).toString(); ??????String day =? new Integer(calendar.get(calendar.DAY_OF_MONTH)).toString(); ??????String strtodaytime = year+"-"+month+"-"+day; ??????DateFormat? formatter=new SimpleDateFormat("yyyy-MM-dd");?? ??????try { ????????endDate = formatter.parse(strtodaytime); ??????} catch (ParseException e) { ????????//TODO Auto-generated catch block ????????e.printStackTrace(); ??????} ????} ?????????if(endDate.after(startDate)){ ??????betweentoday =? (int)((endDate.getTime() -startDate.getTime())/86400000); ????}else{ ??????betweentoday =? (int)((startDate.getTime() -endDate.getTime())/86400000); ????} ????return betweentoday; ??} ?????/**?? ??????*?? 取得指定长度日期时间字符串{不含格式}?? ??????????@param?? format?? 时间格式由常量决定?? ??????????8: YYMMDDHH??????????? 8位?? ????????10: YYMMDDHHmm????????? 10位?? ??????12: YYMMDDHHmmss??????? 12位?? ??????14: YYYYMMDDHHmmss????? 14位?? ??????15: YYMMDDHHmmssxxx???? 15位?? (最后的xxx?? 是毫秒) ???????*/ ??public?? static? String? getTime(int? format){?? ????????StringBuffer?? cTime=new?? StringBuffer(10);?? ????????Calendar?? time=Calendar.getInstance();?? ????????int?? miltime=time.get(Calendar.MILLISECOND);?? ????????int?? second=time.get(Calendar.SECOND);?? ????????int?? minute=time.get(Calendar.MINUTE);?? ????????int?? hour=time.get(Calendar.HOUR_OF_DAY);?? ????????int?? day?? =time.get(Calendar.DAY_OF_MONTH);?? ????????int?? month=time.get(Calendar.MONTH)+1;?? ????????int?? year?? =time.get(Calendar.YEAR);?? ????????if(format!=14){?? ????????????????if(year>=2000)?? year=year-2000;?? ????????????????else?? year=year-1900;?? ????????}?? ????????if(format>=2){?? ????????????????if(format==14)?? cTime.append(year);?? ????????????????else???????? cTime.append(getFormatTime(year,2));?? ????????}?? ????????if(format>=4)?? ????????????????cTime.append(getFormatTime(month,2));?? ????????if(format>=6)?? ????????????????cTime.append(getFormatTime(day,2));?? ????????if(format>=8)?? ????????????????cTime.append(getFormatTime(hour,2));?? ????????if(format>=10)?? ????????????????cTime.append(getFormatTime(minute,2));?? ????????if(format>=12)?? ????????????????cTime.append(getFormatTime(second,2));?? ????????if(format>=15)?? ????????????????cTime.append(getFormatTime(miltime,3));?? ????????return?? cTime.toString();?? ??}?? ????/**?? ??????* 产生任意位的字符串?? ??????*?? @param?? time?? 要转换格式的时间?? ??????*?? @param?? format 转换的格式?? ??????*?? @return String?? 转换的时间?? ??????*/ ??private? static? String? getFormatTime(int? time,int?? format){?? ??????????StringBuffer?? numm=new?? StringBuffer();?? ??????????int?? length=String.valueOf(time).length(); ??????????if(format<length)?? return?? null; ??????????for(int?? i=0?? ;i<format-length?? ;i++){?? ??????????????????numm.append("0");?? ??????????}?? ??????????numm.append(time);?? ??????????return?? numm.toString().trim();?? ???}?? ????????/** ???* 根据生日去用户年龄 ???* @param birthday ???* @return int ???* @exception ???* @author???? 豆皮 ???* @Date?????? Apr 24, 2008 ???*/ ??public static int getUserAge(Date birthday){ ?????if(birthday == null) return 0; ?????Calendar cal = Calendar.getInstance(); ???????if(cal.before(birthday)) { ?????????return 0; ?????} ?????int yearNow = cal.get(Calendar.YEAR); ?????cal.setTime(birthday);// 给时间赋值 ?????int yearBirth = cal.get(Calendar.YEAR); ?????return yearNow - yearBirth; ??} ?????/** ?????* 将int型时间(1970年至今的秒数)转换成Date型时间 ?????* @param unixTime 1970年至今的秒数 ?????* @return ?????* @author???? 郑卿 ?????*/ ????public static Date getDateByUnixTime(int unixTime){ ????????return new Date(unixTime*1000L); ????} ?????????/** ?????* 将Date型时间转换成int型时间(1970年至今的秒数) ?????* @param unixTime 1970年至今的秒数 ?????* @return ?????* @author???? 郑卿 ?????*/ ????public static int getUnixTimeByDate(Date date){ ????????return (int)(date.getTime()/1000); ????} ??????public static void main(String[] args) { ??????Date date1 =dateFormat("1981-01-01 00:00:00"); ??????Date date2 =dateFormat("1900-12-31 00:00:00"); ??????System.out.println(birthdayFormat(date1)); ????????System.out.println(birthdayFormat(date2)); ??} ??public static Date getNextDay(Date date) { ????long time = (date.getTime() / 1000) + 60 * 60 * 24; ????date.setTime(time * 1000); ????SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); ????try { ??????date = format.parse(format.format(date)); ????} catch (Exception ex) { ??????System.out.println(ex.getMessage()); ????} ????return date; ???} ???/** ???* @param date ???* @return ???* 复制新Date,不改变参数 ???*/ ??public static Date nextDay(Date date) { ????Date newDate = (Date) date.clone(); ????long time = (newDate.getTime() / 1000) + 60 * 60 * 24; ????newDate.setTime(time * 1000); ????SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); ????try { ??????newDate = format.parse(format.format(newDate)); ????} catch (Exception ex) { ??????System.out.println(ex.getMessage()); ????} ????return newDate; ???} ???@SuppressWarnings("unused") ??public static Date getNowTime() { ????SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ????Date date = new Date(); ????String dateStr = dateFormat(date); ????try { ??????date = format.parse(dateStr); ????} catch (ParseException e) { ??????e.printStackTrace(); ????} ????return date; ??} ???public static Date getTomorrow(Date date1) { ?????// 创建当前时间对象 ????Calendar now = Calendar.getInstance(); ????now.setTime(date1); ????// 日期[+1]day ????now.add(Calendar.DATE, 1); ????return now.getTime(); ??} ???public static Date getWeekAgo(Date date) { ????Date newDate = (Date) date.clone(); ????long time = (newDate.getTime() / 1000) - 60 * 60 * 24 * 7; ????newDate.setTime(time * 1000); ????SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); ????try { ??????newDate = format.parse(format.format(newDate)); ????} catch (Exception ex) { ??????System.out.println(ex.getMessage()); ????} ????return newDate; ??} ???public static Date getDatebyTime(Date date, int n) { ????String str = DateUtil.dateFormat(date, "yyyy-MM-dd"); ????String[] strs = str.split("-"); ????int month = Integer.parseInt(strs[1]); ????int monthnow = (month + n) % 12; ????int year = Integer.parseInt(strs[0]) + (month + n) / 12; ????str = String.valueOf(year) + "-" + String.valueOf(monthnow) + "-" ????????+ strs[2]; ????return DateUtil.dateFormat(str, "yyyy-MM-dd"); ??} ???/** ???* @param date ???* @return ???* 复制新Date,不改变参数 ???*/ ??public static Date yesterday(Date date) { ????Date newDate = (Date) date.clone(); ????long time = (newDate.getTime() / 1000) - 60 * 60 * 24; ????newDate.setTime(time * 1000); ????SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); ????try { ??????newDate = format.parse(format.format(newDate)); ????} catch (Exception ex) { ??????System.out.println(ex.getMessage()); ????} ????return newDate; ??} ???public static Date getYesterday(Date date) { ????long time = (date.getTime() / 1000) - 60 * 60 * 24; ????date.setTime(time * 1000); ????SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); ????try { ??????date = format.parse(format.format(date)); ????} catch (Exception ex) { ??????System.out.println(ex.getMessage()); ????} ????return date; ??} ???private static SimpleDateFormat format = null; ??@SuppressWarnings("unused") ??public static String getStringNowTime() { ????format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ????Date date = new Date(); ????String dateStr = dateFormat(date); ?????return dateStr; ??} ????????/** ?????* 指定时间的秒数 ?????* 指定时间零点的秒数加指定天数的秒数 ?????* @param time 时间 ?????* @param range? 天 ?????* @return ?????*/ ????public static long getSpecifyTimeSec(long time,int range){ ??????Date date?????? = new Date((time*1000+(23-Calendar.ZONE_OFFSET)*3600000)/86400000*86400000-(23-Calendar.ZONE_OFFSET)*3600000);?? ??????long zeroTime???? = date.getTime()/1000; ??????long specifyTime? = range * 24 * 3600; ??????return (zeroTime+specifyTime); ????} ?????????/** ?????* 将int型时间(1970年至今的秒数)转换成指定格式的时间 ?????* ?????* @param unixTime 1970年至今的秒数 ?????* @param dateFormat 时间格式 ?????* @return ?????* @author? sky ?????*/ ????public static String formatDateByUnixTime(long unixTime, String dateFormat){ ????????return dateFormat(new Date(unixTime*1000), dateFormat); ????} ???}
Java惯用日期封装
Java常用日期封装 ????} ????return ??} ?????/** ???* 判断当前日期是否在两个日期之间 ???* @param
