DateFormat.java
6.6 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package com.framework.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
/**
* 时间 日期 与字符串 转换处理工具类
* @author s
*
*/
public class DateFormat {
/**
* 获取当前年月日字符串
* 20130708
* @return
*/
public static String getYmd(){
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH)+1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
return ""+year+(month <10?"0"+month:month)+(day <10?"0"+day:day);
}
/**
* 把字符串转换成Date
* @param date
* @param format
* @return
* @throws ParseException
*/
public static Date formatDate(String dateSource,String format){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
Date date = null;
try {
date = simpleDateFormat.parse(dateSource);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 把字Date换成Date
* @param date
* @param format
* @return
* @throws ParseException
*/
public static Date formatDate(Date dateSource,String format){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
Date date = null;
try {
date = simpleDateFormat.parse(formatString(dateSource,format));
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 把日期转换成特定字符串
* @param dateSource
* @param format
* @return
* @throws ParseException
*/
public static String formatString(Date dateSource,String format){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
String str = null;
str = simpleDateFormat.format(dateSource);
return str;
}
/**
* 把日期转换成特定字符串
* @param dateSource
* @param format
* @return
* @throws ParseException
*/
public static String formatString(String dateSource,String format){
Date d = formatDate(dateSource,format);
String str = null;
str = formatString(d,format);
return str;
}
/**
* 查询上一个月时间
* @return
*/
public static Date getBeforeMonth() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
return formatDate(calendar.getTime(),"yyyy-MM");
}
/**
* 查询上下个月时间
* @return
*/
public static Date getAfterMonth() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 1);
return formatDate(calendar.getTime(),"yyyy-MM");
}
/**
* 查询上下个月时间
* @return
*/
public static Date getAfterMonth(Date targetDate) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(targetDate);
calendar.add(Calendar.MONTH, 1);
return formatDate(calendar.getTime(),"yyyy-MM");
}
/**
* 查询月
* @return
*/
public static Date getMonth(Date targetDate) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(targetDate);
return formatDate(calendar.getTime(),"yyyy-MM");
}
/**
* 添加 n days 自然日
* @param source
* @param days
* @return
*/
public static Date addDays(Date source,int days) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(source);
calendar.add(Calendar.DATE, days);
return calendar.getTime();
}
/**
* 添加 n days 工作日
* @param source
* @param days
* @return
*/
public static Date addWorkDays(Date source,int days) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(source);
//循环
for(int i=1;i<=days;i++) {
calendar.add(Calendar.DATE, 1);
//如果是周六
if(calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY){
calendar.add(Calendar.DATE, 2);
}else if(calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
calendar.add(Calendar.DATE, 1);
}
}
return calendar.getTime();
}
/**
* 计算两个日期之间相差的月份
* @param minDate
* @param maxDate
* @return
*/
public static String[] getMonthBetween(Date minDate, Date maxDate){
try{
ArrayList<String> result = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
min.setTime(sdf.parse(formatString(minDate, "yyyy-MM")));
min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
max.setTime(sdf.parse(formatString(maxDate, "yyyy-MM")));
max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
Calendar curr = min;
while (curr.before(max)) {
result.add(sdf.format(curr.getTime()));
curr.add(Calendar.MONTH, 1);
}
String[] months = new String[result.size()+1];
for(int i=0;i<result.size();i++) {
months[i] = result.get(i);
}
months[result.size()] = formatString(minDate, "yyyy");
return months;
}catch(ParseException pe) {
pe.printStackTrace();
}
return new String[0];
}
/**
* 计算两个日期之间相差的天数
* @param smdate 较小的时间
* @param bdate 较大的时间
* @return 相差天数
* @throws ParseException
*/
public static int daysBetween(Date smdate,Date bdate) {
try{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
smdate=sdf.parse(sdf.format(smdate));
bdate=sdf.parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days));
}catch(Exception e) {
e.printStackTrace();
}
return -1;
}
/**
*字符串的日期格式的计算
*/
public static int daysBetween(String smdate,String bdate){
try{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days));
}catch(ParseException e) {
e.printStackTrace();
}
return -1;
}
public static void main(String[] args){
Date d = DateFormat.formatDate("2016-12-29", "yyyy-MM-dd");
System.out.print(DateFormat.formatString(DateFormat.addWorkDays(d, 7), "yyyy-MM-dd"));
}
}