时间、日期

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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
public class TimeOrDateUtil {
public static String YYYY_MM_DD = "yyyy-MM-dd";
public static String YYYY_MM_DD_HH_DD = "yyyy-MM-dd HH:mm";// 分钟
public static String YYYY_MM = "yyyy-MM";
public static String YYYY = "yyyy";
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
// public static int WEEK_TIME=java.text.DateFormat.WEEK_OF_MONTH_FIELD;
public static String MM_SS = "mm:ss";

/**
* 获取当前时间
*
* @param type
* @return
*/
public static String getNowTime(String type) {
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat(type);// 可以方便地修改日期格式
return dateFormat.format(now);
}


/**
* 传入时间,返回指定格式的时间字符串
*
* @param type
* @return
*/
public static String formatDate(Date date, String type) {
SimpleDateFormat dateFormat = new SimpleDateFormat(type);
return dateFormat.format(date);
}

/**
* 字符串时间转成Date型时间
*
* @param dateStr
* @param type
* @return
*/
public static Date StringToDate(String dateStr, String type) {
Date date = null;
try {
date = new SimpleDateFormat(type).parse(dateStr);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}

/**
* 传入时间跟时间格式返回long型时间
*
* @param time
* @param type
* @return
*/
public static long getLongTime(String time, String type) {
long timeL = 0;
try {
timeL = new SimpleDateFormat(type).parse(time).getTime();
} catch (Exception e) {
e.printStackTrace();
}
return timeL;
}

/**
* 传入long型返回对应格式的时间
*/
public static String long2FormatTime(long timeL, String type) {
String formatTime = "";
SimpleDateFormat sdf = new SimpleDateFormat(type);
Date dt = new Date(timeL);
formatTime = sdf.format(dt);
return formatTime;
}

/**
* 传入时间(格式为HH:mm)返回long型时间
*
* @param time
* @return
*/
public static long getLongTime(String time) {
long longTime = 0;
String nowDate = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(new Date());
nowDate = nowDate.substring(0, 10);
time = nowDate + " " + time + ":00";
try {
longTime = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).parse(time).getTime();
} catch (Exception e) {
e.printStackTrace();
}
return longTime;
}

/**
* 获取昨天时间
*
* @return
*/
public static String getLastTime() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
String yesterday = new SimpleDateFormat(YYYY_MM_DD).format(cal.getTime());
return yesterday;
}

/**
* 获取上月
*
* @return
*/
public static String getLastMonth() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
String day = new SimpleDateFormat(YYYY_MM).format(cal.getTime());
return day;
}

/**
* 获取当年
*
* @return
*/
public static String getLastYear() {
Calendar cal = Calendar.getInstance();
// cal.add(Calendar.YEAR, 0);//获取上一年
cal.add(Calendar.YEAR, 0);
String day = new SimpleDateFormat(YYYY).format(cal.getTime());
return day;
}

/**
* 计算两个时间天数差
*
* @param fromDate
* @param toDate
* @return
*/
public static int calculateDays(Date fromDate, Date toDate) {
if (null == fromDate || null == toDate) {
throw new NullPointerException();
}

if (fromDate.before(toDate)) {
return (int) ((toDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24));
}
return (int) ((fromDate.getTime() - toDate.getTime()) / (1000 * 60 * 60 * 24));
}

/**
* 计算小时差
*
* @param fromDate
* @param toDate
* @return
*/
public static int calculateHours(Date fromDate, Date toDate) {
if (null == fromDate || null == toDate) {
throw new NullPointerException();
}

if (fromDate.before(toDate)) {
return (int) ((toDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60));
}
return (int) ((fromDate.getTime() - toDate.getTime()) / (1000 * 60 * 60));
}

/**
* 计算分钟差
*
* @param fromDate
* @param toDate
* @return
*/
public static int calculateMinutes(Date fromDate, Date toDate) {
if (null == fromDate || null == toDate) {
throw new NullPointerException();
}

if (fromDate.before(toDate)) {
return (int) ((toDate.getTime() - fromDate.getTime()) / (1000 * 60));
}
return (int) ((fromDate.getTime() - toDate.getTime()) / (1000 * 60));
}
}