罗明文
1 天以前 aa512ff5fc428fbee046d6bc0761c3675023769e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.dollearn.student.utils;
 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
 
/**
 * Created by zhouyou on 2016/7/25.
 * Class desc: 日期工具类
 */
public class DateUtils {
 
    /**
     * 通过年份和月份 得到当月的日子
     */
    public static int getMonthDays(int year, int month) {
        month++;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
                    return 29;
                } else {
                    return 28;
                }
            default:
                return -1;
        }
    }
 
    /**
     * 返回当前月份1号位于周几
     *
     * @param year  年份
     * @param month 月份,传入系统获取的,不需要正常的
     * @return 日:1        一:2        二:3        三:4        四:5        五:6        六:7
     */
    public static int getFirstDayWeek(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, 1);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }
 
 
    /**
     * 查询当前日期前(后)x天的日期
     *
     * @param date 当前日期
     * @param day  天数(如果day数为负数,说明是此日期前的天数)
     * @return yyyyMMdd
     */
    private static String beforNumberDay(Date date, int day) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_YEAR, day);
        return new SimpleDateFormat("yyyyMMdd").format(c.getTime());
    }
}