对日期操作的一些方法收集java

 

    /**
     * 字符串的日期加上一天,在返回字符串
     */
    public static String addOneDay(String countdate) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(sdf.parse(countdate));
        c.add(Calendar.DATE, 1);
        String rtn = sdf.format(c.getTime());
        return rtn;
    }
字符串的日期加上一天,在返回字符串
    /**
     * 
     * 字符串的日期减去一天,在返回字符串
     */
    public String subOneDay(String countdate) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(sdf.parse(countdate));
        c.add(Calendar.DATE, -1);
        String rtn = sdf.format(c.getTime());
        return rtn;
    }
字符串的日期减去一天,在返回字符串
/**
     * @param countdate
     * @param num 正数或负数
     * @return 字符串的日期添加或减轻自定义天数,在返回字符串
     * @throws Exception 
     * String
     * 
     */
    public String addOrSubSomeDay(String countdate,int num) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(sdf.parse(countdate));
        c.add(Calendar.DATE, num);
        String rtn = sdf.format(c.getTime());
        return rtn;
    }
字符串的日期添加或减轻自定义天数,在返回字符串

 

    public static String getQuarter(String tjrq) throws Exception{
        String  season = "summer";
        Calendar c = Calendar.getInstance();
        c.setTime(sdf.parse(tjrq));
        
        int month = c.get(Calendar.MONTH)+1;
        if(!(month>=4&&month<=9)){
             season = "winter";
        }
        return season;
    } 
输入日期取得季节

 

    /**
     *获取对应日期的288个时刻点 two hundred and eighty-eight
     *@date 2017-11-24 下午4:06:14
     *@author Administrator (chenrd)
     */
    public List<String> getTwoHundredAndEightyEightTime(String tjrq){
        List<String> rqs = new ArrayList<String>();
        
        for(int i=0;i<24;i++){
                if(i<10){
                    rqs.add(tjrq +" 0"+i+":00:00");
                    rqs.add(tjrq +" 0"+i+":05:00");
                    rqs.add(tjrq +" 0"+i+":10:00");
                    rqs.add(tjrq +" 0"+i+":15:00");
                    rqs.add(tjrq +" 0"+i+":20:00");
                    rqs.add(tjrq +" 0"+i+":25:00");
                    rqs.add(tjrq +" 0"+i+":30:00");
                    rqs.add(tjrq +" 0"+i+":35:00");
                    rqs.add(tjrq +" 0"+i+":40:00");
                    rqs.add(tjrq +" 0"+i+":45:00");
                    rqs.add(tjrq +" 0"+i+":50:00");
                    rqs.add(tjrq +" 0"+i+":55:00");
                }else{
                    rqs.add(tjrq +" "+i+":00:00");
                    rqs.add(tjrq +" "+i+":05:00");
                    rqs.add(tjrq +" "+i+":10:00");
                    rqs.add(tjrq +" "+i+":15:00");
                    rqs.add(tjrq +" "+i+":20:00");
                    rqs.add(tjrq +" "+i+":25:00");
                    rqs.add(tjrq +" "+i+":30:00");
                    rqs.add(tjrq +" "+i+":35:00");
                    rqs.add(tjrq +" "+i+":40:00");
                    rqs.add(tjrq +" "+i+":45:00");
                    rqs.add(tjrq +" "+i+":50:00");
                    rqs.add(tjrq +" "+i+":55:00");
                } 
        }
        return rqs;
    }
获取对应日期的288个时刻点
    /**
     *获取一段时间对应日期的288个时刻点 two hundred and eighty-eight
     *@author Administrator (chenrd)
     *@date 2018-1-17 上午11:27:20
     */
    public static  List<String> getTwoHundredAndEightyEightTime(String kssj,String jssj) throws Exception{
        List<String> rqs = new ArrayList<String>();
        List<String> tempRqs ;
        Calendar ksc = Calendar.getInstance();
        Calendar jsc = Calendar.getInstance();
        ksc.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(kssj));
        jsc.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(jssj));
        //相差的天数
        long totleDay = (jsc.getTimeInMillis()-ksc.getTimeInMillis())/(24*60*60*1000);
        for(int i=0;i<=totleDay;i++){
            tempRqs = getTwoHundredAndEightyEightTime(new SimpleDateFormat("yyyy-MM-dd").format(ksc.getTime()));
            ksc.add(Calendar.DATE, 1);
            rqs.addAll(tempRqs);
        }
        return rqs;
    }
获取一段时间对应日期的288个时刻点

 

/**
     * 获取当月第一天
     * 
     * @return
     */
    public Date getMonthFirstDay() {
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMinimum(Calendar.DAY_OF_MONTH));
        return ca.getTime();
    }
获取当月第一天
    /**
     *获取当月第一天(传参)
     * 
     * @return
     * @throws ParseException
     */
    public Date getMonthFirstDay(String rq) throws ParseException {
        Calendar ca = Calendar.getInstance();
        ca.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(rq));
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMinimum(Calendar.DAY_OF_MONTH));
        return ca.getTime();
    }
获取当月第一天(传参)
    /**
     * 获取当月最后一天
     * 
     * @return
     */
    public Date getMonthLastDay() {
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        return ca.getTime();
    }
获取当月最后一天
    /**
     * 获取当月最后一天
     * 
     * @return
     * @throws ParseException
     */
    public Date getMonthLastDay(String rq) throws ParseException {
        Calendar ca = Calendar.getInstance();
        ca.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(rq));
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        return ca.getTime();
    }
获取当月最后一天(传参)

 

posted @ 2020-12-30 16:04  爱跳舞的程序员  阅读(79)  评论(0编辑  收藏  举报