java小技巧
String 转 Date
String classCode = RequestHandler.getString(request, "classCode"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); DicClassInfo classInfo=new DicClassInfo(); classInfo.setStartDate(sdf.parse(startDate));
int型除法保留两位小数/求百分比
int openCount = temp.getIsOpenCount(); int passCount = temp.getIsPassCount(); double k = (double)passCount/openCount*100; java.math.BigDecimal big = new java.math.BigDecimal(k); String l =big.setScale(2,java.math.BigDecimal.ROUND_HALF_UP).doubleValue() +"%";
float保留两位小数
//1 float f = 34.232323; BigDecimal b = new BigDecimal(f); float f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue(); // b.setScale(2, BigDecimal.ROUND_HALF_UP) 表明四舍五入,保留两位小数 //2 float scale = 34.236323; DecimalFormat fnum = new DecimalFormat("##0.00"); String dd=fnum.format(scale); System.out.println(dd); //3 float a = 123.2334f; float b = (float)(Math.round(a*100))/100;
BigDecimal保留两位小数
//金额保留小数后两位 BigDecimal b = invoiceTemp.getInvoicePrice(); BigDecimal setScale = b.setScale(2,BigDecimal.ROUND_DOWN); invoiceTemp.setInvoicePrice(setScale);
参数定义
ROUND_CEILING 向正无穷方向舍入
ROUND_DOWN 向零方向舍入
ROUND_FLOOR 向负无穷方向舍入
ROUND_HALF_DOWN 向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,向下舍入, 例如1.55 保留一位小数结果为1.5
ROUND_HALF_EVEN 向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,如果保留位数是奇数,使用ROUND_HALF_UP
,如果是偶数,使用ROUND_HALF_DOWN
ROUND_HALF_UP 向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,向上舍入,
1.55保留一位小数结果为1.6
ROUND_UNNECESSARY 计算结果是精确的,不需要舍入模式
ROUND_UP 向远离0的方向舍入
Action文件mapping.findForward()动态传参至Struts-config.xml
//后台方法 return mapping.findForward(outSystemSign); //配置文件无需判断,直接接收,直接写明映射内容即可 <forward name="qgtzsbcjh" path="/WEB-INF/jsp/outsystem/kpflowchart/qgtzsbcjh.jsp"/> <forward name="gusuwx" path="/WEB-INF/jsp/outsystem/kpflowchart/gusuwx.jsp"/>
isBlank、isEmpty、isNull
org.apache.commons.lang.StringUtils类提供了String的常用操作,最常用判空如下
- StringUtils.isEmpty(String str)
//判断某字符串是否为空,标准是 str==null 或 str.length()==0 System.out.println(StringUtils.isEmpty(null)); //true System.out.println(StringUtils.isEmpty("")); //true System.out.println(StringUtils.isEmpty(" ")); //false System.out.println(StringUtils.isEmpty("dd")); //false
- StringUtils.isNotEmpty(String str) 等价于 !isEmpty(String str),判断是否不为空也不为null,在要替换字段为空串时,应使用isNotNull,isNotEmpty会自动跳过
- StringUtils.isBlank(String str),StringUtils.isNotBlank(String str) 等价于 !isBlank(String str)
//判断某字符串是否为空或长度为0或由空白符(whitespace) 构成System.out.println(StringUtils.isBlank(null)); //true System.out.println(StringUtils.isBlank("")); //true System.out.println(StringUtils.isBlank(" ")); //true System.out.println(StringUtils.isBlank("dd")); //false
时间段控制(00:00:00—23:59:59)
if(null!=startDate){ int len = startDate.length(); switch(len){ case(10):startDate = startDate + " 00:00:00.000";break; case(19):startDate = startDate + ".000";break; default:if(len>10){startDate = startDate.substring(0,10)+"00:00:00.000";}break; } } if(null!=endDate){ int len = endDate.length(); switch(len){ case(10):endDate = endDate + " 23:59:59.999";break; case(19):startDate = startDate + ".999";break; default:if(len>10){startDate = startDate.substring(0,10)+"23:59:59.999";}break; } }
计算时间差
String date = "2010-01-01 11:11:11"; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String nowDate = df.format(new Date()); Date d1 = df.parse(date); Date d2 = df.parse(nowDate); long between = (d2.getTime() - d1.getTime())/1000; //时间差(秒)
获取指定时间
//当前月第一天 Calendar c = Calendar.getInstance(); c.setTime(new Date()); c.set(5, 1); Date startMonthDate =c.getTime(); String startMonthStr = dateToString(startMonthDate , "yyyy-MM-dd 00:00:00"); //当前月最后一天 Calendar calendar = Calendar.getInstance(); calendar.setTime(startMonthDate ); calendar.add(2, n); Date endMonthDate=calendar .getTime(); String endMonthStr = dateToString(endMonthDate, "yyyy-MM-dd 00:00:00"); //七天前 Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 7); Date earlySevenDate = calendar.getTime(); String earlySevenStr = dateToString(earlySevenDate, "yyyy-MM-dd 00:00:00"); //当天 String todayStr = dateToString(new Date(), "yyyy-MM-dd 00:00:00"); //Date date, int n date时间加n天 Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(5, n); Date nDayLaterDate = calendar.getTime(); String earlySevenStr = dateToString(nDayLaterDate, "yyyy-MM-dd 00:00:00"); //Date date, int n date时间加n月 Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(2, n); Date nMonthLaterDate = calendar.getTime(); String earlySevenStr = dateToString(nMonthLaterDate, "yyyy-MM-dd 00:00:00"); public static String dateToString(Date date, String pattern) { if (date != null) { SimpleDateFormat sdf = new SimpleDateFormat(pattern); return sdf.format(date); } else { return ""; } } //Calendar的add()方法:1是对年份操作,2是对月份操作,3是对星期操作,5是对日期操作,11是对小时操作,12是对分钟操作,13是对秒操作,14是对毫秒操作。
String数组、List转换
// List转String数组 List<String> list = new ArrayList<String>(); list.add("a1"); list.add("a2"); String[] toBeStored = list.toArray(new String[list.size()]); for(String s : toBeStored) { System.out.println(s); } //String数组转List String[] arr = new String[] {"1", "2"}; List<String> list = Arrays.asList(arr);
excel导出合并单元格(行)、设置样式
<%@page import="org.apache.poi.hssf.util.Region"%> <%@page import="org.apache.poi.hssf.usermodel.HSSFFont"%> <%@page import="org.apache.poi.hssf.util.HSSFColor"%> HSSFCellStyle cellStyle = wb.createCellStyle();//新样式//居中 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //居中 HSSFFont font=wb.createFont(); font.setColor(HSSFColor.RED.index);//字体颜色 font.setFontHeightInPoints((short)12); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//字体增粗 cellStyle.setFont(font);//把字体应用到当前的样式 cell.setCellStyle(cellStyle); //合并第(0,0)单元格到第(0,13)单元格 sheet.addMergedRegion(new Region(0, (short)0, 0, (short)13)); if(0 != total){ cell.setCellValue("支付金额合计 : "+total+"元"); }
excel动态合并单元格(列)
short num = 1; //列 String s = ""; //sheet.getLastRowNum()获取总行数 for (int k = 1; k <= sheet.getLastRowNum(); k++) { HSSFRow rows = sheet.getRow(k); HSSFCell cells = rows.getCell((short)2); if (cells.getStringCellValue().equals(s)) { sheet.addMergedRegion(new Region(num,(short)2,k,(short)2)); HSSFRow rowsV = sheet.getRow(k); HSSFCell cellsV = rows.getCell((short)2); cellsV.setCellStyle(style); } else { num =(short) k; } s = cells.getStringCellValue(); }