zoukankan      html  css  js  c++  java
  • Java之时间处理(当前年的上一年、上一季度、当月、当季)

    一、当前年的上一年

    核心代码:

    public static String getYearBefore() {
          SimpleDateFormat formats = new SimpleDateFormat("yyyy");
          Calendar c = Calendar.getInstance();
          c.add(Calendar.YEAR, -1);
          Date date = c.getTime();
          return formats.format(date);
     }

    二、上一季度

    核心代码:

    /**
     * 获取上一季度 开始和结束时间
     *
     * @return
     */
    public static DateRange getLastQuarter() {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3 - 1) * 3);
        startCalendar.set(Calendar.DAY_OF_MONTH, 1);
        setMinTime(startCalendar);
    
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.set(Calendar.MONTH, ((int) endCalendar.get(Calendar.MONTH) / 3 - 1) * 3 + 2);
        endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        setMaxTime(endCalendar);
    
        return new DateRange(startCalendar.getTime(), endCalendar.getTime());
    }
    
    
    /**
     * 最小时间
     *
     * @param calendar
     */
    private static void setMinTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }
    
    /**
     * 最大时间
     *
     * @param calendar
     */
    private static void setMaxTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
    }

    DateRange.java:

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class DateRange {
        private Date start;
        private Date end;
    }

    三、当月

    核心代码:

    /**
       * 获取当月(开始时间)
       *
       * @return
       */
      public static String getCurrentMonthStartDate() {
    
          // 获取当前年份、月份、日期
          Calendar cale = null;
          // 获取当月第一天
          SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
          String firstday;
          cale = Calendar.getInstance();
          cale.add(Calendar.MONTH, 0);
          cale.set(Calendar.DAY_OF_MONTH, 1);
          firstday = format.format(cale.getTime());
          return firstday;
      }
    
    
      /**
       * 获取当月(结束时间)
       *
       * @return
       */
      public static String getCurrentMonthEndDate() {
    
          // 获取当前年份、月份、日期
          Calendar cale = null;
          // 获取当月最后一天
          SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
          String lastday;
          cale = Calendar.getInstance();
          cale.add(Calendar.MONTH, 1);
          cale.set(Calendar.DAY_OF_MONTH, 0);
          lastday = format.format(cale.getTime());
          return lastday;
      }

    四、当季

    核心代码:

    /**
        * 获取当季
        *
        * @return
        */
       public static DateRange getThisQuarter() {
           Calendar startCalendar = Calendar.getInstance();
           startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3) * 3);
           startCalendar.set(Calendar.DAY_OF_MONTH, 1);
           setMinTime(startCalendar);
    
           Calendar endCalendar = Calendar.getInstance();
           endCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3) * 3 + 2);
           endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
           setMaxTime(endCalendar);
    
           return new DateRange(startCalendar.getTime(), endCalendar.getTime());
       }
  • 相关阅读:
    flume+kafka (分区实现 默认单分区)
    flume+kafka (分区实现 默认单分区) (二)
    trident 序列号问题
    C#运动控制日志报警记录和操作记录记录查询(SqlLite数据库操作)
    C#运动控制运动暂停思路检测沿信号方法
    C#使用json配置文件方法【读写Json,适合小项目】
    C#NLog日志组件使用
    C#运动控制05实时报警及显示方案
    C#USB扫码枪实现无焦点状态下扫入
    C#运动控制05日志显示(ListView和imageList控件使用)
  • 原文地址:https://www.cnblogs.com/youcong/p/14619022.html
Copyright © 2011-2022 走看看