java日期加减

复制代码
1./** 
2.     * 计算前一天 、后一天 
3.     *  
4.     * @param date 当前输入的日期 格式为 2010-01-21 
5.     * @param amount 
6.     * @return 
7.     */  
8.    public static String getYesterday(String date, int amount) {  
9.        String newDate = Constant.EMPTY_STRING;  
10.        if (date != null) {  
11.            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
12.            Date dd;  
13.            try {  
14.                dd = format.parse(date);  
15.                Calendar calendar = Calendar.getInstance();  
16.                calendar.setTime(dd);  
17.                calendar.add(Calendar.DAY_OF_MONTH, amount);  
18.                newDate = format.format(calendar.getTime());  
19.  
20.            } catch (ParseException e) {  
21.                e.printStackTrace();  
22.            }  
23.  
24.        }  
25.        return newDate;  
26.    }  
27.  
28.  
29.  
30./** 
31.     * 计算两天之间相差的天数 调用此方法必须保证 时间格式与 timeFormatStr指定格式一致 否则异常 
32.     *  
33.     * @param beginDate 开始时间 
34.     * @param endDate 结束时间 
35.     * @param timeFormatStr  输入时间格式 
36.     * @return int 时间相差天数 
37.     */  
38.    public static int getCountDays(String beginDate, String endDate, String timeFormatStr) {  
39.        int days = 0;  
40.  
41.        if (beginDate != null && endDate != null && timeFormatStr != null) {  
42.            SimpleDateFormat dateFormat = new SimpleDateFormat(timeFormatStr);  
43.            Calendar beginCalendar = Calendar.getInstance();  
44.            Calendar endCalendar = Calendar.getInstance();  
45.  
46.            try {  
47.                endCalendar.setTime(dateFormat.parse(endDate));  
48.                beginCalendar.setTime(dateFormat.parse(beginDate));  
49.                while (beginCalendar.before(endCalendar)) {  
50.                    days++;  
51.                    beginCalendar.add(Calendar.DAY_OF_YEAR, 1);  
52.                }  
53.            } catch (ParseException e) {  
54.                // 日期格式异常  
55.                logger.error("getCountDays failed the" + beginDate + "or" + endDate + "is not format"  
56.                        + timeFormatStr, e);  
57.            }  
58.        }  
59.        return days;  
60.    }  
61.  
62.  
63./** 取近期一年的数据*/  
64.    public static List<String> getYears() {  
65.        List<String> result = new ArrayList<String>();  
66.        Calendar c = Calendar.getInstance();  
67.        c.set(Calendar.YEAR, Integer.parseInt(DateUtil.getSysDate("yyyy")));  
68.        c.set(Calendar.MONTH, Integer.parseInt(DateUtil.getSysDate("MM")));  
69.  
70.        result.add(DateUtil.getSysDate("yyyyMM"));  
71.  
72.        for (int i = 0; i < 11; i++) {  
73.            c.add(Calendar.MONTH, -1);  
74.            int y = c.get(Calendar.YEAR);  
75.            int m = c.get(Calendar.MONTH);  
76.  
77.            if (m == 0) {  
78.                result.add((y - 1) + "12");  
79.            } else {  
80.                if (m < 10)  
81.                    result.add(y + "0" + m);  
82.                else  
83.                    result.add(y + (m + ""));  
84.            }  
85.        }  
86.        return result;  
87.    }  
88.  
89.    /** 取上个月 */    
90.    public static String getPrevMonth(String yearMonth) {  
91.        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");  
92.        Calendar calendar = Calendar.getInstance();  
93.        Date curDate = java.sql.Date.valueOf(yearMonth + "-10");  
94.        calendar.setTime(curDate);  
95.        // 取得现在时间  
96.        // System.out.println(sdf.format(curDate));  
97.  
98.        // 取得上一个时间  
99.        calendar.set(Calendar.MONDAY, calendar.get(Calendar.MONDAY) - 1);  
100.  
101.        // 取得上一个月的下一天  
102.        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);  
103.        return sdf.format(calendar.getTime());  
104.    }  
105.  
106.    /** 取当月下的最后第一天 */  
107.    public static void getLastDay(String yearMonth) {  
108.        Calendar cal = Calendar.getInstance();  
109.        cal.setTime(java.sql.Date.valueOf(yearMonth + "-01"));  
110.  
111.        cal.roll(Calendar.DAY_OF_MONTH, -1);  
112.        System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()));  
113.    }  
114.  
115.    /** 取当月下的第一天 */  
116.    public static void getFirstDay(String yearMonth) {  
117.        Calendar cal = Calendar.getInstance();  
118.        cal.setTime(java.sql.Date.valueOf(yearMonth + "-01"));  
119.        cal.set(Calendar.DAY_OF_MONTH, 1);  
120.        System.out.println(new SimpleDateFormat(DEFAULT_DATE_FORMAT).format(cal.getTime()));  
121.    }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值