Java 工具类:常用方法集合
/**
- @param contractPrice
- @param changedPrice
- @return */ public static boolean isChangePriceContract(BigDecimal contractPrice, BigDecimal changedPrice){ BigDecimal minPrice = contractPrice.multiply(new BigDecimal(0.9)); BigDecimal maxPrice = contractPrice.multiply(new BigDecimal(1.1)); if(changedPrice.compareTo(minPrice) >= 0 && changedPrice.compareTo(maxPrice) <= 0){ return true; } return false; }
/**
- 获取报名费
- @param fee
- @return */ public static BigDecimal getFee(String fee){ BigDecimal feeBigDecimal = BigDecimal.ZERO; if(StringUtils.isNotBlank(fee)){ String[] feeStrings = fee.split(';'); for(String feeString : feeStrings){ String[] feeStr = feeString.split(':'); if(feeStr.length == 2){ feeBigDecimal = feeBigDecimal.add(new BigDecimal(feeStr[1])); } } } return feeBigDecimal; }
/**
- 计算报名费
- @param fee
- @param times
- @return */ public static BigDecimal getFee(String fee, int times){ BigDecimal feeBigDecimal = BigDecimal.ZERO; if(StringUtils.isNotBlank(fee)){ String[] feeStrings = fee.split(';'); for(String feeString : feeStrings){ String[] feeStr = feeString.split(':'); if(feeStr.length == 2){ feeBigDecimal = feeBigDecimal.add(new BigDecimal(feeStr[1]).multiply(new BigDecimal(times))); } } } return feeBigDecimal; }
/**
- 对字符串进行base64编码
- @param str
- @return */ public static String getBase64(String str){ if(StringUtils.isBlank(str)){ return ''; } return Base64.encodeBase64String(str.getBytes()); }
/**
- 对字符串进行base64解码
- @param str
- @return
- @throws UnsupportedEncodingException */ public static String getFromBase64(String str) throws UnsupportedEncodingException{ if(StringUtils.isBlank(str)){ return ''; } return new String(Base64.decodeBase64(str),'utf-8'); }
/**
- 获取输入流
- @param file
- @return */ public static InputStream getInputStream(File file){ if(null == file){ return null; } try { return new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } return null; }
public static String getUUID(){ return UUID.randomUUID().toString().replaceAll('-', ''); }
/**
- 获取文件夹的绝对路径
- @param path
- @return */ public static String getRealPath(String path){ String realPath = null; try { realPath = Constants.class.getClassLoader().getResource('').toURI().getPath() + path; } catch (URISyntaxException e) { e.printStackTrace(); } return realPath; }
/**
- 生成图片的名称
- @param ext
- @return */ public static String getImageName(String ext){ return UUID.randomUUID().toString().replaceAll('-', '') + '.' + ext; }
/**
- 计算文件的MD5
- @param file
- @return
- @throws IOException */ public static String getMD5(File file) throws IOException{ if(null == file){ return null; } FileInputStream fis = new FileInputStream(file); return DigestUtils.md5Hex(IOUtils.toByteArray(fis)); }
/**
- 计算文件的SHA1
- @param file
- @return
- @throws IOException */ public static String getSHA1(File file) throws IOException{ if(null == file){ return null; } FileInputStream fis = new FileInputStream(file); return DigestUtils.sha1Hex(IOUtils.toByteArray(fis)); }
/**
- 生成二维码
- @param content
- @param file
- @return
- @throws IOException */ public static boolean createQrCode(String content, File file) throws IOException{ if(StringUtils.isBlank(content) || null == file){ return false; } int width = 300; int height = 300; // 二维码的图片格式 String format = 'gif'; Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); // 内容所使用编码 hints.put(EncodeHintType.CHARACTER_SET, 'utf-8'); BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); MatrixToImageWriter.writeToFile(bitMatrix, format, file); return true; }
/**
- 根据身份证号获取性别
- @param idCard
- @return */ public static String getGenderByIdCard(String idCard){ if(StringUtils.isBlank(idCard)){ return ''; } String gender = ''; String check = idCard.substring(idCard.length() - 2, idCard.length() - 1); if(Integer.parseInt(check) % 2 == 0){ gender = '2'; }else{ gender = '1'; } return gender; }
/**
- 根据身份证号获取出生日期
- @param idCard
- @return */ public static String getBirthdayByIdCard(String idCard){ if(StringUtils.isBlank(idCard)){ return ''; } String birthday = ''; if(idCard.length() == 15){ birthday = '19' + idCard.substring(6, 12); }else if(idCard.length() == 18){ birthday = idCard.substring(6, 14); } return birthday; }
/**
- 根据身份证号获取年龄
- @param idCard
- @return */ public static int getAgeByIdCard(String idCard){ if(StringUtils.isBlank(idCard))
原文地址: https://www.cveoy.top/t/topic/lgkI 著作权归作者所有。请勿转载和采集!