package com.appUtils; import java.math.RoundingMode; import java.text.DateFormat; import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TimeZone; import java.util.regex.Matcher; import java.util.regex.Pattern; public class DateUtils { /**定义常量**/ public static final String DATE_YY_MM_DD="yyyy-MM-dd"; public static final String DATE_JFP_STR="yyyyMM"; public static final String DATE_FULL_STR = "yyyy-MM-dd HH:mm:ss"; public static final String DATE_MINUTE_STR = "yyyy-MM-dd HH:mm"; public static final String DATE_SMALL_STR = "yyyyMMdd"; public static final String DATE_LAST_STR = "HH:mm"; public static final String DATE_KEY_STR = "yyMMddHHmmss"; /** * 获取系统当前时间(yyyy-MM-dd HH:mm:ss) * @return */ public static String getNowTime() { return getNowTime(DATE_FULL_STR); } /** * 获取指定格式的系统当前时间 * @param type 日期格式 * @return */ public static String getNowTime(String type) { SimpleDateFormat df = new SimpleDateFormat(type); return df.format(new Date()); } /** * 获取系统当前计费期 * @return */ public static String getJFPTime() { SimpleDateFormat df = new SimpleDateFormat(DATE_JFP_STR); return df.format(new Date()); } /** * 字符串日期转为日期(yyyy-MM-dd) * @param strDate 日期字符串 * @return Date */ public static Date parse(String strDate) { return parse(strDate,DATE_YY_MM_DD); } /** * 字符串日期转为指定格式的日期 * @param strDate 日期字符串 * @param pattern 日期格式 * @return Date */ public static Date parse(String strDate, String pattern) { SimpleDateFormat df = new SimpleDateFormat(pattern); try { try { Date date = df.parse(strDate); return date; } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 日期字符串转换格式(yyyy-MM-dd) * @param strDate 日期字符串 * @return String */ public static String parseDateString(String date) { return parseDateString(date,DATE_YY_MM_DD) ; } /** * 日期字符串转换格式 * @param strDate 日期字符串 * @param pattern 日期格式 * @return String */ public static String parseDateString(String strDate,String pattern) { SimpleDateFormat df = new SimpleDateFormat(pattern); try { try { Date date = df.parse(strDate); return df.format(date); } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 将日期转化为指定格式的字符串(yyyy-MM-dd) * @param date * @return String */ public static String formatDateToString(Date date){ return formatDateToString(date,DATE_YY_MM_DD); } /** * 将日期转化为指定格式的字符串 * @param date * @param pattern 日期格式 * @return String */ public static String formatDateToString(Date date,String pattern){ SimpleDateFormat df = new SimpleDateFormat(pattern); try { try { return df.format(date); } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 与当前时间比较 * @param date * @return */ public static int compareDateWithNow(Date date1){ Date date2 = new Date(); int rnum =date1.compareTo(date2); return rnum; } /*** * 两个时间进行比较 * @param date1 指定时间 * @param date2 指定时间 * @return */ public static int compareDateWithParms(Date date1,Date date2){ int rnum = date1.compareTo(date2); return rnum; } /** * 与当前时间比较(时间戳比较) * @param date * @return */ public static int compareDateWithNow(long date1){ long date2 = dateToUnixTimestamp(); if(date1>date2){ return 1; }else if(date1date1 返回1 相等0,小于返回-1 */ public static int dateCompare(String date1,String date2) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); int flag = 0; try { Date d1 = df.parse(date1); Date d2 = df.parse(date2); if(d1.getTime() > d2.getTime()){ flag = 1; }else if(d1.getTime() < d2.getTime()){ flag = -1; }else{ flag = 0; } } catch (Exception e) { e.printStackTrace(); } return flag; } /** * 比较时间大小 * @param date1 * @param date2 * @partten 日期格式字符串 如:yyyy-MM-dd 等等 * @return */ public static int dateCompare(String date1,String date2,String partten) { DateFormat df = new SimpleDateFormat(partten); int flag = 0; try { Date d1 = df.parse(date1); Date d2 = df.parse(date2); if(d1.getTime() > d2.getTime()){ flag = 1; }else if(d1.getTime() < d2.getTime()){ flag = -1; }else{ flag = 0; } } catch (Exception e) { e.printStackTrace(); } return flag; } /*** * 处理上传时间 空格 * @return */ public static String formartDate(String time){ int index = time.indexOf("+"); if(index<0){ return time; }else{ String head = time.substring(0,index); String end = time.substring(index+1,time.length()); String temp = head+" "+end; return formartDate(temp); } } /** * 通过年月得到上一季度 * @param year //年 * @param month 月 * @return */ public static int[] getSeanson(int year,int month){ int [] datas = new int[2]; int newyear = 0; int newmonth =0; if(month>=1 && month <=3){ //第一季度 newyear = year-1; newmonth = 4; }else if(month>=4 && month <=6){//第二季度 newyear = year; newmonth = 1; }else if(month>=7 && month <=9){//第 三季度 newyear = year; newmonth = 2; }else{ //第四季度 newyear = year; newmonth = 3; } datas[0] = newyear; datas[1] = newmonth; return datas; }; /** * 通过年月得到当前季度 * @param year //年 * @param month 月 * @return */ public static int[] getSeanson2(int year,int month){ int [] datas = new int[2]; int newyear = 0; int newmonth =0; if(month>=1 && month <=3){ //第一季度 newyear = year; newmonth = 1; }else if(month>=4 && month <=6){//第二季度 newyear = year; newmonth = 2; }else if(month>=7 && month <=9){//第 三季度 newyear = year; newmonth = 3; }else{ //第四季度 newyear = year; newmonth = 4; } datas[0] = newyear; datas[1] = newmonth; return datas; }; /**** * 根据月份算季度 */ public static int getjidu(Date date){ int month=date.getMonth(); if(month>=1 && month <=3){ //第一季度 return 1; }else if(month>=4 && month <=6){//第二季度 return 2; }else if(month>=7 && month <=9){//第 三季度 return 3; }else{ //第四季度 return 4; } } /** * 计算两个日期相差几个月 * @param date1 * @param date2 * @return * @throws java.text.ParseException */ public static int getMonthDiffer(String date1,String date2) throws Exception{ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); Calendar bef = Calendar.getInstance(); Calendar aft = Calendar.getInstance(); bef.setTime(sdf.parse(date1)); aft.setTime(sdf.parse(date2)); int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH); int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12; return Math.abs(month + result); } /** * 计算两个日期相差的天数 * @param smdate * @param bdate * @return * @throws ParseException * @throws java.text.ParseException */ public static int daysBetween(Date smdate,Date bdate) throws Exception{ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); smdate=sdf.parse(sdf.format(smdate)); bdate=sdf.parse(sdf.format(bdate)); Calendar cal = Calendar.getInstance(); cal.setTime(smdate); long time1 = cal.getTimeInMillis(); cal.setTime(bdate); long time2 = cal.getTimeInMillis(); long between_days=(time2-time1)/(1000*3600*24); return Integer.parseInt(String.valueOf(between_days)); } /** * 计算两个日期相差的天数 * @param smdate * @param bdate * @return * @throws ParseException * @throws java.text.ParseException */ public static int daysBetween(String smdate,String bdate) throws Exception{ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Date fdate=sdf.parse(smdate); Date ldate=sdf.parse(bdate); Calendar cal = Calendar.getInstance(); cal.setTime(fdate); long time1 = cal.getTimeInMillis(); cal.setTime(ldate); long time2 = cal.getTimeInMillis(); long between_days=(time2-time1)/(1000*3600*24); return Integer.parseInt(String.valueOf(between_days)); } /** * 计算两个时间相差的分钟数 * @param smdate * @param bdate */ public static long minutesBetween(String smdate,String bdate) throws Exception{ SimpleDateFormat sdf=new SimpleDateFormat(DATE_FULL_STR); Date fdate=sdf.parse(smdate); Date ldate=sdf.parse(bdate); Calendar cal = Calendar.getInstance(); cal.setTime(fdate); long time1 = cal.getTimeInMillis(); cal.setTime(ldate); long time2 = cal.getTimeInMillis(); long between_minute=(time2-time1)/(1000*60); return between_minute; } /** * 根据某个日期获取一周的时间链表 * @return */ public static Map getWeekDays(String dayTime) { String[] a = dayTime.split("-"); int week = getDayOfWeek(a[0], a[1], a[2]);//获取周几 String format = "yyyy-MM-dd"; Map weekLsit = new HashMap(); String TempTime = dayTime; String TempTime2 = dayTime; for (int i = 1; i < week; i++) { String curTime =getFormatDateAdd(parse(TempTime, format), -1, format); TempTime = curTime; weekLsit.put("day"+(week-i), curTime); } for (int i = week; i <= 7; i++) { if (i==week) { weekLsit.put("day"+week, getFormatDateAdd(parse(TempTime2, format), 0, format)); }else{ String curTime =getFormatDateAdd(parse(TempTime2, format), 1, format); TempTime2 = curTime; weekLsit.put("day"+i, curTime); } } return weekLsit; } /** * 获取是周几 */ public static int getDayOfWeek(String year, String month, String day) { Calendar cal = new GregorianCalendar(new Integer(year).intValue(), new Integer(month).intValue() - 1, new Integer(day).intValue()); int weekNum = 0; //周一为一 if (cal.get(Calendar.DAY_OF_WEEK)==1) { weekNum=7; }else{ weekNum = cal.get(Calendar.DAY_OF_WEEK)-1; } return weekNum; } /** * 获取当前时间是本月的第几周 * @param dateStr * @return * @throws Exception */ public static int getWeek(String dateStr) throws Exception{ SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd"); Date date =sdf.parse(dateStr); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); //第几周 int week = calendar.get(Calendar.WEEK_OF_MONTH); return week; } /** * 取得给定日期加上一定天数后的日期对象. * @param date 给定的日期对象 * @param amount 需要添加的天数,如果是向前的天数,使用负数就可以. * @param format 输出格式. * @return Date 加上一定天数以后的Date对象. */ public static String getFormatDateAdd(Date date, int amount, String format) { Calendar cal = new GregorianCalendar(); cal.setTime(date); cal.add(GregorianCalendar.DATE, amount); SimpleDateFormat df = new SimpleDateFormat(format); return df.format(cal.getTime()); } /*** * 生成流水编号 日期加流水 加随机2位数 生成reportid */ public static String getReportid(){ String time = getNowTime(DateUtils.DATE_KEY_STR); int two = (int)(Math.random()*90+10); time =time+String.valueOf(two); return time; } /*** * 计算年龄 */ public static int getage(String date ){ SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd"); int age = 0; Date dateOfBirth; try { dateOfBirth = simple.parse(date); Calendar born = Calendar.getInstance(); Calendar now = Calendar.getInstance(); if (dateOfBirth != null) { now.setTime(new Date()); born.setTime(dateOfBirth); if (born.after(now)) { throw new IllegalArgumentException("年龄不能超过当前日期"); } age = now.get(Calendar.YEAR) - born.get(Calendar.YEAR); int nowDayOfYear = now.get(Calendar.DAY_OF_YEAR); int bornDayOfYear = born.get(Calendar.DAY_OF_YEAR); if (nowDayOfYear < bornDayOfYear) { age -= 1; } } return age; } catch (Exception e) { e.printStackTrace(); } return age; } /** * 处理小数点float * @return */ public static String dealwithFloat(float price){ DecimalFormat decimalFormat=new DecimalFormat(".00");//构造方法的字符格式这里如果小数不足2位,会以0补足. String p=decimalFormat.format(price);//format 返回的是字符串 return p; } /** * 处理小数点double * @return */ public static String dealwithDouble(double num){ DecimalFormat decimalFormat=new DecimalFormat(".00");//构造方法的字符格式这里如果小数不足2位,会以0补足. String p=decimalFormat.format(num);//format 返回的是字符串 return p; } public static String accuracy(double num, double total, int scale){ DecimalFormat df = (DecimalFormat)NumberFormat.getInstance(); //可以设置精确几位小数 df.setMaximumFractionDigits(scale); //模式 例如四舍五入 df.setRoundingMode(RoundingMode.HALF_UP); double accuracy_num = num / total * 100; return df.format(accuracy_num)+"%"; } public static boolean ischinesechar(String str){ boolean temp = false; Pattern p=Pattern.compile("[\u4e00-\u9fa5]"); Matcher m=p.matcher(str); if(m.find()){ temp = true; } return temp; } /*** * 将list进行分割出来 * @param list 集合 * @param separator 分割字符 * @return */ public static String listToString(List list, char separator) { StringBuilder sb = new StringBuilder(); if(list.size()==1){ return list.get(0); }else if(list.size()==0){ return ""; }else{ for (int i = 0; i < list.size(); i++){ if(list.get(i).equals("")|list.get(i)==null){ continue; }else{ sb.append(list.get(i)).append(separator); } } return sb.toString().substring(0,sb.toString().length()-1); } } /*** * 获取指定时间的前 N 天 * @param date 指定时间 * @param day 前 N 天 * @return 日期 * @throws java.text.ParseException */ public static Date getDateByN(String date, int n) throws Exception { SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd"); Date dates= simple.parse(date); Calendar calendar = new GregorianCalendar(); calendar.setTime(dates); calendar.add(Calendar.DATE,-n);//把日期往后增加一天.整数往后推,负数往前移动 dates=calendar.getTime(); //这个时间就是日期往后推一天的结果 return dates; } /*** * 获取指定时间的前 N 天 * @param date 指定时间 * @param day 前 N 天 * @return 日期 * @throws java.text.ParseException */ public static String getDateByNresultString(String date, int n) throws Exception { SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd"); Date dates= simple.parse(date); Calendar calendar = new GregorianCalendar(); calendar.setTime(dates); calendar.add(Calendar.DATE,-n);//把日期往后增加一天.整数往后推,负数往前移动 dates=calendar.getTime(); //这个时间就是日期往后推一天的结果 return simple.format(dates); } /** * 求出前一天 * @param dateTime * @param pattern * @return * @throws Exception */ public static String prevDate(String dateTime,String pattern) throws Exception{ SimpleDateFormat sdf=new SimpleDateFormat(pattern); Date date=sdf.parse(dateTime); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_MONTH, -1); date = calendar.getTime(); return sdf.format(date); } /** * 求出后一天 * @param dateTime * @param pattern * @return * @throws Exception */ public static String nextDate(String dateTime,String pattern) throws Exception{ SimpleDateFormat sdf=new SimpleDateFormat(pattern); Date date=sdf.parse(dateTime); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_MONTH, 1); date = calendar.getTime(); return sdf.format(date); } /** * 两个时间相差距离多少天多少小时 * @param str1 时间参数 1 格式:1990-01-01 12:00:00 * @param str2 时间参数 2 格式:2009-01-01 12:00:00 * @return long[] 返回值为:{天, 时} */ public static long[] getDistanceTimes(String str1, String str2) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd"); Date one; Date two; Date three; Date four; long day = 0; long hour = 0; try { one = df.parse(str1); two = df.parse(str2); three = df2.parse(str1); four = df2.parse(str2); long time1 = one.getTime(); long time2 = two.getTime(); long time3 = three.getTime(); long time4 = four.getTime(); long diff ; long diff2; if(time1