1 /**
2 * 根据年月日,获取是周几
3 * @param year
4 * @param month
5 * @param day
6 * @return
7 */
8 public static String CaculateWeekDay(int y,int m,int d)
9 {
10
11 String strDate = y+"-"+m+"-"+d;// 定义日期字符串
12 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");// 定义日期格式
13 Date date = null;
14 try {
15 date = format.parse(strDate);// 将字符串转换为日期
16 }catch (ParseException e) {
17 }
18 Calendar c = Calendar.getInstance();
19 c.setTime(date);
20 int dayForWeek = 0;
21 if(c.get(Calendar.DAY_OF_WEEK) == 1){
22 dayForWeek = 7;
23 }else{
24 dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
25 }
26 return Integer.toString(dayForWeek);
27
28 }
29
30
31 /**
32 * 获取今日的日期,举例:20141110,20140110
33 * @return,日期的字符串
34 */
35 public static String getNowDateString()
36 {
37 Date date = new Date();
38 //年份
39 int year = date.getYear() + 1900;
40 //月份
41 int month = date.getMonth() + 1;
42 //日期
43 int day = date.getDate();
44
45 String date_string ;
46 if(month < 10)
47 {
48 if(day < 10)
49 {
50 date_string = Integer.toString(year)+"0"+Integer.toString(month)+"0"+Integer.toString(day);
51 }
52 else
53 {
54 date_string = Integer.toString(year)+"0"+Integer.toString(month)+Integer.toString(day);
55 }
56
57 }
58 else
59 {
60 if(day < 10)
61 {
62 date_string = Integer.toString(year)+Integer.toString(month)+"0"+Integer.toString(day);
63 }
64 else
65 {
66 date_string = Integer.toString(year)+Integer.toString(month)+Integer.toString(day);
67 }
68
69 }
70 return date_string;
71 }
72
73
74 /**
75 * 获取今日包含时间点的日期,举例:2014020202(2014年2月2号2点)
76 * @return,日期的字符串
77 */
78 public static String getNowDateAndHourString()
79 {
80
81 String date_string ;
82 String month_str;
83 String day_str;
84 String hour_str;
85
86 Date date = new Date();
87 //年份
88 int year = date.getYear() + 1900;
89 //月份
90 int month = date.getMonth() + 1;
91 //日期
92 int day = date.getDate();
93 //时间
94 int hour = date.getHours();
95
96 //处理月份
97 if(month < 10)
98 {
99 month_str=0+Integer.toString(month);
100 }
101 else
102 {
103 month_str=Integer.toString(month);
104 }
105
106 //处理日期
107 if(day < 10)
108 {
109 day_str=0+Integer.toString(day);
110 }
111 else
112 {
113 day_str=Integer.toString(day);
114 }
115
116 //处理时间
117 if(hour < 10)
118 {
119 hour_str=0+Integer.toString(hour);
120 }
121 else
122 {
123 hour_str=Integer.toString(hour);
124 }
125
126 date_string = year+month_str+day_str+hour_str;
127 return date_string;
128 }
129
130
131 /**
132 * 函数功能是根据当前的日期,当step为正数时,算出加上step后的日期;step为负数时,算出减去step后的日期
133 * @param step:要加或者减的天数
134 * @return :在当前日期的基础上加上或者减去step后的日期
135 */
136 public static String getDate(int step) {
137 Calendar cal = Calendar.getInstance();
138 cal.add(Calendar.DATE, step);
139 SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
140
141 String str = sf.format(cal.getTime());
142
143 System.out.print(str);
144 return str;
145 }
146
147
148 /**
149 *根据距离今天的日期差,得到是第几周周几:举例说今天是本年的第三周周四,那么getWeek(1)得到便是"第三周>周五"
150 * @param step
151 * @return
152 */
153 public static String getWeek(int step)
154 {
155 Calendar cal = Calendar.getInstance();
156 cal.add(Calendar.DATE, step);
157
158 int week = cal.get(cal.WEEK_OF_YEAR);
159 int day = cal.get(cal.DAY_OF_WEEK);
160
161 if(day == 1)
162 {
163 week = week -1;
164 }
165 day = day-1;
166
167 String [] day_of_week = {"日","一","二","三","四","五","六","日"};
168
169 if(week == 0)
170 {
171 return "周"+day_of_week[day];
172 }
173
174 String str = "第"+week+"周"+">周"+day_of_week[day];
175 CommonUtils.LogWuwei(tag, "getWeek week is "+str+"day is "+day);
176 return str;
177 }
178
179
180 /**
181 * 根据距离今天的日期差,得到是周几:举例说今天是周二,那么getWeekDay(-1)得到的便是“周一”
182 * @param step
183 * @return
184 */
185 public static String getWeekDay(int step)
186 {
187 Calendar cal = Calendar.getInstance();
188 cal.add(Calendar.DATE, step);
189
190 int week = cal.get(cal.WEEK_OF_YEAR);
191 int day = cal.get(cal.DAY_OF_WEEK);
192
193 if(day == 1)
194 {
195 week = week -1;
196 }
197 day = day-1;
198
199 String [] day_of_week = {"日","一","二","三","四","五","六","日"};
200
201 return "周"+day_of_week[day];
202
203 }
204
205
206 /**
207 * 获取当前日期是星期几<br>
208 *
209 * @param dt
210 * @return 当前日期是星期几
211 */
212 public static String getWeekOfDate() {
213 Date date=new Date();
214 SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
215 dateFm.format(date);
216
217 String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
218 Calendar cal = Calendar.getInstance();
219 cal.setTime(date);
220 int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
221 if (w < 0)
222 w = 0;
223 CommonUtils.LogWuwei(tag,"今天是 "+weekDays[w]);
224 return weekDays[w];
225 }
226
227
228 /**
229 * 根据日期获取时间戳
230 * @param normalDate 20141228
231 * @return 1419696000
232 */
233 public static long getTimeStamp(Long normalDate)
234 {
235 Long detail_date_tmp = normalDate;
236 String date_str = Long.toString(detail_date_tmp);
237 String year = date_str.substring(0, 4);
238 String month = date_str.substring(4, 6);
239 String day = date_str.substring(6, 8);
240 //CommonUtils.LogWuwei(tag,"detail_date_tmp is "+detail_date_tmp+" "+ year+":"+month+":"+day);
241
242 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
243 Date date;
244 Calendar cal= Calendar.getInstance();
245 String parseStr = year+"-"+month+"-"+day;
246
247 try {
248 date = df.parse(parseStr);
249 cal = Calendar.getInstance();
250 cal.setTime(date);
251 } catch (ParseException e) {
252 // TODO Auto-generated catch block
253 e.printStackTrace();
254 }
255
256 long timeStamp = cal.getTimeInMillis()/1000;
257
258 CommonUtils.LogWuwei(tag, "timeStamp is "+timeStamp);
259
260 return timeStamp;
261 }
262
263
264 /**
265 * 将”20141224” 解析成“2014年12月24日”
266 * @param date
267 * @return
268 */
269 public static String parseDate(Long date)
270 {
271 int year = (int)(date/10000);
272 int month = (int)(date%10000)/100;
273 int day = (int)(date%10000)%100;
274 return year+"年"+month+"月"+day+"日";
275
276 }
277
278
279 /**
280 * 获取当前日期是星期几<br>
281 *
282 * @param dt
283 * @return 当前日期是星期几
284 */
285 public static int getIntWeekOfDate() {
286 Date date=new Date();
287 SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
288 dateFm.format(date);
289
290 int[] weekDays = {7,1,2,3,4,5,6};
291 Calendar cal = Calendar.getInstance();
292 cal.setTime(date);
293 int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
294 if (w < 0)
295 w = 0;
296 // CommonUtils.LogWuwei(tag,"今天是 "+weekDays[w]);
297 return weekDays[w];
298 }
299
300 /**
301 * 根据今天是周几获取上周周一到周日的日期
302 */
303 public static String[] getLastWeekDate()
304 {
305 int today = CommonUtils.getIntWeekOfDate();//获取今天周几
306 String baseDate = "";//初始化上周一的日期
307 String date[] = {null,null,null,null,null,null,null};
308 int index = 0;
309 switch (today)////得到上周一的日期
310 {
311 case 1:
312 baseDate = CommonUtils.getDate(-7);
313 index = -7;
314 break;
315 case 2:
316 baseDate = CommonUtils.getDate(-8);
317 index = -8;
318 break;
319 case 3:
320 baseDate = CommonUtils.getDate(-9);
321 index = -9;
322 break;
323 case 4:
324 baseDate = CommonUtils.getDate(-10);
325 index = -10;
326 break;
327 case 5:
328 baseDate = CommonUtils.getDate(-11);
329 index = -11;
330 break;
331 case 6:
332 baseDate = CommonUtils.getDate(-12);
333 index = -12;
334 break;
335 case 7:
336 baseDate = CommonUtils.getDate(-13);
337 index = -13;
338 break;
339 }
340
341 for(int i=0;i<7;i++)
342 {
343 date[i] = CommonUtils.getDate(index+i);
344 int k = i+1;
345 CommonUtils.LogWuwei(tag,"上周"+k+" is "+date[i]);
346 }
347 return date;
348 }
349
350 /**
351 * 根据本周周一到周日的日期
352 */
353 public static String[] getThisWeekDate()
354 {
355 int today = CommonUtils.getIntWeekOfDate();//获取今天周几
356 String date[] = {null,null,null,null,null,null,null};
357 int index = 0;
358 switch (today)////得到上周一的日期
359 {
360 case 1:
361 index = 0;
362 break;
363 case 2:
364 index = -1;
365 break;
366 case 3:
367 index = -2;
368 break;
369 case 4:
370 index = -3;
371 break;
372 case 5:
373 index = -4;
374 break;
375 case 6:
376 index = -5;
377 break;
378 case 7:
379 index = -6;
380 break;
381 }
382
383 for(int i=0;i<7;i++)
384 {
385 date[i] = CommonUtils.getDate(index+i);
386 int k = i+1;
387 CommonUtils.LogWuwei(tag,"本周"+k+" is "+date[i]);
388 }
389 return date;
390 }
391
392
393 /**
394 * 获取下周周一到周日的日期
395 */
396 public static String[] getNextWeekDate()
397 {
398 int today = CommonUtils.getIntWeekOfDate();//获取今天周几
399 String date[] = {null,null,null,null,null,null,null};
400 int index = 0;
401 switch (today)////得到下周的日期
402 {
403 case 1:
404 index = 7;
405 break;
406 case 2:
407 index = 6;
408 break;
409 case 3:
410 index = 5;
411 break;
412 case 4:
413 index = 4;
414 break;
415 case 5:
416 index = 3;
417 break;
418 case 6:
419 index = 2;
420 break;
421 case 7:
422 index = 1;
423 break;
424 }
425
426 for(int i=0;i<7;i++)
427 {
428 date[i] = CommonUtils.getDate(index+i);
429 int k = i+1;
430 CommonUtils.LogWuwei(tag,"下周"+k+" is "+date[i]);
431 }
432 return date;
433 }
434
435
436 /**
437 * 从今天起往前寻找8周内每个周一的日期
438 */
439 public static long[] checkRecentEfficientDataFromToday()
440 {
441 long modayData[] = new long[8];
442 int today = getIntWeekOfDate();//获取今天是周几
443 int step = 0;
444 switch (today)
445 {
446 case 1:
447 step = 0;
448 break;
449 case 2:
450 step = -1;
451 break;
452 case 3:
453 step = -2;
454 break;
455 case 4:
456 step = -3;
457 break;
458 case 5:
459 step = -4;
460 break;
461 case 6:
462 step = -5;
463 break;
464 case 7:
465 step = -6;
466 break;
467 }
468
469 for(int i=0;i<8;i++)
470 {
471 modayData[i] = Long.parseLong(getDate(step-7*i));
472 CommonUtils.LogWuwei(tag, "monday date is "+modayData[i]);
473 }
474 return modayData;
475 }
476
477 /**
478 * 获取到本周一的日期
479 * @return
480 */
481 public static long getThisMonday()
482 {
483 int today = getIntWeekOfDate();//获取今天是周几
484 long monday = (long)0;
485 int step = 0;
486 switch (today)
487 {
488 case 1:
489 step = 0;
490 break;
491 case 2:
492 step = -1;
493 break;
494 case 3:
495 step = -2;
496 break;
497 case 4:
498 step = -3;
499 break;
500 case 5:
501 step = -4;
502 break;
503 case 6:
504 step = -5;
505 break;
506 case 7:
507 step = -6;
508 break;
509 }
510 monday = Long.parseLong(getNowDateString())+step;
511 return monday;
512 }
513
514 /**
515 * 获取到上周的周一是几号
516 * @return
517 */
518 public static long getLastMonday()
519 {
520 int today = getIntWeekOfDate();//获取今天是周几
521 long monday = (long)0;
522 int step = 0;
523 switch (today)
524 {
525 case 1:
526 step = 0;
527 break;
528 case 2:
529 step = -1;
530 break;
531 case 3:
532 step = -2;
533 break;
534 case 4:
535 step = -3;
536 break;
537 case 5:
538 step = -4;
539 break;
540 case 6:
541 step = -5;
542 break;
543 case 7:
544 step = -6;
545 break;
546 }
547 monday = Long.parseLong(getNowDateString())-7+step;
548 return monday;
549 }
550
551 /**
552 * 获取到下周的周一是几号
553 * @return
554 */
555 public static long getNextMonday()
556 {
557 int today = getIntWeekOfDate();//获取今天是周几
558 long monday = (long)0;
559 int step = 0;
560 switch (today)
561 {
562 case 1:
563 step = 0;
564 break;
565 case 2:
566 step = -1;
567 break;
568 case 3:
569 step = -2;
570 break;
571 case 4:
572 step = -3;
573 break;
574 case 5:
575 step = -4;
576 break;
577 case 6:
578 step = -5;
579 break;
580 case 7:
581 step = -6;
582 break;
583 }
584 monday = Long.parseLong(getNowDateString())+7+step;
585 return monday;
586 }
587