• Date get period



    /**
    * get period for last year
    * @param time
    * @return
    */
    public static DatePeriodDTO getLastYear(long time) {
    Calendar calendar = Calendar.getInstance();
    //get last year
    calendar.add(Calendar.YEAR, -1);

    int lastYear = calendar.get(Calendar.YEAR);
    calendar.clear();
    //set to last year
    calendar.set(Calendar.YEAR, lastYear);
    //get first day of last year
    Date yearFrom = calendar.getTime();
    //get last day of last year
    calendar.roll(Calendar.DAY_OF_YEAR, -1);
    Date yearTo = calendar.getTime();
    return new DatePeriodDTO(yearFrom, yearTo);

    }

    /**
    * get period for last month
    * @param time
    * @return
    */
    public static DatePeriodDTO getLastMonth(long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MONTH, -1);

    //last day of last month
    calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    Date strDateTo = calendar.getTime();

    //first day of last month
    calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
    Date strDatefrom = calendar.getTime();

    return new DatePeriodDTO(strDatefrom, strDateTo);
    }

    /**
    * get period for last week
    * @param time
    * @return
    */
    public static DatePeriodDTO getLastWeek(long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    //last Sunday
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    calendar.add(Calendar.WEEK_OF_MONTH, -1);
    Date endDate = calendar.getTime();
    //last Monday
    calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 6);
    Date startDate = calendar.getTime();
    return new DatePeriodDTO(startDate, endDate);
    }

    /**
    * is last year
    * @param time
    * @return
    */
    public static boolean isLastYear(long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.YEAR, -1);
    int lastYear = calendar.get(Calendar.YEAR);
    calendar.setTime(new Date(time));
    int paramYear = calendar.get(Calendar.YEAR);
    if (paramYear == lastYear) {
    return true;
    }
    return false;
    }

    /**
    * is last month
    * @param time
    * @return
    */
    public static boolean isLastMonth(long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MONTH, -1);
    int lastMonth = calendar.get(Calendar.MONTH);
    calendar.setTime(new Date(time));
    int paramMonth = calendar.get(Calendar.MONTH);
    if (paramMonth == lastMonth) {
    return true;
    }
    return false;
    }

    /**
    * is last week
    * @param time
    * @return
    */
    public static boolean isLastWeek(long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    calendar.add(Calendar.DATE, -7);
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    int lastWeek = calendar.get(Calendar.WEEK_OF_YEAR);
    calendar.setTime(new Date(time));
    int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
    if (paramWeek == lastWeek) {
    return true;
    }
    return false;
    }

  • 相关阅读:
    WIN32弹窗报时间【CSDN】
    函数返回类型为引用的好处
    计算机补码的秘密【转】
    设计模式之Singleton模式【类只有一个对象】
    bitwise constness与logical constness【EC++笔记】
    避免使用隐式类型转换【转】
    private, public, protected,继承【转】
    nonlocal static对象初始化【EC++笔记】
    在资源管理类中提供对原始资源的访问【EC++笔记】
    copy构造函数与copy赋值操作符【EC++笔记】
  • 原文地址:https://www.cnblogs.com/ly-radiata/p/5593608.html
Copyright © 2020-2023  润新知