工具类
Posted lyle-liu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了工具类相关的知识,希望对你有一定的参考价值。
常用工具类: 1.POI工具类: import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.web.multipart.MultipartFile; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; public class POIUtils { private final static String xls = "xls"; private final static String xlsx = "xlsx"; private final static String DATE_FORMAT = "yyyy/MM/dd"; /** * 读入excel文件,解析后返回 * @param file * @throws IOException */ public static List<String[]> readExcel(MultipartFile file) throws IOException { //检查文件 checkFile(file); //获得Workbook工作薄对象 Workbook workbook = getWorkBook(file); //创建返回对象,把每行中的值作为一个数组,所有行作为一个集合返回 List<String[]> list = new ArrayList<String[]>(); if(workbook != null){ for(int sheetNum = 0;sheetNum < workbook.getNumberOfSheets();sheetNum++){ //获得当前sheet工作表 Sheet sheet = workbook.getSheetAt(sheetNum); if(sheet == null){ continue; } //获得当前sheet的开始行 int firstRowNum = sheet.getFirstRowNum(); //获得当前sheet的结束行 int lastRowNum = sheet.getLastRowNum(); //循环除了第一行的所有行 for(int rowNum = firstRowNum+1;rowNum <= lastRowNum;rowNum++){ //获得当前行 Row row = sheet.getRow(rowNum); if(row == null){ continue; } //获得当前行的开始列 int firstCellNum = row.getFirstCellNum(); //获得当前行的列数 int lastCellNum = row.getPhysicalNumberOfCells(); String[] cells = new String[row.getPhysicalNumberOfCells()]; //循环当前行 for(int cellNum = firstCellNum; cellNum < lastCellNum;cellNum++){ Cell cell = row.getCell(cellNum); cells[cellNum] = getCellValue(cell); } list.add(cells); } } workbook.close(); } return list; } //校验文件是否合法 public static void checkFile(MultipartFile file) throws IOException{ //判断文件是否存在 if(null == file){ throw new FileNotFoundException("文件不存在!"); } //获得文件名 String fileName = file.getOriginalFilename(); //判断文件是否是excel文件 if(!fileName.endsWith(xls) && !fileName.endsWith(xlsx)){ throw new IOException(fileName + "不是excel文件"); } } public static Workbook getWorkBook(MultipartFile file) { //获得文件名 String fileName = file.getOriginalFilename(); //创建Workbook工作薄对象,表示整个excel Workbook workbook = null; try { //获取excel文件的io流 InputStream is = file.getInputStream(); //根据文件后缀名不同(xls和xlsx)获得不同的Workbook实现类对象 if(fileName.endsWith(xls)){ //2003 workbook = new HSSFWorkbook(is); }else if(fileName.endsWith(xlsx)){ //2007 workbook = new XSSFWorkbook(is); } } catch (IOException e) { e.printStackTrace(); } return workbook; } public static String getCellValue(Cell cell){ String cellValue = ""; if(cell == null){ return cellValue; } //如果当前单元格内容为日期类型,需要特殊处理 String dataFormatString = cell.getCellStyle().getDataFormatString(); if(dataFormatString.equals("m/d/yy")){ cellValue = new SimpleDateFormat(DATE_FORMAT).format(cell.getDateCellValue()); return cellValue; } //把数字当成String来读,避免出现1读成1.0的情况 if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){ cell.setCellType(Cell.CELL_TYPE_STRING); } //判断数据的类型 switch (cell.getCellType()){ case Cell.CELL_TYPE_NUMERIC: //数字 cellValue = String.valueOf(cell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: //字符串 cellValue = String.valueOf(cell.getStringCellValue()); break; case Cell.CELL_TYPE_BOOLEAN: //Boolean cellValue = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_FORMULA: //公式 cellValue = String.valueOf(cell.getCellFormula()); break; case Cell.CELL_TYPE_BLANK: //空值 cellValue = ""; break; case Cell.CELL_TYPE_ERROR: //故障 cellValue = "非法字符"; break; default: cellValue = "未知类型"; break; } return cellValue; } } 2.日期工具类: import java.text.SimpleDateFormat; import java.util.*; /** * 日期操作工具类 */ public class DateUtils { /** * 日期转换- String -> Date * * @param dateString 字符串时间 * @return Date类型信息 * @throws Exception 抛出异常 */ public static Date parseString2Date(String dateString) throws Exception { if (dateString == null) { return null; } return parseString2Date(dateString, "yyyy-MM-dd"); } /** * 日期转换- String -> Date * * @param dateString 字符串时间 * @param pattern 格式模板 * @return Date类型信息 * @throws Exception 抛出异常 */ public static Date parseString2Date(String dateString, String pattern) throws Exception { if (dateString == null) { return null; } SimpleDateFormat sdf = new SimpleDateFormat(pattern); Date date = sdf.parse(dateString); return date; } /** * 日期转换 Date -> String * * @param date Date类型信息 * @return 字符串时间 * @throws Exception 抛出异常 */ public static String parseDate2String(Date date) throws Exception { if (date == null) { return null; } return parseDate2String(date, "yyyy-MM-dd"); } /** * 日期转换 Date -> String * * @param date Date类型信息 * @param pattern 格式模板 * @return 字符串时间 * @throws Exception 抛出异常 */ public static String parseDate2String(Date date, String pattern) throws Exception { if (date == null) { return null; } SimpleDateFormat sdf = new SimpleDateFormat(pattern); String strDate = sdf.format(date); return strDate; } /** * 获取当前日期的本周一是几号 * * @return 本周一的日期 */ public static Date getThisWeekMonday() { Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); // 获得当前日期是一个星期的第几天 int dayWeek = cal.get(Calendar.DAY_OF_WEEK); if (1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); } // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一 cal.setFirstDayOfWeek(Calendar.MONDAY); // 获得当前日期是一个星期的第几天 int day = cal.get(Calendar.DAY_OF_WEEK); // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值 cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day); return cal.getTime(); } /** * 获取当前日期周的最后一天 * * @return 当前日期周的最后一天 */ public static Date getSundayOfThisWeek() { Calendar c = Calendar.getInstance(); int dayOfWeek = c.get(Calendar.DAY_OF_WEEK) - 1; if (dayOfWeek == 0) { dayOfWeek = 7; } c.add(Calendar.DATE, -dayOfWeek + 7); return c.getTime(); } /** * 根据日期区间获取月份列表 * * @param minDate 开始时间 * @param maxDate 结束时间 * @return 月份列表 * @throws Exception */ public static List<String> getMonthBetween(String minDate, String maxDate, String format) throws Exception { ArrayList<String> result = new ArrayList<>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); Calendar min = Calendar.getInstance(); Calendar max = Calendar.getInstance(); min.setTime(sdf.parse(minDate)); min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1); max.setTime(sdf.parse(maxDate)); max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2); SimpleDateFormat sdf2 = new SimpleDateFormat(format); Calendar curr = min; while (curr.before(max)) { result.add(sdf2.format(curr.getTime())); curr.add(Calendar.MONTH, 1); } return result; } /** * 根据日期获取年度中的周索引 * * @param date 日期 * @return 周索引 * @throws Exception */ public static Integer getWeekOfYear(String date) throws Exception { Date useDate = parseString2Date(date); Calendar cal = Calendar.getInstance(); cal.setTime(useDate); return cal.get(Calendar.WEEK_OF_YEAR); } /** * 根据年份获取年中周列表 * * @param year 年分 * @return 周列表 * @throws Exception */ public static Map<Integer, String> getWeeksOfYear(String year) throws Exception { Date useDate = parseString2Date(year, "yyyy"); Calendar cal = Calendar.getInstance(); cal.setTime(useDate); //获取年中周数量 int weeksCount = cal.getWeeksInWeekYear(); Map<Integer, String> mapWeeks = new HashMap<>(55); for (int i = 0; i < weeksCount; i++) { cal.get(Calendar.DAY_OF_YEAR); mapWeeks.put(i + 1, parseDate2String(getFirstDayOfWeek(cal.get(Calendar.YEAR), i))); } return mapWeeks; } /** * 获取某年的第几周的开始日期 * * @param year 年分 * @param week 周索引 * @return 开始日期 * @throws Exception */ public static Date getFirstDayOfWeek(int year, int week) throws Exception { Calendar c = new GregorianCalendar(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, Calendar.JANUARY); c.set(Calendar.DATE, 1); Calendar cal = (GregorianCalendar) c.clone(); cal.add(Calendar.DATE, week * 7); return getFirstDayOfWeek(cal.getTime()); } /** * 获取某年的第几周的结束日期 * * @param year 年份 * @param week 周索引 * @return 结束日期 * @throws Exception */ public static Date getLastDayOfWeek(int year, int week) throws Exception { Calendar c = new GregorianCalendar(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, Calendar.JANUARY); c.set(Calendar.DATE, 1); Calendar cal = (GregorianCalendar) c.clone(); cal.add(Calendar.DATE, week * 7); return getLastDayOfWeek(cal.getTime()); } /** * 获取当前时间所在周的开始日期 * * @param date 当前时间 * @return 开始时间 */ public static Date getFirstDayOfWeek(Date date) { Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.SUNDAY); c.setTime(date); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); return c.getTime(); } /** * 获取当前时间所在周的结束日期 * * @param date 当前时间 * @return 结束日期 */ public static Date getLastDayOfWeek(Date date) { Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.SUNDAY); c.setTime(date); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); return c.getTime(); } //获得上周一的日期 public static Date geLastWeekMonday(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(getThisWeekMonday(date)); cal.add(Calendar.DATE, -7); return cal.getTime(); } //获得本周一的日期 public static Date getThisWeekMonday(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); // 获得当前日期是一个星期的第几天 int dayWeek = cal.get(Calendar.DAY_OF_WEEK); if (1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); } // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一 cal.setFirstDayOfWeek(Calendar.MONDAY); // 获得当前日期是一个星期的第几天 int day = cal.get(Calendar.DAY_OF_WEEK); // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值 cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day); return cal.getTime(); } //获得下周一的日期 public static Date getNextWeekMonday(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(getThisWeekMonday(date)); cal.add(Calendar.DATE, 7); return cal.getTime(); } //获得今天日期 public static Date getToday(){ return new Date(); } //获得本月一日的日期 public static Date getFirstDay4ThisMonth(){ Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH,1); return calendar.getTime(); } //获得本月最后一日的日期 public static Date getLastDay4ThisMonth(){ Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH, 1); calendar.set(Calendar.DAY_OF_MONTH, 0); return calendar.getTime(); } public static void main(String[] args) { try { System.out.println("本周一" + parseDate2String(getThisWeekMonday())); System.out.println("本月一日" + parseDate2String(getFirstDay4ThisMonth())); } catch (Exception e) { e.printStackTrace(); } } } 3.Jedis工具类: import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * ToDo * * @author Lyle * @date 2019/12/25 */ public class JedisUtils { //创建全局变量以便静态代码块中可以使用 private static JedisPoolConfig jedisPoolConfig; private static JedisPool jedisPool; static { //因为JedisPool只需要创建和配置一次,所以放入静态代码块比较好 jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(10); //最多连接数量 jedisPoolConfig.setMaxWaitMillis(1000); //等待时间 // 2. 创建JedisPool对象 String host = "127.0.0.1"; int port = 6379; jedisPool = new JedisPool(jedisPoolConfig, host, port); } //对外提供get方法获得jedis,静态方法,类名调用比较好 public static Jedis getJedis(){ // 3. 从JedisPool获得jedis Jedis jedis = jedisPool.getResource(); return jedis; } //释放资源 public static void close(Jedis jedis){ if(jedis != null){ jedis.close(); } } }
以上是关于工具类的主要内容,如果未能解决你的问题,请参考以下文章