songhuangfeng123
2022-09-02 47bfee2fafdae8dd9339021f787787acfddfc0a5
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.gkhy.safePlatform.emergency.utils;
 
import com.gkhy.safePlatform.commons.enums.E;
 
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
public class TimeUtils {
 
    public static String formatDate(Date date) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        String sDate = f.format(date);
        return sDate;
    }
 
 
    public static void main(String[] args) {
        String XXX= "2022-09-01";
        String EEE = formatDate(new Date());
        int a = differentDays(XXX, EEE);
        System.out.println(a);
    }
 
 
    /**
     * 计算两个时间的间隔
     * date2比date1多的天数
     */
    public static int differentDays(String date1, String date2) {
        int days = 0;
        try {
            DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
            Date star = dft.parse(date1);
            Date end = dft.parse(date2);//结束时间
            Long starTime = star.getTime();
            Long endTime = end.getTime();
            Long num = endTime - starTime;//时间戳相差的毫秒数
            days = (int) (num / 24 / 60 / 60 / 1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }
 
 
    /**
     * 获取某年第一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static String getYearFirst(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return formatDate(currYearFirst);
    }
 
    /**
     * 获取某年最后一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static String getYearLast(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();
 
        return formatDate(currYearLast);
    }
 
    /**
     * 获取某月第一天日期
     *
     * @param month 年份
     * @return Date
     */
    public static String getMonthFirst(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        Date currYearFirst = calendar.getTime();
        return formatDate(currYearFirst);
    }
 
    /**
     * 获取某月最后一天日期
     *
     * @param month 年份
     * @return Date
     */
    public static String getMonthLast(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.roll(Calendar.DAY_OF_MONTH, -1);
        Date currYearLast = calendar.getTime();
 
        return formatDate(currYearLast);
    }
 
 
}