主頁 > 後端開發 > Java 日期大小對比及日期轉換

Java 日期大小對比及日期轉換

2020-10-02 11:16:09 後端開發

宣告
1)該文章整理自網上的大牛和專家無私奉獻的資料,具體參考的資料請看參考文獻,
2)本文僅供學術交流,非商用,如果某部分不小心侵犯了大家的利益,還望海涵,并聯系博主洗掉,
3)博主才疏學淺,文中如有不當之處,請各位指出,共同進步,謝謝,
4)此屬于第一版本,若有錯誤,還需繼續修正與增刪,還望大家多多指點,大家都共享一點點,一起為祖國科研的推進添磚加瓦,

Java 字串型的日期對比大小

 1 /** 
 2  * @description: 兩個String型別,按照日期格式對比 
 3  */  
 4 public static int compareTime(String dateOne, String dateTwo , String dateFormatType){  
 5       
 6     DateFormat df = new SimpleDateFormat(dateFormatType);  
 7     Calendar calendarStart = Calendar.getInstance();  
 8     Calendar calendarEnd = Calendar.getInstance();  
 9       
10     try {  
11         calendarStart.setTime(df.parse(dateOne));  
12         calendarEnd.setTime(df.parse(dateTwo));  
13     } catch (ParseException e) {  
14         e.printStackTrace();  
15         return 100;  
16     }  
17   
18     int result = calendarStart.compareTo(calendarEnd);  
19     if(result > 0){  
20         result = 1;  
21     }else if(result < 0){  
22         result = -1;  
23     }else{  
24         result = 0 ;  
25     }  
26     return result ;  
27 }  

java int型日期轉為date或特定格式

 1 Calendar c=Calendar.getInstance();
 2 int seconds = 1514871613;//資料庫中提取的資料
 3 long millions=new Long(seconds).longValue()*1000;
 4 c.setTimeInMillis(millions);
 5 System.out.println(""+c.getTime());
 6 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 7 String dateString = sdf.format(c.getTime());
 8 System.out.println(dateString);
 9 
10 
11 根據以上例子實作int資料轉DATE
12 
13 1:SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
14 //format套工具類方法進行賦值
15    String aInteger = sdf.format(DateFormatUtil.intFormatDate(hAlarm.getAttime()));
16    date.setAttime(aInteger);

java 日期轉換

  1 java 日期轉換
  2 package com.hoperun.self.supermarket.common;
  3 
  4 import java.text.ParseException;
  5 import java.text.SimpleDateFormat;
  6 import java.util.Calendar;
  7 import java.util.Date;
  8 import java.util.GregorianCalendar;
  9 
 10 import java.sql.Timestamp;
 11 import java.util.ArrayList;
 12 import java.util.List;
 13 
 14 public class DateFormatUtil {
 15     
 16     /**
 17      * 獲取當前的時間 yyyy-MM-dd HH:mm:ss
 18      * @return
 19      * @throws ParseException
 20      */
 21     public static Date getNow(){
 22         Date date = new Date();
 23         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 24         String createdate = sdf.format(date);
 25         Date date2 = null;
 26         try {
 27             date2 = sdf.parse(createdate);
 28         } catch (ParseException e) {
 29             e.printStackTrace();
 30         }
 31         return date2;
 32     }
 33     
 34     /**
 35      * 獲取當前的時間 yyyy-MM-dd
 36      * @return
 37      */
 38     public static Date getNowDay(){
 39         Date date = new Date();
 40         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 41         String createdate = sdf.format(date);
 42         Date date2 = null;
 43         try {
 44             date2 = sdf.parse(createdate);
 45         } catch (ParseException e) {
 46             e.printStackTrace();
 47         }
 48         return date2;
 49     }
 50     
 51     /**
 52      * 獲取當前的時間 yyyy-MM-dd
 53      * @return
 54      */
 55     public static String getNowDayStr(){
 56         Date date = new Date();
 57         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 58         String createdate = sdf.format(date);
 59         return createdate;
 60     }
 61     
 62     /**
 63      * 獲取當前時間 yyyyMMddHHmmss
 64      * @return
 65      */
 66     public static String getNowDayStr2(){
 67         Date date = new Date();
 68         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
 69         String createdate = sdf.format(date);
 70         return createdate;
 71     }
 72     
 73     /**
 74      * 獲取前一天的時間 yyyy-MM-dd
 75      * @return
 76      */
 77     public static String getLastDayStr() {
 78         Calendar ca = Calendar.getInstance();
 79         ca.setTime(new Date()); 
 80         ca.add(Calendar.DATE, -1);
 81         Date lastDay = ca.getTime(); //結果
 82         SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd"); 
 83         String lastDayStr = sf.format(lastDay);
 84         return lastDayStr;
 85     }
 86     
 87     /**
 88      * 獲取后一天的時間 yyyy-MM-dd
 89      * @return
 90      */
 91     public static String getAfterDayStr() {
 92         Calendar ca = Calendar.getInstance();
 93         ca.setTime(new Date()); 
 94         ca.add(Calendar.DATE, 1);
 95         Date lastDay = ca.getTime(); //結果
 96         SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd"); 
 97         String lastDayStr = sf.format(lastDay);
 98         return lastDayStr;
 99     }
100 
101     /**
102      * 字串轉為日期格式
103      * 
104      * @param dateString
105      * @return
106      * @throws ParseException
107      */
108     public static Date stringFormatDate(String dateString){
109         SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
110         Date date = null;
111         try {
112             date = bartDateFormat.parse(dateString);
113         } catch (ParseException e) {
114             e.printStackTrace();
115         }
116         return date;
117     }
118 
119     /**
120      * 字串轉為日期格式
121      * 
122      * @param dateString
123      * @return
124      * @throws ParseException
125      */
126     public static Date stringFormatDateTime(String dateString){
127         SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
128         Date date = null;
129         try {
130             date = bartDateFormat.parse(dateString);
131         } catch (ParseException e) {
132             e.printStackTrace();
133         }
134         return date;
135     }
136 
137     /**
138      * 字串轉為日期格式
139      * 
140      * @param dateString
141      * @return
142      * @throws ParseException
143      */
144     public static Date stringFormatDateTimeNoSecond(String dateString) throws ParseException {
145         SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
146         Date date = bartDateFormat.parse(dateString);
147         return date;
148     }
149 
150     /**
151      * 字串轉為日期格式
152      * 
153      * @param dateString
154      * @return
155      * @throws ParseException
156      */
157     public static Date stringFormatDateTime2(String dateString) throws ParseException {
158         SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
159         Date date = bartDateFormat.parse(dateString);
160         return date;
161     }
162 
163     /**
164      * 將時間格式化為含時分秒的字串
165      * 
166      * @param date
167      * @return
168      * @throws ParseException
169      */
170     public static String dateTimeFormatString(Date date){
171         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
172         return dateFormat.format(date);
173     }
174 
175     /**
176      * 將時間格式化為不含時分秒的字串
177      * 
178      * @param date
179      * @return
180      * @throws ParseException
181      */
182     public static String dateFormatString(Date date) throws ParseException {
183         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
184         return dateFormat.format(date);
185     }
186 
187     /**
188      * 將時間格式化為不含時分秒的字串MM/dd
189      * 
190      * @param date
191      * @return
192      * @throws ParseException
193      */
194     public static String dateFormatString2(Date date) throws ParseException {
195         SimpleDateFormat dateFormat = new SimpleDateFormat("M/dd");
196         return dateFormat.format(date);
197     }
198 
199     /**
200      * 將時間格式化為不含時分秒的字串MM
201      * 
202      * @param date
203      * @return
204      * @throws ParseException
205      */
206     public static String dateFormatString3(Date date) throws ParseException {
207         SimpleDateFormat dateFormat = new SimpleDateFormat("M");
208         return dateFormat.format(date);
209     }
210 
211     /**
212      * 將int型日期轉換為日期
213      * 
214      * @param dateInt
215      * @return
216      */
217     public static Date intFormatDate(int dateInt) {
218         Calendar c = Calendar.getInstance();
219         long millions = new Long(dateInt).longValue() * 1000;
220         c.setTimeInMillis(millions);
221         return c.getTime();
222     }
223 
224 
225     // 獲取當天的開始時間
226     public static Date getDayBegin() {
227         Calendar cal = new GregorianCalendar();
228         cal.set(Calendar.HOUR_OF_DAY, 0);
229         cal.set(Calendar.MINUTE, 0);
230         cal.set(Calendar.SECOND, 0);
231         cal.set(Calendar.MILLISECOND, 0);
232         return cal.getTime();
233     }
234 
235     // 獲取當天的結束時間
236     public static Date getDayEnd() {
237         Calendar cal = new GregorianCalendar();
238         cal.set(Calendar.HOUR_OF_DAY, 23);
239         cal.set(Calendar.MINUTE, 59);
240         cal.set(Calendar.SECOND, 59);
241         return cal.getTime();
242     }
243 
244     // 獲取昨天的開始時間
245     public static Date getBeginDayOfYesterday() {
246         Calendar cal = new GregorianCalendar();
247         cal.setTime(getDayBegin());
248         cal.add(Calendar.DAY_OF_MONTH, -1);
249         return cal.getTime();
250     }
251 
252     // 獲取昨天的結束時間
253     public static Date getEndDayOfYesterDay() {
254         Calendar cal = new GregorianCalendar();
255         cal.setTime(getDayEnd());
256         cal.add(Calendar.DAY_OF_MONTH, -1);
257         return cal.getTime();
258     }
259 
260     // 獲取前天的開始時間
261     public static Date getBeginDayOfYesterday2() {
262         Calendar cal = new GregorianCalendar();
263         cal.setTime(getDayBegin());
264         cal.add(Calendar.DAY_OF_MONTH, -2);
265         return cal.getTime();
266     }
267 
268     // 獲取前天的結束時間
269     public static Date getEndDayOfYesterDay2() {
270         Calendar cal = new GregorianCalendar();
271         cal.setTime(getDayEnd());
272         cal.add(Calendar.DAY_OF_MONTH, -2);
273         return cal.getTime();
274     }
275 
276     // 獲取前3天的開始時間
277     public static Date getBeginDayOfYesterday3() {
278         Calendar cal = new GregorianCalendar();
279         cal.setTime(getDayBegin());
280         cal.add(Calendar.DAY_OF_MONTH, -3);
281         return cal.getTime();
282     }
283 
284     // 獲取前3天的結束時間
285     public static Date getEndDayOfYesterDay3() {
286         Calendar cal = new GregorianCalendar();
287         cal.setTime(getDayEnd());
288         cal.add(Calendar.DAY_OF_MONTH, -3);
289         return cal.getTime();
290     }
291 
292     // 獲取前4天的開始時間
293     public static Date getBeginDayOfYesterday4() {
294         Calendar cal = new GregorianCalendar();
295         cal.setTime(getDayBegin());
296         cal.add(Calendar.DAY_OF_MONTH, -4);
297         return cal.getTime();
298     }
299 
300     // 獲取前4天的結束時間
301     public static Date getEndDayOfYesterDay4() {
302         Calendar cal = new GregorianCalendar();
303         cal.setTime(getDayEnd());
304         cal.add(Calendar.DAY_OF_MONTH, -4);
305         return cal.getTime();
306     }
307 
308     // 獲取前5天的開始時間
309     public static Date getBeginDayOfYesterday5() {
310         Calendar cal = new GregorianCalendar();
311         cal.setTime(getDayBegin());
312         cal.add(Calendar.DAY_OF_MONTH, -5);
313         return cal.getTime();
314     }
315 
316     // 獲取前5天的結束時間
317     public static Date getEndDayOfYesterDay5() {
318         Calendar cal = new GregorianCalendar();
319         cal.setTime(getDayEnd());
320         cal.add(Calendar.DAY_OF_MONTH, -5);
321         return cal.getTime();
322     }
323 
324     // 獲取前6天的開始時間
325     public static Date getBeginDayOfYesterday6() {
326         Calendar cal = new GregorianCalendar();
327         cal.setTime(getDayBegin());
328         cal.add(Calendar.DAY_OF_MONTH, -6);
329         return cal.getTime();
330     }
331 
332     // 獲取前6天的結束時間
333     public static Date getEndDayOfYesterDay6() {
334         Calendar cal = new GregorianCalendar();
335         cal.setTime(getDayEnd());
336         cal.add(Calendar.DAY_OF_MONTH, -6);
337         return cal.getTime();
338     }
339 
340     // 獲取明天的開始時間
341     public static Date getBeginDayOfTomorrow() {
342         Calendar cal = new GregorianCalendar();
343         cal.setTime(getDayBegin());
344         cal.add(Calendar.DAY_OF_MONTH, 1);
345 
346         return cal.getTime();
347     }
348 
349     // 獲取明天的結束時間
350     public static Date getEndDayOfTomorrow() {
351         Calendar cal = new GregorianCalendar();
352         cal.setTime(getDayEnd());
353         cal.add(Calendar.DAY_OF_MONTH, 1);
354         return cal.getTime();
355     }
356 
357     // 獲取本周的開始時間
358     @SuppressWarnings("unused")
359     public static Date getBeginDayOfWeek() {
360         Date date = new Date();
361         if (date == null) {
362             return null;
363         }
364         Calendar cal = Calendar.getInstance();
365         cal.setTime(date);
366         int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
367         if (dayofweek == 1) {
368             dayofweek += 7;
369         }
370         cal.add(Calendar.DATE, 2 - dayofweek);
371         return getDayStartTime(cal.getTime());
372     }
373 
374     // 獲取本周的結束時間
375     public static Date getEndDayOfWeek() {
376         Calendar cal = Calendar.getInstance();
377         cal.setTime(getBeginDayOfWeek());
378         cal.add(Calendar.DAY_OF_WEEK, 6);
379         Date weekEndSta = cal.getTime();
380         return getDayEndTime(weekEndSta);
381     }
382 
383     // 獲取上周的開始時間
384     @SuppressWarnings("unused")
385     public static Date getBeginDayOfLastWeek() {
386         Date date = new Date();
387         if (date == null) {
388             return null;
389         }
390         Calendar cal = Calendar.getInstance();
391         cal.setTime(date);
392         int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
393         if (dayofweek == 1) {
394             dayofweek += 7;
395         }
396         cal.add(Calendar.DATE, 2 - dayofweek - 7);
397         return getDayStartTime(cal.getTime());
398     }
399 
400     // 獲取上周的結束時間
401     public static Date getEndDayOfLastWeek() {
402         Calendar cal = Calendar.getInstance();
403         cal.setTime(getBeginDayOfLastWeek());
404         cal.add(Calendar.DAY_OF_WEEK, 6);
405         Date weekEndSta = cal.getTime();
406         return getDayEndTime(weekEndSta);
407     }
408 
409     // 獲取上2周的開始時間
410     @SuppressWarnings("unused")
411     public static Date getBeginDayOfLast2Week() {
412         Date date = new Date();
413         if (date == null) {
414             return null;
415         }
416         Calendar cal = Calendar.getInstance();
417         cal.setTime(date);
418         int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
419         if (dayofweek == 1) {
420             dayofweek += 7;
421         }
422         cal.add(Calendar.DATE, 2 - dayofweek - 14);
423         return getDayStartTime(cal.getTime());
424     }
425 
426     // 獲取上2周的結束時間
427     public static Date getEndDayOfLast2Week() {
428         Calendar cal = Calendar.getInstance();
429         cal.setTime(getBeginDayOfLast2Week());
430         cal.add(Calendar.DAY_OF_WEEK, 6);
431         Date weekEndSta = cal.getTime();
432         return getDayEndTime(weekEndSta);
433     }
434 
435     // 獲取上3周的開始時間
436     @SuppressWarnings("unused")
437     public static Date getBeginDayOfLast3Week() {
438         Date date = new Date();
439         if (date == null) {
440             return null;
441         }
442         Calendar cal = Calendar.getInstance();
443         cal.setTime(date);
444         int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
445         if (dayofweek == 1) {
446             dayofweek += 7;
447         }
448         cal.add(Calendar.DATE, 2 - dayofweek - 21);
449         return getDayStartTime(cal.getTime());
450     }
451 
452     // 獲取上3周的結束時間
453     public static Date getEndDayOfLast3Week() {
454         Calendar cal = Calendar.getInstance();
455         cal.setTime(getBeginDayOfLast3Week());
456         cal.add(Calendar.DAY_OF_WEEK, 6);
457         Date weekEndSta = cal.getTime();
458         return getDayEndTime(weekEndSta);
459     }
460 
461     // 獲取本月的開始時間
462     public static Date getBeginDayOfMonth() {
463         Calendar calendar = Calendar.getInstance();
464         calendar.set(getNowYear(), getNowMonth() - 1, 1);
465         return getDayStartTime(calendar.getTime());
466     }
467 
468     // 獲取本月的結束時間
469     public static Date getEndDayOfMonth() {
470         Calendar calendar = Calendar.getInstance();
471         calendar.set(getNowYear(), getNowMonth() - 1, 1);
472         int day = calendar.getActualMaximum(5);
473         calendar.set(getNowYear(), getNowMonth() - 1, day);
474         return getDayEndTime(calendar.getTime());
475     }
476 
477     // 獲取上月的開始時間
478     public static Date getBeginDayOfLastMonth() {
479         Calendar calendar = Calendar.getInstance();
480         calendar.set(getNowYear(), getNowMonth() - 2, 1);
481         return getDayStartTime(calendar.getTime());
482     }
483 
484     // 獲取上月的結束時間
485     public static Date getEndDayOfLastMonth() {
486         Calendar calendar = Calendar.getInstance();
487         calendar.set(getNowYear(), getNowMonth() - 2, 1);
488         int day = calendar.getActualMaximum(5);
489         calendar.set(getNowYear(), getNowMonth() - 2, day);
490         return getDayEndTime(calendar.getTime());
491     }
492 
493     // 獲取上2月的開始時間
494     public static Date getBeginDayOfLast2Month() {
495         Calendar calendar = Calendar.getInstance();
496         calendar.set(getNowYear(), getNowMonth() - 3, 1);
497         return getDayStartTime(calendar.getTime());
498     }
499 
500     // 獲取上2月的結束時間
501     public static Date getEndDayOfLast2Month() {
502         Calendar calendar = Calendar.getInstance();
503         calendar.set(getNowYear(), getNowMonth() - 3, 1);
504         int day = calendar.getActualMaximum(5);
505         calendar.set(getNowYear(), getNowMonth() - 3, day);
506         return getDayEndTime(calendar.getTime());
507     }
508 
509     // 獲取上月的開始時間
510     public static Date getBeginDayOfLast3Month() {
511         Calendar calendar = Calendar.getInstance();
512         calendar.set(getNowYear(), getNowMonth() - 4, 1);
513         return getDayStartTime(calendar.getTime());
514     }
515 
516     // 獲取上月的結束時間
517     public static Date getEndDayOfLast3Month() {
518         Calendar calendar = Calendar.getInstance();
519         calendar.set(getNowYear(), getNowMonth() - 4, 1);
520         int day = calendar.getActualMaximum(5);
521         calendar.set(getNowYear(), getNowMonth() - 4, day);
522         return getDayEndTime(calendar.getTime());
523     }
524 
525     // 獲取本年的開始時間
526     public static java.util.Date getBeginDayOfYear() {
527         Calendar cal = Calendar.getInstance();
528         cal.set(Calendar.YEAR, getNowYear());
529         // cal.set
530         cal.set(Calendar.MONTH, Calendar.JANUARY);
531         cal.set(Calendar.DATE, 1);
532 
533         return getDayStartTime(cal.getTime());
534     }
535 
536     // 獲取本年的結束時間
537     public static java.util.Date getEndDayOfYear() {
538         Calendar cal = Calendar.getInstance();
539         cal.set(Calendar.YEAR, getNowYear());
540         cal.set(Calendar.MONTH, Calendar.DECEMBER);
541         cal.set(Calendar.DATE, 31);
542         return getDayEndTime(cal.getTime());
543     }
544 
545     // 獲取某個日期的開始時間
546     public static Timestamp getDayStartTime(Date d) {
547         Calendar calendar = Calendar.getInstance();
548         if (null != d)
549             calendar.setTime(d);
550         calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0,
551                 0, 0);
552         calendar.set(Calendar.MILLISECOND, 0);
553         return new Timestamp(calendar.getTimeInMillis());
554     }
555 
556     // 獲取某個日期的結束時間
557     public static Timestamp getDayEndTime(Date d) {
558         Calendar calendar = Calendar.getInstance();
559         if (null != d)
560             calendar.setTime(d);
561         calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23,
562                 59, 59);
563         calendar.set(Calendar.MILLISECOND, 999);
564         return new Timestamp(calendar.getTimeInMillis());
565     }
566 
567     // 獲取今年是哪一年
568     public static Integer getNowYear() {
569         Date date = new Date();
570         GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
571         gc.setTime(date);
572         return Integer.valueOf(gc.get(1));
573     }
574 
575     // 獲取本月是哪一月
576     public static int getNowMonth() {
577         Date date = new Date();
578         GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
579         gc.setTime(date);
580         return gc.get(2) + 1;
581     }
582 
583     // 兩個日期相減得到的天數
584     public static int getDiffDays(Date beginDate, Date endDate) {
585 
586         if (beginDate == null || endDate == null) {
587             throw new IllegalArgumentException("getDiffDays param is null!");
588         }
589 
590         long diff = (endDate.getTime() - beginDate.getTime()) / (1000 * 60 * 60 * 24);
591 
592         int days = new Long(diff).intValue();
593 
594         return days;
595     }
596 
597     // 兩個日期相減得到的毫秒數
598     public static long dateDiff(Date beginDate, Date endDate) {
599         long date1ms = beginDate.getTime();
600         long date2ms = endDate.getTime();
601         return date2ms - date1ms;
602     }
603 
604     // 獲取兩個日期中的最大日期
605     public static Date max(Date beginDate, Date endDate) {
606         if (beginDate == null) {
607             return endDate;
608         }
609         if (endDate == null) {
610             return beginDate;
611         }
612         if (beginDate.after(endDate)) {
613             return beginDate;
614         }
615         return endDate;
616     }
617 
618     // 獲取兩個日期中的最小日期
619     public static Date min(Date beginDate, Date endDate) {
620         if (beginDate == null) {
621             return endDate;
622         }
623         if (endDate == null) {
624             return beginDate;
625         }
626         if (beginDate.after(endDate)) {
627             return endDate;
628         }
629         return beginDate;
630     }
631 
632     // 回傳某月該季度的第一個月
633     public static Date getFirstSeasonDate(Date date) {
634         final int[] SEASON = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 };
635         Calendar cal = Calendar.getInstance();
636         cal.setTime(date);
637         int sean = SEASON[cal.get(Calendar.MONTH)];
638         cal.set(Calendar.MONTH, sean * 3 - 3);
639         return cal.getTime();
640     }
641 
642     // 回傳某個日期下幾天的日期
643     public static Date getNextDay(Date date, int i) {
644         Calendar cal = new GregorianCalendar();
645         cal.setTime(date);
646         cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i);
647         return cal.getTime();
648     }
649 
650     // 回傳某個日期前幾天的日期
651     public static Date getFrontDay(Date date, int i) {
652         Calendar cal = new GregorianCalendar();
653         cal.setTime(date);
654         cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);
655         return cal.getTime();
656     }
657 
658     // 回傳昨天的日期
659     public static String getYesterDay() {
660         Date date = new Date();// 取時間
661         Calendar calendar = new GregorianCalendar();
662         calendar.setTime(date);
663         calendar.add(Calendar.DATE, -1);// 把日期往后增加一天.整數往后推,負數往前移動
664         date = calendar.getTime(); // 這個時間就是日期往后推一天的結果
665         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
666         String dateString = formatter.format(date);
667         return dateString;
668     }
669 
670     // 獲取某年某月到某年某月按天的切片日期集合(間隔天數的集合)
671     @SuppressWarnings({ "rawtypes", "unchecked" })
672     public static List getTimeList(int beginYear, int beginMonth, int endYear, int endMonth, int k) {
673         List list = new ArrayList();
674         if (beginYear == endYear) {
675             for (int j = beginMonth; j <= endMonth; j++) {
676                 list.add(getTimeList(beginYear, j, k));
677 
678             }
679         } else {
680             {
681                 for (int j = beginMonth; j < 12; j++) {
682                     list.add(getTimeList(beginYear, j, k));
683                 }
684 
685                 for (int i = beginYear + 1; i < endYear; i++) {
686                     for (int j = 0; j < 12; j++) {
687                         list.add(getTimeList(i, j, k));
688                     }
689                 }
690                 for (int j = 0; j <= endMonth; j++) {
691                     list.add(getTimeList(endYear, j, k));
692                 }
693             }
694         }
695         return list;
696     }
697 
698     // 獲取某年某月按天切片日期集合(某個月間隔多少天的日期集合)
699     @SuppressWarnings({ "unchecked", "rawtypes" })
700     public static List getTimeList(int beginYear, int beginMonth, int k) {
701         List list = new ArrayList();
702         Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);
703         int max = begincal.getActualMaximum(Calendar.DATE);
704         for (int i = 1; i < max; i = i + k) {
705             list.add(begincal.getTime());
706             begincal.add(Calendar.DATE, k);
707         }
708         begincal = new GregorianCalendar(beginYear, beginMonth, max);
709         list.add(begincal.getTime());
710         return list;
711     }
712 }

 

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/148104.html

標籤:Java

上一篇:Java實作對List中的物件按某個欄位進行排序

下一篇:微服務設計 10 大反模式和陷阱!

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more