/** * 获取 time n周以前的周一 * [@param](https://my.oschina.net/u/2303379) time 时间 * [@param](https://my.oschina.net/u/2303379) minus * [@return](https://my.oschina.net/u/556800) */ public static long getMinusMonDayOfWeek(Long time, int minus) { return Instant.ofEpochMilli(time).atZone(ZoneId.systemDefault()).toLocalDate().minusWeeks(minus) .with(DayOfWeek.MONDAY).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); } /** * 获取前几天 * [@param](https://my.oschina.net/u/2303379) time * [@param](https://my.oschina.net/u/2303379) minus * @return */ public static long getMinusDay(Long time, int minus) { return Instant.ofEpochMilli(time).atZone(ZoneId.systemDefault()).toLocalDate().minusDays(minus).atStartOfDay() .atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); } /** * 获取前几个月的 第一天 * @param time * @param minus * @return */ public static long getMinusFirstDayOfMonth(Long time, int minus) { return Instant.ofEpochMilli(time).atZone(ZoneId.systemDefault()).toLocalDate().minusMonths(minus) .with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant() .toEpochMilli(); } /** * 获取前几个月的 最后一天 * @param time * @param minus * @return */ public static long getMinusLastDayOfMonth(Long time, int minus) { return Instant.ofEpochMilli(time).atZone(ZoneId.systemDefault()).toLocalDate().minusMonths(minus) .with(TemporalAdjusters.lastDayOfMonth()).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant() .toEpochMilli(); } /** * 时间戳转换为字符串,格式化 * @param time * @return */ public static String longToString(Long time){ DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); return Instant.ofEpochMilli(time).atZone(ZoneId.systemDefault()).format(dateTimeFormatter); } /** * 获取今日凌晨的时间戳 * @return */ public static long getStartOfDay(){ return Instant.now().atZone(ZoneId.systemDefault()).toLocalDate().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); } /** * 两个时间直接的月数 * @param start * @param end * @return */ public static long getPeriodMonth(Long start,Long end){ System.out.println(start); System.out.println(end); LocalDate startDate = Instant.ofEpochMilli(start).atZone(ZoneId.systemDefault()).toLocalDate(); System.out.println(startDate); LocalDate endDate = Instant.ofEpochMilli(end).atZone(ZoneId.systemDefault()).toLocalDate(); System.out.println(endDate); return ChronoUnit.MONTHS.between(startDate,endDate); } /** * 获取时间戳,所在月份 * @param time * @return */ public static int getMonthValue(Long time){ return Instant.ofEpochMilli(time).atZone(ZoneId.systemDefault()).getMonthValue(); } /** * 获取时间戳所在的周数 如:2017年11月1日 是 2017年的 第44周 * @param time * @return */ public static int getWeekOfYear(Long time){ return Instant.ofEpochMilli(time).atZone(ZoneId.systemDefault()).toLocalDate().get(ChronoField.ALIGNED_WEEK_OF_YEAR); } /** * 获取时间戳所在月数 * @param time * @return */ public static int getMonthOfYear(Long time){ return Instant.ofEpochMilli(time).atZone(ZoneId.systemDefault()).toLocalDate().getMonthValue(); } /** * 今日是本周的第几天 * @param time * @return */ public static int getDayOfWeekr(Long time){ return Instant.ofEpochMilli(time).atZone(ZoneId.systemDefault()).toLocalDate().getDayOfWeek().getValue(); }