Java获取当前时间季度开始和结束时间以及对应的天数,

 1     /**
 2      * 获取当前日期所在季度的开始日期和结束日期
 3      * 季度一年四季, 第一季度:1月-3月, 第二季度:4月-6月, 第三季度:7月-9月, 第四季度:10月-12月
 4      * @param isFirst  true表示查询本季度开始日期  false表示查询本季度结束日期
 5      * @return
 6      */
 7     public static LocalDate getStartOrEndDayOfQuarter(Boolean isFirst){
 8         LocalDate today=LocalDate.now();
 9         LocalDate resDate = LocalDate.now();
10         if (today == null) {
11             today = resDate;
12         }
13         Month month = today.getMonth();
14         Month firstMonthOfQuarter = month.firstMonthOfQuarter();
15         Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
16         if (isFirst) {
17             resDate = LocalDate.of(today.getYear(), firstMonthOfQuarter, 1);
18         } else {
19             resDate = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear()));
20         }
21         return resDate;
22     }
23     //获取当前季度
24     public static String getQuarterByDate(String date) throws ParseException {
25         if(date == ""|| "".equals(date)){
26             return "";
27         }
28         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
29         Date datePar = sdf.parse(date);
30         Calendar calendar = Calendar.getInstance();
31         calendar.setTime(datePar);
32 
33         String year = String.valueOf(calendar.get(Calendar.YEAR));
34         int mouth = datePar.getMonth()+1;
35 
36         if(mouth>=1 && mouth<=3){
37             return year + "年第一季度";
38         }else if(mouth>=4 && mouth<=6){
39             return year + "年第二季度";
40         }else if(mouth>=7 && mouth<=9){
41             return year + "年第三季度";
42         }else if(mouth>=10 && mouth<=12){
43             return year + "年第四季度";
44         }else{
45             return "";
46         }
47     }
48 
49     /**
50      * 获取当期季度的天数
51      * @param cntDateBeg  开始时间
52      * @param cntDateEnd  结束时间
53      * @return
54      */
55     public static List<String> addDates(String cntDateBeg, String cntDateEnd) {
56         List<String> list = new ArrayList<>();
57         String[] dateBegs = cntDateBeg.split("-");
58         String[] dateEnds = cntDateEnd.split("-");
59         Calendar start = Calendar.getInstance();
60         start.set(Integer.valueOf(dateBegs[0]), Integer.valueOf(dateBegs[1]) - 1, Integer.valueOf(dateBegs[2]));
61         Long startTIme = start.getTimeInMillis();
62         Calendar end = Calendar.getInstance();
63         end.set(Integer.valueOf(dateEnds[0]), Integer.valueOf(dateEnds[1]) - 1, Integer.valueOf(dateEnds[2]));
64         Long endTime = end.getTimeInMillis();
65         Long oneDay = 1000 * 60 * 60 * 24l;
66         Long time = startTIme;
67         while (time <= endTime) {
68             Date d = new Date(time);
69             DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
70             time += oneDay;
71             list.add(df.format(d));
72         }
73         return list;
74     }

 

posted @ 2021-01-08 20:52    阅读(5278)  评论(0编辑  收藏  举报