美文网首页
java calendar操作2019-03-08

java calendar操作2019-03-08

作者: _好孩子 | 来源:发表于2019-03-22 14:02 被阅读0次

/**

    * 获取今日开始时间和结束时间

    * @param condition

    * @return

    */

    private void setDayRange(PageData condition){

        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.MILLISECOND,0);

        calendar.set(Calendar.SECOND,0);

        calendar.set(Calendar.MINUTE,0);

        calendar.set(Calendar.HOUR_OF_DAY,0);

        condition.put("startTime",calendar.getTime());

        calendar.set(Calendar.HOUR_OF_DAY,24);

        condition.put("stopTime",calendar.getTime());

    }

    /**

    * 获取昨天开始时间和结束时间

    * @param condition

    * @return

    */

    private void setYesterdayRange(PageData condition) {

        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.MILLISECOND,0);

        calendar.set(Calendar.SECOND,0);

        calendar.set(Calendar.MINUTE,0);

        calendar.set(Calendar.HOUR_OF_DAY,0);

        condition.put("stopTime",calendar.getTime());

        calendar.set(Calendar.HOUR_OF_DAY,-24);

        condition.put("startTime",calendar.getTime());

    }

    /**

    * 获得最近三天的开始时间和结束时间

    * @param condition

    * @return

    */

    private void setThreeRange(PageData condition) {

        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.MILLISECOND,0);

        calendar.set(Calendar.SECOND,0);

        calendar.set(Calendar.MINUTE,0);

        calendar.set(Calendar.HOUR_OF_DAY,24);

        condition.put("stopTime",calendar.getTime());

        calendar.set(Calendar.HOUR_OF_DAY,-72);

        condition.put("startTime",calendar.getTime());

    }

    /**

    * 获得近一周的开始时间和结束时间

    * @param condition

    * @return

    */

    private void setDaySevenRange(PageData condition){

        Calendar calendar = Calendar.getInstance();

        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);

        calendar.set(Calendar.HOUR_OF_DAY,24);

        condition.put("stopTime",calendar.getTime());

        calendar.set(Calendar.HOUR_OF_DAY,-168);

        condition.put("startTime",calendar.getTime());

    }

    /**

    * 获得近一月的开始时间和结束时间

    * @param condition

    * @return

    */

    private void setDayTRange(PageData condition){

        Calendar calendar = Calendar.getInstance();

        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);

        calendar.set(Calendar.HOUR_OF_DAY,24);

        condition.put("stopTime",calendar.getTime());

        calendar.set(Calendar.HOUR_OF_DAY,-720);

        condition.put("startTime",calendar.getTime());

    }

    /**

    * 获取本周开始时间和结束时间

    * @param condition

    * @return

    */

    private void setWeekRange(PageData condition){

        Calendar calendar = Calendar.getInstance();

        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);

        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

        condition.put("startTime",calendar.getTime());

        calendar.add(Calendar.DAY_OF_WEEK, 7);

        condition.put("stopTime",calendar.getTime());

    }

    /**

    * 获取上周开始时间和结束时间

    * @param condition

    * @return

    */

    private void setLastWeekRange(PageData condition){

        Calendar calendar = Calendar.getInstance();

        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);

        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

        condition.put("stopTime",calendar.getTime());

        calendar.add(Calendar.DAY_OF_WEEK, -7);

        condition.put("startTime",calendar.getTime());

    }

    /**

    * 获取本月开始时间和结束时间

    * @param condition

    * @return

    */

    private void setMonthRange(PageData condition){

        Calendar calendar = Calendar.getInstance();

        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);

        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));

        condition.put("startTime",calendar.getTime());

        calendar.add(Calendar.DAY_OF_WEEK, 7);

        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));

        calendar.set(Calendar.HOUR_OF_DAY, 24);

        condition.put("stopTime",calendar.getTime());

    }

    /**

    * 获取上月开始时间和结束时间

    * @param condition

    * @return

    */

    private void setLastMonthRange(PageData condition){

        Calendar calendar = Calendar.getInstance();

        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);

        calendar.add(Calendar.MONTH, -1);

        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));

        condition.put("startTime",calendar.getTime());

        calendar.add(Calendar.DAY_OF_WEEK, 7);

        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));

        calendar.set(Calendar.HOUR_OF_DAY, 24);

        condition.put("stopTime",calendar.getTime());

    }

    /**

    * 获取本季度开始时间和结束时间

    * @param condition

    * @return

    */

    private void setQuarterRange(PageData condition){

        Calendar calendar = Calendar.getInstance();

        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),        calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);

        int currentMonth = calendar.get(Calendar.MONTH) + 1;

        try {

            if (currentMonth >= 1 && currentMonth <= 3)

                calendar.set(Calendar.MONTH, 0);

            else if (currentMonth >= 4 && currentMonth <= 6)

                calendar.set(Calendar.MONTH, 3);

            else if (currentMonth >= 7 && currentMonth <= 9)

                calendar.set(Calendar.MONTH, 6);

            else if (currentMonth >= 10 && currentMonth <= 12)

                calendar.set(Calendar.MONTH, 9);

            calendar.set(Calendar.DATE, 1);

        } catch (Exception e) {

            e.printStackTrace();

        }

        condition.put("startTime",calendar.getTime());

        calendar.add(Calendar.MONTH, 3);

        condition.put("stopTime",calendar.getTime());

    }

    /**

    * 获取上季度开始时间和结束时间

    * @param condition

    * @return

    */

    private void setLastQuarterRange(PageData condition){

        Calendar calendar = Calendar.getInstance();

        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);

        int currentMonth = calendar.get(Calendar.MONTH) + 1;

        try {

            if (currentMonth >= 1 && currentMonth <= 3)

                calendar.set(Calendar.MONTH, 0);

            else if (currentMonth >= 4 && currentMonth <= 6)

                calendar.set(Calendar.MONTH, 3);

            else if (currentMonth >= 7 && currentMonth <= 9)

                calendar.set(Calendar.MONTH, 6);

            else if (currentMonth >= 10 && currentMonth <= 12)

                calendar.set(Calendar.MONTH, 9);

            calendar.set(Calendar.DATE, 1);

        } catch (Exception e) {

            e.printStackTrace();

        }

        calendar.add(Calendar.MONTH, -3);

        condition.put("startTime",calendar.getTime());

        calendar.add(Calendar.MONTH, 3);

        condition.put("stopTime",calendar.getTime());

    }

    /**

    * 获取本年开始时间和结束时间

    * @param condition

    * @return

    */

    private void setYearRange(PageData condition){

        Calendar calendar = Calendar.getInstance();

        calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);

        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));

        condition.put("startTime",calendar.getTime());

        calendar.add(Calendar.YEAR, 1);

        condition.put("stopTime",calendar.getTime());

    }

    /**

    * 获取上一年开始时间和结束时间

    * @param condition

    * @return

    */

    private void setLastYearRange(PageData condition){

        Calendar calendar = Calendar.getInstance();

        calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);

        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));

        calendar.add(Calendar.YEAR, -1);

        condition.put("startTime",calendar.getTime());

        calendar.add(Calendar.YEAR, 1);

        condition.put("stopTime",calendar.getTime());

    }

    /**

    * ("startTime" -> "Fri Jun 08 00:00:00 CST 2018")String的Date转为String类型的date   

    */

    private String strToformatDate(String date){

        String str="";

        Date date1=new Date();

        try {

            DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);

            date1 = df.parse(date);

            df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            str = df.format(date1);

        }catch (Exception e){

            e.printStackTrace();

        }

        return str;

    }

    /**

    * ("startTime" -> "Fri Jun 08 00:00:00 CST 2018")String的Date转为时间戳

    */

    public long toformatDate(String date){

        String str="";

        Date date1=new Date();

        try {

            DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);

            date1 = df.parse(date);

            System.err.println(date1.getTime()/1000);

        }catch (Exception e){

            e.printStackTrace();

        }

        return date1.getTime()/1000;

    }

/**

* 测试

*/

public static void main(String[] args) {

    PageData condition = new PageData();

    setLastQuarterRange(condition);

    String startTime=String.valueOf(strToformatDate(condition.get("startTime").toString()));

    String stopTime=String.valueOf(strToformatDate(condition.get("stopTime").toString()));

    System.out.println(startTime+"--->"+stopTime);

}

/**

* 得到n天之后是周几

* @param days

* @return

*/

public static String getAfterDayWeek(String days) {

        int daysInt = Integer.parseInt(days);

        Calendar canlendar = Calendar.getInstance(); // java.util包

        canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动

        Date date = canlendar.getTime();

        SimpleDateFormat sdf = new SimpleDateFormat("E");

        String dateStr = sdf.format(date);

        return dateStr;

}

/**

* 得到n天之后的当前时间

* @param days

* @return

*/

public static String getAfterDayDate(String days) {

        int daysInt = Integer.parseInt(days);

        Calendar canlendar = Calendar.getInstance(); // java.util包

        canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动

        Date date = canlendar.getTime();

        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String dateStr = sdfd.format(date);

        return dateStr;

}

/**

  * 得到几分钟后的时间

  * @param date minute(可为正负)

  * @return

  */

public static Date getAfterTimeDate(Date date,int minute) {

    Calendar canlendar = Calendar.getInstance(); // java.util包

    canlendar.setTime(date);

    canlendar.add(Calendar.MINUTE, minute); // 日期减 如果不够减会将月变动

    return canlendar.getTime();

}

/**

  * 时间相减得到天数(开始时间和结束时间可任意大小)

  * @param beginDateStr(格式为"yyyy-MM-dd",也可为"yyyy-MM-dd hh:mm:ss")

  * @param endDateStr

  * @return  long

  */

  public static long getDaySub(String beginDateStr,String endDateStr){

      long day=0;

      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

      //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

      Date beginDate = null;

      Date endDate = null;

      try {

        beginDate = format.parse(beginDateStr);

        endDate= format.parse(endDateStr);

} catch (ParseException e) {

        e.printStackTrace();

}

      day=(endDate.getTime()-beginDate.getTime())/(24*60*60*1000);

      //System.out.println("相隔的天数="+day);

      return day;

  }

---------------------

作者:123~六儿

来源:CSDN

原文:https://blog.csdn.net/weixin_43052839/article/details/82015021

版权声明:本文为博主原创文章,转载请附上博文链接!

相关文章

网友评论

      本文标题:java calendar操作2019-03-08

      本文链接:https://www.haomeiwen.com/subject/jofdpqtx.html