zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    XML时代离我们有多远?
    关注程序员健康之——最佳答案梅核气
    巾帼不让须眉 IT界10大女性CEO排行
    网站?XML?我的思考
    企业建站代码HTML滚动文字代码(垂直)
    web前端工程师:WEB标准,Web前端开发工程师必备技术列表
    数据库开发
    面向.NET 的XML 程序设计
    脾与胃病辨证
    技术部工作中常见问题(o_company)
  • 原文地址:https://www.cnblogs.com/ly-radiata/p/5593608.html
Copyright © 2011-2022 走看看