工具类一
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"; public static final String DATE_LAST = "HH:mm:ss"; /** * 获取系统当前时间(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(date1<date2){ return -1; }else{ return 0; } } /** * 将指定的日期转换成Unix时间戳 * @param String date 需要转换的日期 yyyy-MM-dd HH:mm:ss * @return long 时间戳 */ public static long dateToUnixTimestamp(String date) { long timestamp = 0; try { timestamp = new SimpleDateFormat(DATE_FULL_STR).parse(date).getTime(); } catch (Exception e) { e.printStackTrace(); } return timestamp; } /** * 将指定的日期转换成Unix时间戳 * @param String date 需要转换的日期 yyyy-MM-dd * @return long 时间戳 */ public static long dateToUnixTimestamp(String date, String dateFormat) { long timestamp = 0; try { try { timestamp = new SimpleDateFormat(dateFormat).parse(date).getTime(); } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } return timestamp; } /** * 将当前日期转换成Unix时间戳 * @return long 时间戳 */ public static long dateToUnixTimestamp() { long timestamp = new Date().getTime(); return timestamp; } /** * 将Unix时间戳转换成日期 * @param long timestamp 时间戳 * @return String 日期字符串 */ public static String unixTimestampToDate(long timestamp) { SimpleDateFormat sd = new SimpleDateFormat(DATE_FULL_STR); sd.setTimeZone(TimeZone.getTimeZone("GMT+8")); return sd.format(new Date(timestamp)); } /** * 指定时间字符串加n分钟 * @param dateStr * @param minute * @return String */ public static String dateAddMinute(String dateStr,int minute) { return formatDateToString(dateAddMinute(parse(dateStr,DATE_FULL_STR),minute),DATE_FULL_STR); } /** * 指定时间加n分钟 * @param date * @param minute * @return Date */ public static Date dateAddMinute(Date date,int minute) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MINUTE, minute); return cal.getTime(); } /** * 获取某月的第一天 * @Title:getFirstDayOfMonth * @Description: * @param:@param year * @param:@param month * @param:@return * @return:String * @throws */ public static String getFirstDayOfMonth(int year,int month){ Calendar cal = Calendar.getInstance(); //设置年份 cal.set(Calendar.YEAR,year); //设置月份 cal.set(Calendar.MONTH, month-1); //获取某月最小天数 int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH); //设置日历中月份的最小天数 cal.set(Calendar.DAY_OF_MONTH, firstDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String firstDayOfMonth = sdf.format(cal.getTime()); return firstDayOfMonth; } /** * 获取某月的最后一天 * @Title:getLastDayOfMonth * @Description: * @param:@param year * @param:@param month * @param:@return * @return:String * @throws */ public static String getLastDayOfMonth(int year,int month){ Calendar cal = Calendar.getInstance(); //设置年份 cal.set(Calendar.YEAR,year); //设置月份 cal.set(Calendar.MONTH, month-1); //获取某月最大天数 int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); //设置日历中月份的最大天数 cal.set(Calendar.DAY_OF_MONTH, lastDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String lastDayOfMonth = sdf.format(cal.getTime()); return lastDayOfMonth; } /** * 获取上月的第一天 * @Title:getFirstDayOfNextMonth * @Description: * @param:@param year * @param:@param month * @param:@return * @return:String * @throws */ public static String getFirstDayOfLastMonth(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.MONTH, 1); //获取某月最小天数 int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH); //设置日历中月份的最小天数 cal.set(Calendar.DAY_OF_MONTH, firstDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String firstDayOfMonth = sdf.format(cal.getTime()); return firstDayOfMonth; } /** * 获取上月的最后一天 * @Title:getLastDayOfNextMonth * @Description: * @param:@param year * @param:@param month * @param:@return * @return:String * @throws */ public static String getLastDayOfLastMonth(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MONTH, -1); //获取某月最大天数 int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); //设置日历中月份的最大天数 cal.set(Calendar.DAY_OF_MONTH, lastDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String lastDayOfMonth = sdf.format(cal.getTime()); return lastDayOfMonth; } /** * 获取下月的第一天 * @Title:getFirstDayOfNextMonth * @Description: * @param:@param year * @param:@param month * @param:@return * @return:String * @throws */ public static String getFirstDayOfNextMonth(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.MONTH, 1); //获取某月最小天数 int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH); //设置日历中月份的最小天数 cal.set(Calendar.DAY_OF_MONTH, firstDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String firstDayOfMonth = sdf.format(cal.getTime()); return firstDayOfMonth; } /** * 获取下月的最后一天 * @Title:getLastDayOfNextMonth * @Description: * @param:@param year * @param:@param month * @param:@return * @return:String * @throws */ public static String getLastDayOfNextMonth(Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MONTH, 1); //获取某月最大天数 int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); //设置日历中月份的最大天数 cal.set(Calendar.DAY_OF_MONTH, lastDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String lastDayOfMonth = sdf.format(cal.getTime()); return lastDayOfMonth; } /** * 获取去年同期最后一天 * @Title:getFirstDayOfLastYear * @Description: * @param:@param year * @param:@param month * @param:@return * @return:String * @throws */ public static String getFirstDayOfLastYear(int year,int month) { Calendar cal = Calendar.getInstance(); //设置年份 cal.set(Calendar.YEAR,year-1); //设置月份 cal.set(Calendar.MONTH, month-1); //获取某月最小天数 int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH); //设置日历中月份的最小天数 cal.set(Calendar.DAY_OF_MONTH, firstDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String firstDayOfMonth = sdf.format(cal.getTime()); return firstDayOfMonth; } /** * 获取去年同期最后一天 * @Title:getLastDayOfLastYear * @Description: * @param:@param year * @param:@param month * @param:@return * @return:String * @throws */ public static String getLastDayOfLastYear(int year,int month) { Calendar cal = Calendar.getInstance(); //设置年份 cal.set(Calendar.YEAR,year-1); //设置月份 cal.set(Calendar.MONTH, month-1); //获取某月最小天数 int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); //设置日历中月份的最小天数 cal.set(Calendar.DAY_OF_MONTH, lastDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String lastDayOfMonth = sdf.format(cal.getTime()); return lastDayOfMonth; } /** * 获取前年同期第一天 * @Title:getFirstDatOfTheYearBeforeLast * @Description: * @param:@param year * @param:@param month * @param:@return * @return:String * @throws */ public static String getFirstDayOfTheYearBeforeLast(int year,int month){ Calendar cal = Calendar.getInstance(); //设置年份 cal.set(Calendar.YEAR,year-2); //设置月份 cal.set(Calendar.MONTH, month-1); //获取某月最小天数 int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH); //设置日历中月份的最小天数 cal.set(Calendar.DAY_OF_MONTH, firstDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String firstDayOfMonth = sdf.format(cal.getTime()); return firstDayOfMonth; } /** * 获取前年同期最后一天 * @Title:getFirstDatOfTheYearBeforeLast * @Description: * @param:@param year * @param:@param month * @param:@return * @return:String * @throws */ public static String getLastDayOfTheYearBeforeLast(int year,int month){ Calendar cal = Calendar.getInstance(); //设置年份 cal.set(Calendar.YEAR,year-2); //设置月份 cal.set(Calendar.MONTH, month-1); //获取某月最小天数 int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); //设置日历中月份的最小天数 cal.set(Calendar.DAY_OF_MONTH, lastDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String lastDayOfMonth = sdf.format(cal.getTime()); return lastDayOfMonth; } /** * 获取明年同期最后一天 * @Title:getFirstDayOfNextYear * @Description: * @param:@param year * @param:@param month * @param:@return * @return:String * @throws */ public static String getFirstDayOfNextYear(int year,int month){ Calendar cal = Calendar.getInstance(); //设置年份 cal.set(Calendar.YEAR,year+1); //设置月份 cal.set(Calendar.MONTH, month-1); //获取某月最小天数 int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH); //设置日历中月份的最小天数 cal.set(Calendar.DAY_OF_MONTH, firstDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String firstDayOfMonth = sdf.format(cal.getTime()); return firstDayOfMonth; } /** * 获取明年同期最后一天 * @Title:getLastDayOfNextYear * @Description: * @param:@param year * @param:@param month * @param:@return * @return:String * @throws */ public static String getLastDayOfNextYear(int year,int month) { Calendar cal = Calendar.getInstance(); //设置年份 cal.set(Calendar.YEAR,year+1); //设置月份 cal.set(Calendar.MONTH, month-1); //获取某月最小天数 int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); //设置日历中月份的最小天数 cal.set(Calendar.DAY_OF_MONTH, lastDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String lastDayOfMonth = sdf.format(cal.getTime()); return lastDayOfMonth; } /** * 比较时间大小 * @param date1 * @param date2 * @return date1>date1 返回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(); return (time2-time1)/(1000*60); } /** * 计算两个时间相差的小时数 * @param smdate * @param bdate */ public static long hourBetween(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(); return (time2-time1)/(1000*60*60); } /** * 根据某个日期获取一周的时间链表 * @return */ public static Map<String, Object> getWeekDays(String dayTime) { String[] a = dayTime.split("-"); int week = getDayOfWeek(a[0], a[1], a[2]);//获取周几 String format = "yyyy-MM-dd"; Map<String, Object> weekLsit = new HashMap<String, Object>(); 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<String> 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<time2) { diff = time2 - time1; diff2 = time4 - time3; } else { diff = time1 - time2; diff2 = time3 - time4; } if(diff2==0){ //算出是当天 day=0; hour = (diff / (60 * 60 * 1000) - day * 24); }else{ day = diff2 / (24 * 60 * 60 * 1000); hour = Math.abs((diff / (60 * 60 * 1000) - day * 24)); } } catch (ParseException e) { e.printStackTrace(); } long[] times = {day, hour}; return times; } /** * 获取指定时间的小时 * @param dateTime * @return * @throws Exception */ public static int getHourByDateTime(String dateTime) throws Exception{ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date=sdf.parse(dateTime); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.HOUR_OF_DAY); } /** * 获取当年的第一天 * @param year * @return */ public static Date getCurrYearFirst(){ Calendar currCal=Calendar.getInstance(); int currentYear = currCal.get(Calendar.YEAR); return getYearFirst(currentYear); } /** * 获取当年的最后一天 * @param year * @return */ public static Date getCurrYearLast(){ Calendar currCal=Calendar.getInstance(); int currentYear = currCal.get(Calendar.YEAR); return getYearLast(currentYear); } /** * 获取某年第一天日期 * @param year 年份 * @return Date */ public static Date getYearFirst(int year){ Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.YEAR, year); Date currYearFirst = calendar.getTime(); return currYearFirst; } /** * 获取某年最后一天日期 * @param year 年份 * @return Date */ public static Date getYearLast(int year){ Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.YEAR, year); calendar.roll(Calendar.DAY_OF_YEAR, -1); Date currYearLast = calendar.getTime(); return currYearLast; } /** * 获取指定日期前指定前月数日期 * @param dateStr 2019-6-25 * @param number 3 * @return 2019-3-25 * @throws Exception */ public static String getSubMonthDate(String dateStr, Integer number) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat(DATE_YY_MM_DD); Date date = sdf.parse(dateStr); Calendar calendar = Calendar.getInstance(); // 设置为当前时间 calendar.setTime(date); // 设置为上一个月 calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - number); date = calendar.getTime(); return sdf.format(date); } /** * 日期加减获取时间戳 * @param date1 * @param date2 * @param type 0:加;1:减 * @return * @throws Exception */ public static long addAndSubtractDaysByGetTime(Date date1, Date date2, int type) throws Exception { long result=0L; if(type==0) {//日期加 result=date1.getTime()+date2.getTime(); }else {//日期减 result=date1.getTime()-date2.getTime(); } return result; } }
。。。待续
还没有评论,来说两句吧...