-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateHandler.java
345 lines (308 loc) · 10.1 KB
/
DateHandler.java
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package com.gootrip.util;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2007</p>
* <p>Company: </p>
* @author <a href="mailto:royiwu@hotmail.com">advance.wu</a>
* @version 1.0
*/
import java.util.*;
import java.text.*;
public class DateHandler {
public DateHandler() {
}
public static int openDay=5;
private String iDate="";
private int iYear;
private int iMonth;
private int iDay;
// iDateTime = 2002-01-01 23:23:23
public void setDate(String iDateTime){
this.iDate=iDateTime.substring(0,10);
}
public String getDate(){
return this.iDate;
}
public int getYear(){
iYear=Integer.parseInt(iDate.substring(0,4));
return iYear;
}
public int getMonth(){
iMonth=Integer.parseInt(iDate.substring(5,7));
return iMonth;
}
public int getDay(){
iDay=Integer.parseInt(iDate.substring(8,10));
return iDay;
}
public static String subDate(String date){
return date.substring(0,10);
}
/**
* 计算是否是季度末
* @param date
* @return
*/
public static boolean isSeason(String date){
int getMonth = Integer.parseInt(date.substring(5,7));
boolean sign = false;
if (getMonth==3)
sign = true;
if (getMonth==6)
sign = true;
if (getMonth==9)
sign = true;
if (getMonth==12)
sign = true;
return sign;
}
/**
* 计算从现在开始几天后的时间
* @param afterDay
* @return
*/
public static String getDateFromNow(int afterDay){
GregorianCalendar calendar = new GregorianCalendar();
Date date = calendar.getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+afterDay);
date = calendar.getTime();
return df.format(date);
}
/**
* 带格式
* @param afterDay
* @param format_string
* @return
*/
public static String getDateFromNow(int afterDay, String format_string)
{
Calendar calendar = Calendar.getInstance();
Date date = null;
DateFormat df = new SimpleDateFormat(format_string);
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + afterDay);
date = calendar.getTime();
return df.format(date);
}
/**
* 得到当前时间,用于文件名,没有特殊字符,使用yyyyMMddHHmmss格式
* @param afterDay
* @return
* by tim
*/
public static String getNowForFileName(int afterDay){
GregorianCalendar calendar = new GregorianCalendar();
// Date date = calendar.getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+afterDay);
Date date = calendar.getTime();
return df.format(date);
}
//==============================================================================
//比较日期,与现在-N天的日期对比 -1 0 1
//==============================================================================
public int getDateCompare(String limitDate,int afterDay){
GregorianCalendar calendar = new GregorianCalendar();
Date date = calendar.getTime();
calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+afterDay);
date = calendar.getTime();//date是新来的天数,跟今天相比的天数
iDate=limitDate;
calendar.set(getYear(),getMonth()-1,getDay());
Date dateLimit = calendar.getTime();
return dateLimit.compareTo(date);
}
//==============================================================================
//比较日期,与现在的日期对比
//==============================================================================
public int getDateCompare(String limitDate){
iDate=limitDate;
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(getYear(),getMonth()-1,getDay());
Date date = calendar.getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date nowDate = new Date();
return date.compareTo(nowDate);
}
//==============================================================================
//比较日期,与现在的日期对比 得到天数
//==============================================================================
public long getLongCompare(String limitDate){
iDate=limitDate;
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(getYear(),getMonth()-1,getDay());
Date date = calendar.getTime();
long datePP=date.getTime();
Date nowDate = new Date();
long dateNow = nowDate.getTime();
return ((dateNow-datePP)/(24*60*60*1000));
}
//==============================================================================
//比较日期,与现在的日期对比 得到信息
//==============================================================================
public String getStringCompare(String limitDate,int openDay){
iDate=limitDate;
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(getYear(),getMonth()-1,getDay());
Date date = calendar.getTime();
long datePP=date.getTime();
Date nowDate = new Date();
long dateNow = nowDate.getTime();
long overDay = ((dateNow-datePP)/(24*60*60*1000));
String info="";
return info;
}
//==============================================================================
//比较日期,与现在的日期对比 得到信息
//==============================================================================
public String getPicCompare(String limitDate,int openDay){
iDate=limitDate;
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(getYear(),getMonth()-1,getDay());
Date date = calendar.getTime();
long datePP=date.getTime();
Date nowDate = new Date();
long dateNow = nowDate.getTime();
long overDay = ((dateNow-datePP)/(24*60*60*1000));
String info="";
if (overDay>0){
info="plaint1.gif";
}
if (overDay==0){
info="plaint2.gif";
}
if (overDay<0&&overDay>=-openDay){
info="plaint2.gif";
}
if (overDay<-openDay){
info="plaint3.gif";
}
if (overDay<-150){
info="plaint3.gif";
}
return info;
}
/**
* method diffdate 计算两个日期间相隔的日子
* @param beforDate 格式:2005-06-20
* @param afterDate 格式:2005-06-21
* @return
*/
public static int diffDate(String beforeDate,String afterDate){
String[] tt = beforeDate.split("-");
Date firstDate = new Date(Integer.parseInt(tt[0]),Integer.parseInt(tt[1])-1,Integer.parseInt(tt[2]));
tt = afterDate.split("-");
Date nextDate = new Date(Integer.parseInt(tt[0]),Integer.parseInt(tt[1])-1,Integer.parseInt(tt[2]));
return (int)(nextDate.getTime()-firstDate.getTime())/(24*60*60*1000);
}
/**
* 获取今天的日期的字符串
* @return
*/
public static String getToday(){
Calendar cld=Calendar.getInstance();
java.util.Date date=new Date();
cld.setTime(date);
int intMon=cld.get(Calendar.MONTH)+1;
int intDay=cld.get(Calendar.DAY_OF_MONTH);
String mons=String.valueOf(intMon);
String days=String.valueOf(intDay);
if(intMon<10)
mons="0"+String.valueOf(intMon);
if(intDay<10)
days="0"+String.valueOf(intDay);
return String.valueOf(cld.get(Calendar.YEAR))+"-"+mons+"-"+days;
}
/**
* 获取当前月份
* @return 返回字符串 格式:两位数
*/
public static String getCurrentMonth(){
String strmonth = null;
Calendar cld = Calendar.getInstance();
java.util.Date date = new Date();
cld.setTime(date);
int intMon=cld.get(Calendar.MONTH) + 1;
if(intMon<10)
strmonth = "0" + String.valueOf(intMon);
else
strmonth = String.valueOf(intMon);
date = null;
return strmonth;
}
// public static String getCurrMonth()
// {
// Calendar cld=Calendar.getInstance();
// java.util.Date date=new Date();
// cld.setTime(date);
// int intMon=cld.get(Calendar.MONTH)+1;
//
// return String.valueOf(intMon).toString();
// }
/**
* 获取昨天的日期的字符串
*/
public static String getYestoday(){
Calendar cld = Calendar.getInstance();
java.util.Date date = new Date();
cld.setTime(date);
cld.add(Calendar.DATE,-1);
int intMon = cld.get(Calendar.MONTH)+1;
int intDay = cld.get(Calendar.DAY_OF_MONTH);
String mons = String.valueOf(intMon);
String days = String.valueOf(intDay);
if(intMon < 10)
mons="0" + String.valueOf(intMon);
if(intDay < 10)
days = "0" + String.valueOf(intDay);
return String.valueOf(cld.get(Calendar.YEAR)) + "-" + mons + "-" + days;
}
/**
* 此函数用来计算员工的工作天数,如在使用期和离职期该月份的工作日
* 输入(离职日期,-1)可得该月工作天数 时间以2002-12-14为准
* 输入(入司时间,1)可的该月工作天数
*/
public static int getWorkDay(String date , int sign){
int month=0;
int week=0;
int workDay=0;
Calendar rightNow = Calendar.getInstance();
DateHandler dateOver=new DateHandler();
dateOver.setDate(date);
rightNow.set(rightNow.YEAR,dateOver.getYear());
rightNow.set(rightNow.MONTH,dateOver.getMonth()-1);
rightNow.set(rightNow.DATE,dateOver.getDay());
month = rightNow.get(rightNow.MONTH);
while(rightNow.get(rightNow.MONTH)==month){
week=rightNow.get(Calendar.DAY_OF_WEEK);
if (week==1||week==7){
}else{
workDay++;
System.out.println(rightNow.get(rightNow.DATE));
}
rightNow.add(rightNow.DATE,sign);
}
return workDay;
}
public static void main(String args[]){
System.out.println(DateHandler.isSeason("2002-03-02"));
// String cc ="100.123.342";
// System.out.println(cc.indexOf(".",3));
//
// StringTokenizer st=new StringTokenizer(cc,".");
//
// if (st.countTokens()!=2) {
//
// String state = st.nextToken();
// String event = st.nextToken();
// System.out.println(""+event);
String strDate = DateHandler.getDateFromNow(0,"yyyy-MM-dd HH:mm:ss");
System.out.println("date:" + strDate);
System.out.println("15:" + strDate.substring(0,16));
Date firstDate = new Date(2006,11,14,18,3,0);
Date nextDate = new Date(2006,11,15,18,2,0);
System.out.println("date's num: " + (int)(nextDate.getTime()-firstDate.getTime())/(24*60*60*1000));
// }
//System.out.println(DateHandler.getWorkDay("2002-11-14",-1));
}
}