java 8 时间常用 工具类

/**
 * 获取 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();
}

转载于:https://my.oschina.net/u/3247419/blog/1857292

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值