1.
Date d1 = new Date(); logger.info("1<{}>", d1.getTime()); // <1552463576744> logger.info("2<{}>", new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(d1.getTime())); // <2019/03/13 15:52:56> logger.info("3<{}>", new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(d1)); // <2019/03/13 15:52:56> Date d2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("1970/01/01 08:00:00"); logger.info("4<{}>", d2.getTime()); // 结果 0 logger.info("5<{}>", new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(d2.getTime())); // <1970/01/01 08:00:00> Date d3 = new SimpleDateFormat("yyyy/MM/dd").parse("2019/3/10"); d3 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2019/3/10 22:36:01"); logger.info("6<{}>", d3); // <Sun Mar 10 22:36:01 CST 2019> logger.info("d3 = <{}>", d3.getTime()); // <1552228561000> logger.info("d3= <{}>", new SimpleDateFormat("yyyy/MM/dd").format(d3.getTime())); // <2019/03/10> logger.info("7<{}>", DateUtil.getPatternDate("yyyy/MM/dd HH:mm:ss", 1552012596964L)); // <2019/03/08 10:36:36> logger.info("8<{}>", DateUtil.getPatternDate("yyyy/MM/dd HH:mm:ss", 1551966731871L)); // <2019/03/07 21:52:11>
2.
// 用"2018-11-28"算出"2018-11-21" String d = "2018-11-28"; String pattern1 = "yyyy-MM-dd"; String pattern2 = "yyyy-MM-dd HH:mm:ss"; SimpleDateFormat sdf1 = new SimpleDateFormat(pattern1); SimpleDateFormat sdf2 = new SimpleDateFormat(pattern2); int offset = -7; long time = DateUtil.parseDateAddDaysToLongStrict(d, pattern1, offset); // 自定义工具 DateUtil
String day = DateUtil.getPatternDate(pattern1, time);
logger.info("day:<{}>", day);
3.
// 用今天时间,算出向前7天的日期,方法二
Calendar calendar = Calendar.getInstance(); logger.info("taday短格式:<{}>", sdf1.format(calendar.getTime())); // 短格式 logger.info("taday长格式:<{}>", sdf2.format(calendar.getTime())); // 长格式 calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + offset); Date tod = calendar.getTime(); String t = sdf1.format(tod); logger.info("7天前的这一天:<{}>", t);
4.
// 用Calendar根据时区获得当天00:00:00的时间戳 logger.info("当前时间戳:<{}>", System.currentTimeMillis()); // 当前时间的时间戳 TimeZone curTimeZone = TimeZone.getTimeZone("GMT+8"); // 东8区 Calendar cal = Calendar.getInstance(curTimeZone); // 当前时间, 精确到秒 long now = cal.getTimeInMillis(); logger.info("当前时间戳:<{}>", now); // 当前时间的时间戳 calendar.setTimeInMillis(now); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); long dayBeginning = calendar.getTimeInMillis(); logger.info("当天开始时间戳:<{}>", dayBeginning); // 今天00:00:00的时间戳 long dayEnd = dayBeginning + 86400000; logger.info("当天结束时间戳:<{}>", dayEnd); // 第二天00:00:00的时间戳
e.