java8获取今日,本周,本月,本季度,本年的开始时间,结束时间

8 篇文章 0 订阅
 /**
     * 获取本周的第一天或最后一天
     * 
     * @param : [today, isFirst: true 表示开始时间,false表示结束时间]
     * @return
     */
    public static String getStartOrEndDayOfWeek(LocalDate today, Boolean isFirst) {
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        DayOfWeek week = today.getDayOfWeek();
        int value = week.getValue();
        if (isFirst) {
            resDate = today.minusDays(value - 1);
        } else {
            resDate = today.plusDays(7 - value);
        }
        return resDate.toString();
    }
/**
 * 获取本月的第一天或最后一天
 *
 * @Param: [today, isFirst: true 表示开始时间,false表示结束时间]
 * @return
 */
public static String getStartOrEndDayOfMonth(LocalDate today, Boolean isFirst) {
    LocalDate resDate = LocalDate.now();
    if (today == null) {
        today = resDate;
    }
    Month month = today.getMonth();
    int length = month.length(today.isLeapYear());
    if (isFirst) {
        resDate = LocalDate.of(today.getYear(), month, 1);
    } else {
        resDate = LocalDate.of(today.getYear(), month, length);
    }
    return resDate.toString();
}
/**
 * 获取本季度的第一天或最后一天
 * 
 * @param : [today, isFirst: true 表示开始时间,false表示结束时间]
 * @return
 */
public static String getStartOrEndDayOfQuarter(LocalDate today, Boolean isFirst) {
    LocalDate resDate = LocalDate.now();
    if (today == null) {
        today = resDate;
    }
    Month month = today.getMonth();
    Month firstMonthOfQuarter = month.firstMonthOfQuarter();
    Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
    if (isFirst) {
        resDate = LocalDate.of(today.getYear(), firstMonthOfQuarter, 1);
    } else {
        resDate = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear()));
    }
    return resDate.toString();
}
  /**
     * 获取本年的第一天或最后一天
     * 
     * @param : [today, isFirst: true 表示开始时间,false表示结束时间]
     * @return
     */
    public static String getStartOrEndDayOfYear(LocalDate today, Boolean isFirst) {
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        if (isFirst) {
            resDate = LocalDate.of(today.getYear(), Month.JANUARY, 1);
        } else {
            resDate = LocalDate.of(today.getYear(), Month.DECEMBER, Month.DECEMBER.length(today.isLeapYear()));
        }
        return resDate.toString();
    }
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值