• LocalDateTime 获取上个月最后第一天及最后一天


    有个需求 如果获取本月一号到昨天 如果今天是1号 获取上月月底到月末   实现如下 

    public static void main(String[] args) {

    // 如果是1号
    //LocalDate localDate = LocalDate.of(2020,11, 1);
    LocalDate localDate = LocalDate.of(2020,11, 11);
    LocalDateTime localDateTime=null;
    LocalDateTime localDateTime2=null;
    if(localDate.getDayOfMonth()==1){//1号查询上个月月底到月末
    int dayOfMonth = localDate.plusDays(-1).getDayOfMonth();//获取上个月的天数
    localDate = localDate.plusDays(-(dayOfMonth));//上一月一号
    String date = localDate.getYear() + "-" + localDate.getMonthValue() + "-" + localDate.getDayOfMonth();
    date = DateUtil.getDateShort(date);
    localDateTime = DateUtil.toLocalDateTime(date + " 00:00:00");
    date = localDate.getYear() + "-" + localDate.getMonthValue() + "-" + dayOfMonth;
    date = DateUtil.getDateShort(date);
    localDateTime2 = DateUtil.toLocalDateTime(date + " 00:00:00");
    }else{//一号到昨天
    int dayOfMonth = localDate.plusDays(-1).getDayOfMonth();//昨天
    String date = localDate.getYear() + "-" + localDate.getMonthValue() + "-" + 1;
    date = DateUtil.getDateShort(date);
    localDateTime = DateUtil.toLocalDateTime(date + " 00:00:00");
    date = localDate.getYear() + "-" + localDate.getMonthValue() + "-" + dayOfMonth;
    date = DateUtil.getDateShort(date);
    localDateTime2 = DateUtil.toLocalDateTime(date + " 00:00:00");
    }

    System.out.println(localDateTime);
    System.out.println(localDateTime2);

    }

  • 相关阅读:
    [ERROR] Terminal initialization failed; falling back to unsupported
    设计模式原则
    设计模式:概述
    INFO Dispatcher:42
    Exception occurred during processing request: id to load is required for loading
    Java编程基础篇第六章
    Spring (一)(未完~~~
    Spring MVC处理过程理解(一)
    Spring MVC源码解析(二)
    MyBatis拦截器(一)
  • 原文地址:https://www.cnblogs.com/Mr-Y1907/p/14024286.html
Copyright © 2020-2023  润新知