JavaSE-32 工具类及其他
日期转换
package com.etc.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** 日期转换工具类 */ public class DateConvert { /** 日期对象转Long类型时间戳 */ public static Long date2Long(Date date) { return date.getTime(); } /** 时间戳转Date日期类型 */ public static Date long2Date(Long timeStamp) { return new Date(timeStamp); } /** 日期转字符串 */ public static String date2String(Date date, String pattern) { SimpleDateFormat sdf = new SimpleDateFormat(pattern); return sdf.format(date); } /** 字符串转日期 */ public static Date string2Date(String dateStr, String pattern) { // String dateStr="2018-3-15 14:26:00"; // String pattern="yyyy-MM-dd HH:mm:ss"; SimpleDateFormat sdf = new SimpleDateFormat(pattern); Date date = null; try { date = sdf.parse(dateStr); } catch (ParseException e) { e.printStackTrace(); } return date; } /** Long时间戳转日期字符串 */ public static String long2String(Long timeStamp) { Date date = new Date(timeStamp); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(date); } /** 日期字符串转Long时间戳 */ public static Long string2Long(String dateStr) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = null; try { date = sdf.parse(dateStr); } catch (ParseException e) { e.printStackTrace(); } return date.getTime(); } }
翻页类
package com.etc.util; import java.util.List; /**分页工具类*/ public class Page<T> { // 总页数 private int totalPageCount = 1; // 页面大小,即每页显示记录数 private int pageSize = 0; // 记录总数 private int totalCount = 0; // 当前页号 private int currPageNo = 1; // 每页数据集合 private List<T> list; public List<T> getList() { return list; } public void setList(List<T> list) { this.list = list; } public int getCurrPageNo() { if (totalPageCount == 0) return 0; return currPageNo; } public void setCurrPageNo(int currPageNo) { if (this.currPageNo > 0) this.currPageNo = currPageNo; } public int getTotalPageCount() { return totalPageCount; } public void setTotalPageCount(int totalPageCount) { this.totalPageCount = totalPageCount; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { if (pageSize > 0) this.pageSize = pageSize; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { if (totalCount > 0) { this.totalCount = totalCount; // 计算总页数 totalPageCount = this.totalCount % pageSize == 0 ? (this.totalCount / pageSize) : this.totalCount / pageSize + 1; } } }
读取配置文件
文件位置:src\database.properties
package com.etc.util; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** 读取配置文件工具类 */ public class ConfigManager { private static ConfigManager configManager; private static Properties properties; // 构造工具类时,读取配置文件 private ConfigManager() { String configFile = "database.properties"; properties=new Properties(); InputStream in=ConfigManager.class.getClassLoader().getResourceAsStream(configFile); try { properties.load(in); in.close(); } catch (IOException e) { e.printStackTrace(); } } //单例:获取实例 public static ConfigManager getInstance(){ if(configManager==null){ configManager=new ConfigManager(); } return configManager; } //通过key获取value public String getString(String key){ return properties.getProperty(key); } }
本博客文章未经许可,禁止转载和商业用途!
如有疑问,请联系: 2083967667@qq.com