liuxiaolong
2019-05-09 0d1d88cdb668e75ea8609417ac18ae19947e9525
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package framework.util;
 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
 
public final class TimeUtil {
    public static final SimpleDateFormat yyyy_MM_dd=new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat yyyy_MM_dd_HH_mm_ss=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat TIME_FULL =new SimpleDateFormat("yyyyMMdd-HHmmss-SSS");
    
    /**
     * yyyy年MM月dd日
     * @param date
     * @return
     */
    public static String getDateCnStr(Date date){
        if(date==null){
            return "";
        }
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy年MM月dd日");
        return fmt.format(date);
    }
    
    /**
     * new Date().getTime()
     * 
     * @return
     */
    public static String getTime() {
        return String.valueOf(new Date().getTime());
    }
 
    /**
     * yyyy-MM-dd_HH_mm_ss
     * @return
     */
    public static String getLongTime() {
        SimpleDateFormat fmt = new SimpleDateFormat(
                "yyyy-MM-dd_HH_mm_ss");
        return fmt.format(new Date());
    }
    /**
     * yyyyMMdd
     * @return
     */
    public static String getDate8() {
        SimpleDateFormat fmt = new SimpleDateFormat(
                "yyyyMMdd");
        return fmt.format(new Date());
    }
    /**
     * yyyy-MM-dd
     * @param date
     * @return
     */
    public static String getDate10(Date date){
        if(date==null){
            return "";
        }
        return yyyy_MM_dd.format(date);
    }
 
    /**
     * yyyy-MM-dd HH:mm:ss
     * @param date
     * @return
     */
    public static String getDate20(Date date) {
        SimpleDateFormat fmt = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        return fmt.format(date);
    }
    /**
     * yyyy-MM-dd 00:00:00
     * @param date
     * @return
     */
    public static Date getDateBegin(Date date) throws Exception {
        String s = yyyy_MM_dd.format(date)+" 00:00:00";
        return yyyy_MM_dd_HH_mm_ss.parse(s);
    }
    /**
     * yyyy-MM-dd 00:00:00
     * @param date
     * @return
     * @throws Exception
     */
    public static Date getDateBegin(String date) throws Exception {
        String s = date+" 00:00:00";
        return yyyy_MM_dd_HH_mm_ss.parse(s);
    }
    /**
     * yyyy-MM-dd 23:59:59
     * @param date
     * @return
     * @throws Exception
     */
    public static Date getDateEnd(Date date) throws Exception {
        String s = yyyy_MM_dd.format(date)+" 23:59:59";
        return yyyy_MM_dd_HH_mm_ss.parse(s);
    }
    /**
     * yyyy-MM-dd 23:59:59
     * @param date
     * @return
     * @throws Exception
     */
    public static Date getDateEnd(String date) throws Exception {
        String s = date+" 23:59:59";
        return yyyy_MM_dd_HH_mm_ss.parse(s);
    }
    /**
     * 天数计算
     * @param date
     * @param d 加减天数
     * @return
     */
    public static Date getDate(Date date, int d){
        long t = date.getTime()+24*3600000*d;
        return new Date(t);
    }
    
    /**
     * 当月第一天
     * @return
     */
    public static String getCurrentMonthFirstDate(){
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH)+1;
        
        StringBuffer s = new StringBuffer();
        s.append(year).append("-");
        if(month<10){
            s.append("0");
        }
        s.append(month).append("-01");
        return s.toString();
    }
    
    /**
     * 当月最后一天
     * @return
     */
    public static String getCurrentMonthLastDate(){
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 1);
        c.set(Calendar.DATE, 1);
        c.add(Calendar.DATE, -1);
        
        return yyyy_MM_dd.format(c.getTime());
    }
 
}