• java8 LocalDateTime/LocalDate 获取各种日期类型


    获取当前时间:

    LocalDateTime.now()

    获取当天:

    LocalDate.now()

    获取本周星期一至星期天

    LocalDate.now().with(DayOfWeek.MONDAY)
    ...
    ...
    LocalDate.now().with(DayOfWeek.SUNDAY)

    获取星期一至星期天中文:

    now.with(DayOfWeek.MONDAY).getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINA);
    ...
    ...
    now.with(DayOfWeek.SUNDAY).getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINA);

    获取一天开始时间

    final LocalDateTime startOfDay = LocalDate.now().atStartOfDay();

    获取一天结束时间

    final LocalDate now = LocalDate.now();
    final LocalDateTime endTime = LocalDateTime.of(now .getYear(), now .getMonth(), now .getDayOfMonth(), 23, 59, 59);

    获取前一天/后一天

    final LocalDateTime now = LocalDateTime.now();
    final LocalDateTime afterDay  = now.plusDays(1);  //增加一天
    final LocalDateTime beforeDay = now.minusDays(1); //减少一天

    获取当月开始时间 / 结束时间

    final LocalDate now = LocalDate.now();
    final LocalDateTime startOfDay = now.atStartOfDay();
    final LocalDateTime fistDayOfMonth = startOfDay.with(TemporalAdjusters.firstDayOfMonth());
    final LocalDateTime endOfDay = now.plusDays(1L).atStartOfDay().plusNanos(-1L)
    final LocalDateTime endDayOfMonth = endOfDay.with(TemporalAdjusters.lastDayOfMonth()

    获取当年开始时间 / 结束时间

    final LocalDate now = LocalDate.now();
    final LocalDateTime startOfDay = now.atStartOfDay();
    final LocalDateTime firstDayOfYear = startOfDay.with(TemporalAdjusters.firstDayOfYear())
    final LocalDateTime lastDayOfYear = startOfDay.with(TemporalAdjusters.lastDayOfYear())
  • 相关阅读:
    暑期学习录
    08管道命名符
    07输入输出重定向
    06grep与find命令详解
    05tar命令详解
    04文件目录管理命令
    03工作目录切换命令与文本文件编辑命令
    02系统状态检测命令
    01常用系统工作命令
    vue2.0细节剖析
  • 原文地址:https://www.cnblogs.com/smiledragon/p/14890794.html
Copyright © 2020-2023  润新知