程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Java 周歷日歷,java周歷日歷

Java 周歷日歷,java周歷日歷

編輯:JAVA綜合教程

Java 周歷日歷,java周歷日歷


WeekCalendarUtils工具類代碼,傳入起始日期即可返回對應日期的周歷日歷,年月部分添加周數統計

  1 import java.util.Calendar;
  2 import java.util.Date;
  3 import java.util.Map;
  4 
  5 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
  6 import org.apache.commons.lang3.builder.ToStringStyle;
  7 import org.apache.commons.lang3.time.DateFormatUtils;
  8 import org.apache.commons.lang3.time.DateUtils;
  9 
 10 import com.google.common.collect.Maps;
 11 
 12 /**
 13  * <b>function:</b> 周歷
 14  * 
 15  * @author hoojo
 16  * @createDate 2016-11-21 上午11:02:08
 17  * @file WeekCalendarUtils.java
 18  * @package 
 19  * @project 
 20  * @blog http://blog.csdn.net/IBM_hoojo
 21  * @email [email protected]
 22  * @version 1.0
 23  */
 24 public abstract class WeekCalendarUtils {
 25 
 26     public final static String DATE_FORMAT = "yyyy-MM-dd";  
 27     
 28     private static String getWeekDay(Calendar cal) {
 29         if (cal == null) {
 30             return null;
 31         }
 32         
 33         switch (cal.get(Calendar.DAY_OF_WEEK)) {
 34         
 35             case Calendar.MONDAY:
 36                 return "星期一";
 37             case Calendar.TUESDAY:
 38                 return "星期二";
 39             case Calendar.WEDNESDAY:
 40                 return "星期三";
 41             case Calendar.THURSDAY:
 42                 return "星期四";
 43             case Calendar.FRIDAY:
 44                 return "星期五";
 45             case Calendar.SATURDAY:
 46                 return "星期六";
 47             default:
 48                 return "星期日";
 49         }
 50     }
 51     
 52     private static String getSimpleWeekDay(Calendar cal) {
 53         if (cal == null) {
 54             return null;
 55         }
 56         
 57         switch (cal.get(Calendar.DAY_OF_WEEK)) {
 58         
 59             case Calendar.MONDAY:
 60                 return "一";
 61             case Calendar.TUESDAY:
 62                 return "二";
 63             case Calendar.WEDNESDAY:
 64                 return "三";
 65             case Calendar.THURSDAY:
 66                 return "四";
 67             case Calendar.FRIDAY:
 68                 return "五";
 69             case Calendar.SATURDAY:
 70                 return "六";
 71             default:
 72                 return "日";
 73         }
 74     }
 75     
 76     public static String[] getWeekDays(boolean hasMonFirstWeekDay) {
 77         if (hasMonFirstWeekDay) {
 78             return new String[] { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
 79         } else {
 80             return new String[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
 81         }
 82     }
 83 
 84     /**
 85      * <b>function:</b> 獲取周歷
 86      * @author hoojo
 87      * @createDate 2016-11-21 下午6:00:18
 88      * @param begin 開始日期
 89      * @param end 結束日期
 90      * @return 周歷Map
 91      */
 92     public static Map<Integer, YearModel> get(String begin, String end, boolean hasMonFirstWeekDay) {
 93         
 94         Map<Integer, YearModel> years = Maps.newLinkedHashMap();
 95         
 96         Date beginDate = null;
 97         Date endDate = null;
 98         
 99         try {
100             beginDate = DateUtils.parseDate(begin, DATE_FORMAT);
101             endDate = DateUtils.parseDate(end, DATE_FORMAT);
102             
103             if (beginDate.compareTo(endDate) > 0) {
104                 return null;
105             }
106             
107             int weekCount = 0, monthWeekCount = 0;
108             do {
109                 Calendar cal = DateUtils.toCalendar(beginDate);
110                 if (hasMonFirstWeekDay) {
111                     cal.setFirstDayOfWeek(Calendar.MONDAY);
112                 }
113                 
114                 Map<Integer, MonthModel> months = Maps.newLinkedHashMap();
115                 int year = cal.get(Calendar.YEAR);
116                 YearModel yearModel = null;
117                 if (years.containsKey(year)) {
118                     yearModel = years.get(year);
119                     months = yearModel.getMonths();
120                 } else {
121                     yearModel = new YearModel(year, year + "年", months);
122                     years.put(year, yearModel);
123                     
124                     weekCount = 0;
125                 }
126                 
127                 Map<String, WeekModel> weeks = Maps.newLinkedHashMap();
128                 int month = cal.get(Calendar.MONTH) + 1;
129                 MonthModel monthModel = null;
130                 if (months.containsKey(month)) {
131                      monthModel = months.get(month);
132                      weeks = monthModel.getWeeks();
133                 } else {
134                     monthModel = new MonthModel(month, year + "年" + month + "月", weeks);
135                     months.put(month, monthModel);
136                     
137                     monthWeekCount = 0;
138                 }
139                 
140                 Map<String, DayModel> days = Maps.newLinkedHashMap();
141                 int weekInMonth = cal.get(Calendar.WEEK_OF_MONTH);
142                 String week = cal.getWeekYear() + "_" + month + "_" + weekInMonth;
143                 if (weeks.containsKey(week)) {
144                     days = weeks.get(week).getDays();
145                 } else {
146                     weeks.put(week, new WeekModel(weekInMonth, month + "月第" + weekInMonth + "周", days));
147                     
148                     monthWeekCount++;
149                     weekCount++;
150                     monthModel.setWeekCount(monthWeekCount);
151                     yearModel.setWeekCount(weekCount);
152                 }
153                 
154                 String weekDay = getWeekDay(cal);
155                 days.put(week + "_" + weekDay, new DayModel(cal.get(Calendar.DAY_OF_MONTH), weekDay, getSimpleWeekDay(cal), beginDate));
156                 /*
157                 System.out.println("日期:" + DateFormatUtils.format(beginDate, DATE_FORMAT));
158                 System.out.println("年份:" + cal.getWeekYear());
159                 System.out.println("月份:" + (cal.get(Calendar.MONTH) + 1));
160                 System.out.println("星期:" + cal.get(Calendar.DAY_OF_WEEK));
161                 System.out.println("本月周次:" + cal.get(Calendar.WEEK_OF_MONTH));
162                 System.out.println();
163                 */
164                 beginDate = DateUtils.addDays(beginDate, 1);
165             } while (beginDate.compareTo(endDate) <= 0);
166             
167         } catch (Exception e) {
168             e.printStackTrace();
169         }
170         return years;
171     }
172     
173     public static Map<Integer, YearModel> get(Date beginDate, Date endDate, boolean hasMonFirstWeekDay) {
174         
175         try {
176             return get(DateFormatUtils.format(beginDate, DATE_FORMAT), DateFormatUtils.format(endDate, DATE_FORMAT), hasMonFirstWeekDay);
177         } catch (Exception e) {
178             e.printStackTrace();
179         }
180         return null;
181     }
182 
183     public static class YearModel {
184         private int yearName;
185         private String displayName;
186         private int weekCount;
187         private Map<Integer, MonthModel> months;
188         
189         public YearModel(int yearName, String displayName, Map<Integer, MonthModel> months) {
190             super();
191             this.yearName = yearName;
192             this.displayName = displayName;
193             this.months = months;
194         }
195         
196         public int getYearName() {
197             return yearName;
198         }
199         public void setYearName(int yearName) {
200             this.yearName = yearName;
201         }
202         public String getDisplayName() {
203             return displayName;
204         }
205         public void setDisplayName(String displayName) {
206             this.displayName = displayName;
207         }
208         public Map<Integer, MonthModel> getMonths() {
209             return months;
210         }
211         public void setMonths(Map<Integer, MonthModel> months) {
212             this.months = months;
213         }
214         
215         @Override
216         public String toString() {
217             return ReflectionToStringBuilder.toString(this, ToStringStyle.SIMPLE_STYLE);
218         }
219 
220         public int getWeekCount() {
221             return weekCount;
222         }
223 
224         public void setWeekCount(int weekCount) {
225             this.weekCount = weekCount;
226         }
227     }
228     
229     public static class MonthModel {
230         
231         private int monthName;
232         private String displayName;
233         private int weekCount;
234         private Map<String, WeekModel> weeks;
235         
236         public MonthModel(int monthName, String displayName, Map<String, WeekModel> weeks) {
237             super();
238             this.monthName = monthName;
239             this.displayName = displayName;
240             this.weeks = weeks;
241         }
242         
243         public int getMonthName() {
244             return monthName;
245         }
246         public void setMonthName(int monthName) {
247             this.monthName = monthName;
248         }
249         public String getDisplayName() {
250             return displayName;
251         }
252         public void setDisplayName(String displayName) {
253             this.displayName = displayName;
254         }
255         public Map<String, WeekModel> getWeeks() {
256             return weeks;
257         }
258         public void setWeeks(Map<String, WeekModel> weeks) {
259             this.weeks = weeks;
260         }
261         public int getWeekCount() {
262             return weekCount;
263         }
264         
265         public void setWeekCount(int weekCount) {
266             this.weekCount = weekCount;
267         }
268         @Override
269         public String toString() {
270             return ReflectionToStringBuilder.toString(this, ToStringStyle.SIMPLE_STYLE);
271         }
272     }
273     
274     public static class WeekModel {
275         
276         private int weekName;
277         private String displayName;
278         private Map<String, DayModel> days;
279         
280         public WeekModel(int weekName, String displayName, Map<String, DayModel> days) {
281             super();
282             this.weekName = weekName;
283             this.displayName = displayName;
284             this.days = days;
285         }
286         public int getWeekName() {
287             return weekName;
288         }
289         public void setWeekName(int weekName) {
290             this.weekName = weekName;
291         }
292         public String getDisplayName() {
293             return displayName;
294         }
295         public void setDisplayName(String displayName) {
296             this.displayName = displayName;
297         }
298         public Map<String, DayModel> getDays() {
299             return days;
300         }
301         public void setDays(Map<String, DayModel> days) {
302             this.days = days;
303         }
304         @Override
305         public String toString() {
306             return ReflectionToStringBuilder.toString(this, ToStringStyle.SIMPLE_STYLE);
307         }
308     }
309     
310     public static class DayModel {
311         
312         private int dayName;
313         private String displayName;
314         private String simpleName;
315         private Date date;
316         
317         public DayModel(int dayName, String displayName, String simpleName, Date date) {
318             super();
319             this.dayName = dayName;
320             this.displayName = displayName;
321             this.simpleName = simpleName;
322             this.date = date;
323         }
324         public int getDayName() {
325             return dayName;
326         }
327         public void setDayName(int dayName) {
328             this.dayName = dayName;
329         }
330         public String getDisplayName() {
331             return displayName;
332         }
333         public void setDisplayName(String displayName) {
334             this.displayName = displayName;
335         }
336         public Date getDate() {
337             return date;
338         }
339         public void setDate(Date date) {
340             this.date = date;
341         }
342         public String getSimpleName() {
343             return simpleName;
344         }
345         public void setSimpleName(String simpleName) {
346             this.simpleName = simpleName;
347         }
348         @Override
349         public String toString() {
350             return ReflectionToStringBuilder.toString(this, ToStringStyle.SIMPLE_STYLE);
351         }
352     }
353     
354     public static void main(String[] args) {
355         System.out.println(get("2016-06-01", "2017-07-03", false));
356     }
357 }

一個table頁面展示部分

1 <style type="text/css"> 2 td { 3 border: 1px solid black; 4 background-color: #eeeeee; 5 padding: 5px; 6 text-align: center; 7 } 8 9 table { 10 border-collapse: collapse; 11 border-spacing: 5px; 12 border: 1px solid black; 13 } 14 15 th { 16 border: 1px solid black; 17 background: #9DACBF; 18 color: #FFF; 19 height: 20px; 20 line-height: 20px 21 } 22 23 body { 24 font-family: "宋體", "Arial", "Helvetica"; 25 font-size: 12px; 26 font-style: normal; 27 font-weight: lighter; 28 } 29 30 .head { 31 background-color: #ccc; 32 font-weight: bold; 33 } 34 35 .head b { 36 color: #337ab7; 37 } 38 39 .odd td { 40 background-color: white; 41 } 42 43 .even td { 44 background-color: lavender; 45 } 46 </style> 47 48 <table class="xuenianTable" width="100%" cellspacing="0" cellpadding="0" border="0"> 49 <thead> 50 <tr height="55"> 51 <th colspan="10" > (${param.fileName })教學周歷</th> 52 </tr> 53 <tr height="35"> 54 <th width="10%">年份</th> 55 <th width="10%">月份</th> 56 <th width="10%">周次</th> 57 <th width="10%">一</th> 58 <th width="10%">二</th> 59 <th width="10%">三</th> 60 <th width="10%">四</th> 61 <th width="10%">五</th> 62 <th width="10%" >六</th> 63 <th width="10%" >七</th> 64 </tr> 65 </thead> 66 67 <tbody> 68 69 <c:set var="weekCount" value="1"/> 70 <c:forEach items="${result }" varStatus="st" var="year"> 71 <c:set var="yearFirst" value="true"/> 72 <c:forEach items="${year.value.months }" var="month"> 73 <c:set var="monthFirst" value="true"/> 74 <c:forEach items="${month.value.weeks }" var="week"> 75 <tr class="zhuanjafora ${st.index % 2 == 0 ? 'odd' : 'even' }"> 76 <c:if test="${yearFirst }"> 77 <c:set var="yearFirst" value="false"/> 78 <td rowspan="${year.value.weekCount }" title="${year.value.displayName }">${year.value.displayName }</td> 79 </c:if> 80 <c:if test="${!monthFirst}"> 81 <c:set var="weekCount" value="${weekCount + 1 }"/> 82 </c:if> 83 <c:if test="${monthFirst }"> 84 <c:set var="monthFirst" value="false"/> 85 <td rowspan="${month.value.weekCount }" title="${month.value.displayName }">${month.value.monthName }月</td> 86 </c:if> 87 <td title="${week.value.displayName }">${weekCount }周</td> 88 <c:forEach items="${weekDays }" var="weekDay"> 89 <c:set var="weekDayKey" value="${week.key}_${weekDay }"/> 90 <td title='${week.value.days[weekDayKey].displayName } <fmt:formatDate value="${week.value.days[weekDayKey].date }" pattern="yyyy-MM-dd"/>'>91 ${week.value.days[weekDayKey].dayName } 92 </td> 93 </c:forEach> 94 </tr> 95 </c:forEach> 96 </c:forEach> 97 </c:forEach> 98 </tbody> 99 </table> View Code

 

日歷形式展示部分,在日歷中一個周次不足6周會用空白格填充,來保證布局完整不錯位。

1 <div > 2 3 <c:set var="weekCount" value="1"/> 4 <c:forEach items="${result }" var="year"> 5 <c:set var="yearFirst" value="true"/> 6 <c:forEach items="${year.value.months }" var="month" varStatus="st"> 7 <c:set var="monthFirst" value="true"/> 8 <div > 9 <table border="0" cellspacing="0" cellpadding="0" width="100%" class="zhouli"> 10 <thead> 11 <tr height="35"> 12 <th width="10%">年份</th> 13 <th width="10%">月份</th> 14 <th width="10%">周次</th> 15 <th width="10%">一</th> 16 <th width="10%">二</th> 17 <th width="10%">三</th> 18 <th width="10%">四</th> 19 <th width="10%">五</th> 20 <th width="10%" >六</th> 21 <th width="10%" >七</th> 22 </tr> 23 </thead> 24 25 <tbody> 26 <c:set var="fillWeekCount" value="${6 - fn:length(month.value.weeks) }"/> 27 <c:forEach items="${month.value.weeks }" var="week"> 28 <tr class="zhuanjafora ${st.index % 2 == 0 ? 'odd' : 'even' }"> 29 <c:if test="${!monthFirst}"> 30 <c:set var="weekCount" value="${weekCount + 1 }"/> 31 </c:if> 32 <c:if test="${monthFirst }"> 33 <c:set var="monthFirst" value="false"/> 34 <td rowspan="${month.value.weekCount + fillWeekCount }" title="${year.value.displayName }">${year.value.yearName }</td> 35 <td rowspan="${month.value.weekCount + fillWeekCount }" title="${month.value.displayName }">${month.value.monthName }月</td> 36 </c:if> 37 <td title="${week.value.displayName }">${weekCount }周</td> 38 <c:forEach items="${weekDays }" var="weekDay"> 39 <c:set var="weekDayKey" value="${week.key}_${weekDay }"/> 40 <td title='${week.value.days[weekDayKey].displayName } <fmt:formatDate value="${week.value.days[weekDayKey].date }" pattern="yyyy-MM-dd"/>'>41 ${week.value.days[weekDayKey].dayName } 42 </td> 43 </c:forEach> 44 </tr> 45 </c:forEach> 46 47 <c:forEach begin="1" end="${fillWeekCount }" var="i"> 48 <tr class="zhuanjafora ${st.index % 2 == 0 ? 'odd' : 'even' }"> 49 <td>&nbsp;</td> 50 <td>&nbsp;</td> 51 <td>&nbsp;</td> 52 <td>&nbsp;</td> 53 <td>&nbsp;</td> 54 <td>&nbsp;</td> 55 <td>&nbsp;</td> 56 <td>&nbsp;</td> 57 </tr> 58 </c:forEach> 59 </tbody> 60 </table> 61 </div> 62 </c:forEach> 63 </c:forEach> 64 65 </div> View Code

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved