所有工具类
字符串转日期,日期转字符串,当前日期的月份,当前日期的年份,当前日期月份枚举信息,一天的开始和结束时间,获取一天,两天,三天以后的日期,获取上周 ,下周,上个月,下个月的日期,毫秒数时间戳,精确到秒,分,时,基于生日获取年龄,基于年份获取是否闰年等等。。。。
测试
@Test public void 基本工具类() { TestUtil.printStart("测试日期工具类"); System.out.println("日期转字符串"); System.out.println(DateUtil.dateParse(new Date(), "yyyy年MM月dd日HH时mm分ss秒SSS毫秒p(0-6=>周日-周六)w x q季度")); System.out.println("字符串转日期"); System.out.println(DateUtil.parseDate("2019-07-30")); System.out.println(DateUtil.parseDate("2019-07-30 10:11:12")); System.out.println(DateUtil.parseDate("20190730")); System.out.println(DateUtil.parseDate("20190730101112")); System.out.println(DateUtil.parseDate("2019/07/30 10:11:12")); System.out.println(DateUtil.parseDate("2019-07-30 10:11")); System.out.println(DateUtil.parseDate("2019/07/30 10:11")); System.out.println(DateUtil.parseDate("2019/07/30 10")); System.out.println(DateUtil.parseDate("2019-07-30 10")); System.out.println("日期相差天数"); System.out.println("2019-02-25到2019-03-10共" + DateUtil.diffDay(DateUtil.parseDate("2019-02-25"), DateUtil.parseDate("2019-03-10")) + "天"); System.out.println("2019-02-25加10天是" + DateUtil.dateParse(DateUtil.addDay(DateUtil.parseDate("2019-02-25"), 10), "yyyy-MM-dd")); System.out.println("对日期或时间相加减就不多做测试了"); TestUtil.printEnd("测试日期工具类"); System.out.println(DateUtil.dateParse(new Date(),"yyy-MM-ddw")); System.out.println(DateUtil.getDescWeekday(DateUtil.dateParse(new Date(),"w"))); }
输出结果
测试日期工具类------>测试开始 日期转字符串 2020年03月22日16时10分08秒064毫秒下午(0-6=>周日-周六)7 星期日 1季度 字符串转日期 Tue Jul 30 00:00:00 CST 2019 Tue Jul 30 10:11:12 CST 2019 Tue Jul 30 00:00:00 CST 2019 Tue Jul 30 10:11:12 CST 2019 Tue Jul 30 10:11:12 CST 2019 Tue Jul 30 10:11:00 CST 2019 Tue Jul 30 10:11:00 CST 2019 Tue Jul 30 10:00:00 CST 2019 Tue Jul 30 10:00:00 CST 2019 日期相差天数 2019-02-25到2019-03-10共13天 2019-02-25加10天是2019-03-07 对日期或时间相加减就不多做测试了 测试日期工具类------>测试结束 20y-03-227 星期日
工具类
package zj.date.util; import java.io.File; import java.io.Serializable; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.log4j.Logger; import zj.cache.util.EhCacheUtil; import zj.common.exception.ServiceException; import zj.date.bean.TradeDate; import zj.io.util.FileUtil; import zj.java.util.JavaUtil; import zj.reflect.util.TypeUtil; /** * * 日期工具类 * <p> * 一般,在项目中,我们会会经常使用到日期的各种方式的处理,在各个业务逻辑操作中,都需要相关的日期操作, 因此,实现项目中的日期工具类的提出,还是十分重要的,下面,就项目中常用到的日期的相关操作方式, 做了一个小的工具类,几乎包含所有的日期操作了,方便项目使用,也方便以后的复用和查询.下面见代码: * </p> * * @version 1.00 (2014.09.15) * @author SHNKCS 张军 {@link <a target=_blank href="http://www.zhangjunbk.com">张军个人网站</a> <a target=_blank href="http://user.qzone.qq.com/360901061/">张军QQ空间</a>} * */ public class DateUtil implements Serializable { private static final long serialVersionUID = 1L; private transient static final Logger log = Logger.getLogger(DateUtil.class); public static final String EHCACHE_HOLIDAY_DATE = "holidayDate"; // =====================================================交易日====================================== // DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA); // String value = "'" + formatter.format(new Date()) + "'"; // System.out.println(value); // /** // * 初使化节假日数据 // * // * @param conn // * 数据库连接 // * @return // */ // public static void cacheHoliday(Connection conn) { // Map<String, String> holidayMap = new HashMap<String, String>(); // PreparedStatement psQuery = null; // ResultSet rs = null; // try { // String sql = "select date_format(a.c_date,'%Y%m%d') c_date,a.c_desc from dc_trade_date a where a.is_trade_date=2"; // psQuery = conn.prepareStatement(sql); // rs = psQuery.executeQuery(); // while (rs.next()) { // holidayMap.put(rs.getString(1), rs.getString(2)); // } // EhCacheUtil.put(Constant.EHCACHE_HOLIDAY_DATE, holidayMap); // logger.info("缓存节假日数据成功:" + holidayMap.size()); // } catch (Exception e) { // throw new ServiceException(e); // } finally { // JdbcUtil.closeDB(rs, psQuery); // } // } /** * 取交易日[不包含当前日期] * * @param td * 参数信息 * @return */ public static final Date tradeDate(TradeDate td) { Map<String, String> holidayMap = EhCacheUtil.get(EHCACHE_HOLIDAY_DATE); if (holidayMap == null || holidayMap.size() == 0) { throw new ServiceException("未初使化交易日文件,请执行DateUtil.initTradeDate(file)"); } /** 当前日期 **/ Date current = td.current; /** 递归次数 **/ int recursionCount = td.recursionCount; /** 当前日期前后,默认前 **/ boolean beforeCurrent = td.beforeCurrent; /** 交易日前后,默认前 **/ boolean beforeTradeDate = td.beforeTradeDate; /** 类型[默认0:天,1:周,2:月,3:季度,4:年] **/ int type = td.type; // 首先取上一个日期 if (type == 3) { // 加减3个月 current = DateUtil.addMonth(current, beforeCurrent ? -3 : 3); } else { // 加减1天 current = DateUtil.addDay(current, beforeCurrent ? -1 : 1); } int addDay = beforeTradeDate ? -1 : 1; // 加一天 while (holidayMap.containsKey(DateUtil.dateParse(current, "yyyyMMdd"))) { // 是否是节假日 current = DateUtil.addDay(current, addDay); } if (recursionCount <= 1) { return current; } else { td.current = current; td.recursionCount = td.recursionCount - 1; return tradeDate(td); } } /** * 是否是交易日 * * @param current * 日期 * @return */ public static final boolean isTradeDate(Date current) { Map<String, String> holidayMap = EhCacheUtil.get(EHCACHE_HOLIDAY_DATE); if (holidayMap.containsKey(DateUtil.dateParse(current, "yyyyMMdd"))) { return false; } return true; } /** * 初使化交易日数据 * * @param holidayMap * 交易日数据 */ public static final void initTradeDate(Map<String, String> holidayMap) { EhCacheUtil.put(EHCACHE_HOLIDAY_DATE, holidayMap); } /** * 初使化交易日数据 * * @param tradeDateFilePath * 交易日文件 */ public static final void initTradeDate(String tradeDateFilePath) { try { // 测试股票 // System.out.println(DateUtil.class.getResource("/trade_dates.txt").getPath()); // String file = "/versionManager/sources/java/zj-model/integration/tools/src/main/resources/trade_dates.txt"; Set<String> holidays = new HashSet<String>(); Map<String, String> holidayMap = new HashMap<String, String>(); FileUtil.readLines(holidays, new File(tradeDateFilePath)); for (String v : holidays) { String[] vs = JavaUtil.split(v, "\t"); if (vs.length == 2) { // System.out.println(vs[0] + ":" + vs[1]); holidayMap.put(vs[0], vs[1]); } } EhCacheUtil.put(EHCACHE_HOLIDAY_DATE, holidayMap); } catch (Exception e) { log.error("初使化交易日错误"); } } /** * 取交易日 * * @param current * 日期 * @return */ @Deprecated public static final Date preTradeDate(Date current) { return preTradeDate(current, 1, true); } /** * 取交易日 * * @param current * 日期 * @return */ @Deprecated public static final Date preTradeDate(Date current, int tDay) { return preTradeDate(current, tDay, true); } /** * 取交易日 * * @param current * 日期 * @return */ @Deprecated public static final Date preTradeDate(Date current, int tDay, boolean before) { Map<String, String> holidayMap = EhCacheUtil.get(EHCACHE_HOLIDAY_DATE); if (holidayMap == null || holidayMap.size() == 0) { throw new ServiceException("未初使化交易日文件,请执行DateUtil.initTradeDate(file)"); } // 首先取上一个日期 int addDay = before ? -1 : 1; current = DateUtil.addDay(current, addDay); // 加一天 while (holidayMap.containsKey(DateUtil.dateParse(current, "yyyyMMdd"))) { // 是否是节假日 current = DateUtil.addDay(current, addDay); } if (tDay <= 1) { return current; } else { return preTradeDate(current, tDay - 1, before); } } /** * 获取交易日集合 * * @param sdate * 日期 * @param preDays * 向前取多少天 * @param diffDays * 相差多少天 * @author zhangjun * @return */ public static List<String> getTradeDates(String sdate, int preDays, int diffDays) { List<String> rdates = new ArrayList<String>(); try { Date date = DateUtil.parseDate(sdate); Map<String, String> holidayDate = EhCacheUtil.get(EHCACHE_HOLIDAY_DATE); // 相差天数 diffDays = diffDays <= 0 ? -1 : -(diffDays + 1); // 总交易日 int totalDays = -diffDays * preDays; // 相隔日期 List<String> dates = new ArrayList<String>(); loop: while (true) { while (true) { // 判断是否是节假日 if (!holidayDate.containsKey(DateUtil.dateParse(date, "yyyyMMdd"))) { // 添加交易日 dates.add(DateUtil.dateParse(date, "yyyy-MM-dd")); // 总天数减1 totalDays--; if (totalDays <= 0) { // 循环结束后-1 break loop; } } date = DateUtil.addDay(date, -1); } } // 真实的交易日 for (int i = 0; i < dates.size(); i = i - diffDays) { rdates.add(dates.get(i)); } // logger.info("-------待计算的所有交易日------------\n" + dates.size() + ":" + dates); // logger.info("-------计算相隔后的所有交易日------------\n" + rdates.size() + ":" + rdates); } catch (Exception e) { throw new ServiceException(e); } return rdates; } // =====================================================交易日====================================== /** * 日期类型 * * @author zhangjun * */ public static class DateType implements Serializable { private static final long serialVersionUID = 1L; public static int DATE_TYPE_ALL = 0; public static int DATE_TYPE_y4 = 1; public static int DATE_TYPE_y4_MM = 2; public static int DATE_TYPE_y4_MM_dd = 3; public static int DATE_TYPE_y4_MM_dd_HH = 4; public static int DATE_TYPE_y4_MM_dd_HH_mm = 5; public static int DATE_TYPE_y4_MM_dd_HH_mm_ss = 6; public static int RANGE_TYPE_y = 0; public static int RANGE_TYPE_M = 1; public static int RANGE_TYPE_d = 2; public static int RANGE_TYPE_H = 3; public static int RANGE_TYPE_m = 4; public static int RANGE_TYPE_s = 5; } /** * 获取星期几 * * @param w * 星期标识1-7=>周一-周日 * @return */ public static final String getDescWeekday(int w) { switch (w) { case 1: return "星期一"; case 2: return "星期二"; case 3: return "星期三"; case 4: return "星期四"; case 5: return "星期五"; case 6: return "星期六"; case 7: return "星期日"; default: return ""; } } /** * 获取星期几 * * @param w * 星期标识0-6=>周日-周六 * @return */ public static final String getDescWeekday(String w) { return getDescWeekday(TypeUtil.Primitive.intValue(w)); } /** * 字符串转日期 * * @param dt * 日期字符串 * @return 日期对象 */ public static Date parseDate(String dt) { return parseDate(dt, DateType.DATE_TYPE_ALL); } /** * 字符串转日期 * * @param dt * 日期字符串 * @param dateType * 日期类型 * @see DateType * @return 日期对象 */ public static Date parseDate(String dt, int dateType) { Date rd = null; try { if (dt == null || dt == "") { log.warn("日期不能为:" + dt); } else { boolean isFail = false; String _val_format = "【" + dt + "】不是一个有效的日期格式\n支持日期格式如下:\n"; if (dateType == DateType.DATE_TYPE_y4) { _val_format += "【yyyy】"; } else if (dateType == DateType.DATE_TYPE_y4_MM) { _val_format += "【yyyyMM或yyyy-MM或yyyy/MM或yyyy MM】"; } else if (dateType == DateType.DATE_TYPE_y4_MM_dd) { _val_format += "【yyyyMMdd或yyyy-MM-dd或yyyy/MM/dd或yyyy MM dd】"; } else if (dateType == DateType.DATE_TYPE_y4_MM_dd_HH) { _val_format += "【yyyyMMddhh或yyyy-MM-dd hh或yyyy/MM/dd hh或yyyy MM dd hh】"; } else if (dateType == DateType.DATE_TYPE_y4_MM_dd_HH_mm) { _val_format += "【yyyyMMddhhmm或yyyy-MM-dd hh:mm或yyyy/MM/dd hh:mm或yyyy MM dd hh:mm】"; } else if (dateType == DateType.DATE_TYPE_y4_MM_dd_HH_mm_ss) { _val_format += "【yyyyMMddhhmmss或yyyy-MM-dd hh:mm:ss或yyyy/MM/dd hh:mm:ss或yyyy MM dd hh:mm:ss】"; } else { dateType = DateType.DATE_TYPE_ALL; _val_format += "【yyyy】"; _val_format += "【yyyyMM或yyyy-MM或yyyy/MM或yyyy MM】"; _val_format += "【yyyyMMdd或yyyy-MM-dd或yyyy/MM/dd或yyyy MM dd】"; _val_format += "【yyyyMMddhh或yyyy-MM-dd hh或yyyy/MM/dd hh或yyyy MM dd hh】"; _val_format += "【yyyyMMddhhmm或yyyy-MM-dd hh:mm或yyyy/MM/dd hh:mm或yyyy MM dd hh:mm】"; _val_format += "【yyyyMMddhhmmss或yyyy-MM-dd hh:mm:ss或yyyy/MM/dd hh:mm:ss或yyyy MM dd hh:mm:ss】"; } String datePat = ""; datePat = "^(\\d{4})(/|-| )(\\d{1,2})(\\2)([0-9]{1,2}) (\\d{1,2}):(\\d{1,2}):(\\d{1,2})$"; Matcher mt = Pattern.compile(datePat).matcher(dt); String year = ""; String month = ""; String day = ""; String hour = ""; String minute = ""; String second = ""; if (mt.find()) { if (dateType == DateType.DATE_TYPE_y4_MM_dd_HH_mm_ss || dateType == DateType.DATE_TYPE_ALL) { year = mt.group(1); month = mt.group(3); day = mt.group(5); hour = mt.group(6); minute = mt.group(7); second = mt.group(8); } else { isFail = true; } } else { datePat = "^(\\d{14})$"; mt = Pattern.compile(datePat).matcher(dt); if (mt.find()) { if (dateType == DateType.DATE_TYPE_y4_MM_dd_HH_mm_ss || dateType == DateType.DATE_TYPE_ALL) { year = dt.substring(0, 4); month = dt.substring(4, 6); day = dt.substring(6, 8); hour = dt.substring(8, 10); minute = dt.substring(10, 12); second = dt.substring(12, 14); } else { isFail = true; } } else { datePat = "^(\\d{4})(/|-| )(\\d{1,2})(\\2)(\\d{1,2}) (\\d{1,2}):(\\d{1,2})$"; mt = Pattern.compile(datePat).matcher(dt); if (mt.find()) { if (dateType == DateType.DATE_TYPE_y4_MM_dd_HH_mm || dateType == DateType.DATE_TYPE_ALL) { year = mt.group(1); month = mt.group(3); day = mt.group(5); hour = mt.group(6); minute = mt.group(7); second = "0"; } else { isFail = true; } } else { datePat = "^(\\d{12})$"; mt = Pattern.compile(datePat).matcher(dt); if (mt.find()) { if (dateType == DateType.DATE_TYPE_y4_MM_dd_HH_mm || dateType == DateType.DATE_TYPE_ALL) { year = dt.substring(0, 4); month = dt.substring(4, 6); day = dt.substring(6, 8); hour = dt.substring(8, 10); minute = dt.substring(10, 12); second = "0"; } else { isFail = true; } } else { datePat = "^(\\d{4})(/|-| )(\\d{1,2})(\\2)(\\d{1,2}) (\\d{1,2})$"; mt = Pattern.compile(datePat).matcher(dt); if (mt.find()) { if (dateType == DateType.DATE_TYPE_y4_MM_dd_HH || dateType == DateType.DATE_TYPE_ALL) { year = mt.group(1); month = mt.group(3); day = mt.group(5); hour = mt.group(6); minute = "0"; second = "0"; } else { isFail = true; } } else { datePat = "^(\\d{10})$"; mt = Pattern.compile(datePat).matcher(dt); if (mt.find()) { if (dateType == DateType.DATE_TYPE_y4_MM_dd_HH || dateType == DateType.DATE_TYPE_ALL) { year = dt.substring(0, 4); month = dt.substring(4, 6); day = dt.substring(6, 8); hour = dt.substring(8, 10); minute = "0"; second = "0"; } else { isFail = true; } } else { datePat = "^(\\d{4})(/|-| )(\\d{1,2})(\\2)(\\d{1,2})$"; mt = Pattern.compile(datePat).matcher(dt); if (mt.find()) { if (dateType == DateType.DATE_TYPE_y4_MM_dd || dateType == DateType.DATE_TYPE_ALL) { year = mt.group(1); month = mt.group(3); day = mt.group(5); hour = "0"; minute = "0"; second = "0"; } else { isFail = true; } } else { datePat = "^(\\d{8})$"; mt = Pattern.compile(datePat).matcher(dt); if (mt.find()) { if (dateType == DateType.DATE_TYPE_y4_MM_dd || dateType == DateType.DATE_TYPE_ALL) { year = dt.substring(0, 4); month = dt.substring(4, 6); day = dt.substring(6, 8); hour = "0"; minute = "0"; second = "0"; } else { isFail = true; } } else { datePat = "^(\\d{4})(/|-| )(\\d{1,2})$"; mt = Pattern.compile(datePat).matcher(dt); if (mt.find()) { if (dateType == DateType.DATE_TYPE_y4_MM || dateType == DateType.DATE_TYPE_ALL) { year = mt.group(1); month = mt.group(3); day = "1"; hour = "0"; minute = "0"; second = "0"; } else { isFail = true; } } else { datePat = "^(\\d{6})$"; mt = Pattern.compile(datePat).matcher(dt); if (mt.find()) { if (dateType == DateType.DATE_TYPE_y4_MM || dateType == DateType.DATE_TYPE_ALL) { year = dt.substring(0, 4); month = dt.substring(4, 6); day = "1"; hour = "0"; minute = "0"; second = "0"; } else { isFail = true; } } else { datePat = "^(\\d{4})$"; mt = Pattern.compile(datePat).matcher(dt); if (mt.find()) { if (dateType == DateType.DATE_TYPE_y4 || dateType == DateType.DATE_TYPE_ALL) { year = mt.group(1); month = "1"; day = "1"; hour = "0"; minute = "0"; second = "0"; } else { isFail = true; } } else { _val_format = "【" + dt + "】不是一个有效的日期格式"; isFail = true; } } } } } } } } } } } if (isFail) { log.warn(_val_format); } else { int iyear = Integer.parseInt(year); int imonth = Integer.parseInt(month); int iday = Integer.parseInt(day); int ihour = Integer.parseInt(hour); int iminute = Integer.parseInt(minute); int isecond = Integer.parseInt(second); if (iyear < 1 || iyear > 9999) { _val_format = "年份必须在0001-9999之间"; isFail = true; } if (imonth < 1 || imonth > 12) { _val_format = "月份必须在01-12之间"; isFail = true; } if (iday < 1 || iday > 31) { _val_format = "天数必须在01-31之间"; isFail = true; } if ((imonth == 4 || imonth == 6 || imonth == 9 || imonth == 11) && iday == 31) { _val_format = month + "月不能有31天!"; isFail = true; } if (imonth == 2) { boolean isleap = (iyear % 4 == 0 && iyear % 100 != 0) || iyear % 400 == 0; if (isleap) { if (iday > 29) { _val_format = "闰年[" + year + "]年的2月不能有[" + day + "]天!"; isFail = true; } } else { if (iday > 28) { _val_format = "平年[" + year + "]年的2月不能有[" + day + "]天!"; isFail = true; } } } if (ihour < 0 || ihour > 23) { _val_format = "小时必须在00-23之间"; isFail = true; } if (iminute < 0 || iminute > 59) { _val_format = "分钟必须在00-59之间"; isFail = true; } if (isecond < 0 || isecond > 59) { _val_format = "秒钟必须在00-59之间"; isFail = true; } if (isFail) { log.warn(_val_format); } else { // //("调试字符串如下【开始】\n" + sb.toString() + "\n调试字符串如下【结束】"); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(0); calendar.set(iyear, imonth - 1, iday, ihour, iminute, isecond); rd = calendar.getTime(); } } } } catch (Exception e) { rd = null; log.error(e.getMessage()); } return rd; } /** * 日期解析字符串 * * @param odt * 日期对象 * @return 返回格式化后的日期字符串,如果出错返回当前日期对象 */ public static String dateParse(Object odt) { return dateParse(odt, null); } /** * 日期解析字符串 * * @param odt * 日期对象 * @param partten * 日期格式 * <p> * yyyy年MM月dd日HH时mm分ss秒SSS毫秒p(1-7=>周一-周日)w x q季度 * </p> * @return 返回格式化后的日期字符串,如果出错返回当前日期对象 */ public static String dateParse(Object odt, String partten) { try { if (odt == null) return ""; Date dt = null; if (odt instanceof Date) { dt = (Date) odt; } else if (odt instanceof Timestamp) { Timestamp ts = (Timestamp) odt; dt = new Date(ts.getTime()); } else if (odt instanceof Calendar) { Calendar cal = (Calendar) odt; dt = cal.getTime(); } else if (odt instanceof String) { dt = parseDate(String.valueOf(odt)); if (dt == null) { log.error("无效的日期:" + odt); return ""; } } else { log.error("无效的日期:" + odt); return String.valueOf(odt); } return getParseDt(dt, partten); } catch (Exception e) { log.error(e.getMessage()); return String.valueOf(odt); } }
如查看完整内容,请 登录
本文为张军原创文章,转载无需和我联系,但请注明来自张军的军军小站,个人博客http://www.zhangjunbk.com