java 星期顺序_Java 周历日历

1 importjava.util.Calendar;2 importjava.util.Date;3 importjava.util.Map;4

5 importorg.apache.commons.lang3.builder.ReflectionToStringBuilder;6 importorg.apache.commons.lang3.builder.ToStringStyle;7 importorg.apache.commons.lang3.time.DateFormatUtils;8 importorg.apache.commons.lang3.time.DateUtils;9

10 importcom.google.common.collect.Maps;11

12 /**

13 * function: 周历14 *15 *@authorhoojo16 * @createDate 2016-11-21 上午11:02:0817 * @file WeekCalendarUtils.java18 * @package19 * @project20 * @bloghttp://blog.csdn.net/IBM_hoojo

21 * @email hoojo_@126.com22 *@version1.023 */

24 public abstract classWeekCalendarUtils {25

26 public final static String DATE_FORMAT = "yyyy-MM-dd";27

28 private staticString getWeekDay(Calendar cal) {29 if (cal == null) {30 return null;31 }32

33 switch(cal.get(Calendar.DAY_OF_WEEK)) {34

35 caseCalendar.MONDAY:36 return "星期一";37 caseCalendar.TUESDAY:38 return "星期二";39 caseCalendar.WEDNESDAY:40 return "星期三";41 caseCalendar.THURSDAY:42 return "星期四";43 caseCalendar.FRIDAY:44 return "星期五";45 caseCalendar.SATURDAY:46 return "星期六";47 default:48 return "星期日";49 }50 }51

52 private staticString getSimpleWeekDay(Calendar cal) {53 if (cal == null) {54 return null;55 }56

57 switch(cal.get(Calendar.DAY_OF_WEEK)) {58

59 caseCalendar.MONDAY:60 return "一";61 caseCalendar.TUESDAY:62 return "二";63 caseCalendar.WEDNESDAY:64 return "三";65 caseCalendar.THURSDAY:66 return "四";67 caseCalendar.FRIDAY:68 return "五";69 caseCalendar.SATURDAY:70 return "六";71 default:72 return "日";73 }74 }75

76 public static String[] getWeekDays(booleanhasMonFirstWeekDay) {77 if(hasMonFirstWeekDay) {78 return new String[] { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};79 } else{80 return new String[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};81 }82 }83

84 /**

85 * function: 获取周历86 *@authorhoojo87 * @createDate 2016-11-21 下午6:00:1888 *@parambegin 开始日期89 *@paramend 结束日期90 *@return周历Map91 */

92 public static Map get(String begin, String end, booleanhasMonFirstWeekDay) {93

94 Map 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 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 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 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, newDayModel(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 returnyears;171 }172

173 public static Map get(Date beginDate, Date endDate, booleanhasMonFirstWeekDay) {174

175 try{176 returnget(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 classYearModel {184 private intyearName;185 privateString displayName;186 private intweekCount;187 private Mapmonths;188

189 public YearModel(int yearName, String displayName, Mapmonths) {190 super();191 this.yearName =yearName;192 this.displayName =displayName;193 this.months =months;194 }195

196 public intgetYearName() {197 returnyearName;198 }199 public void setYearName(intyearName) {200 this.yearName =yearName;201 }202 publicString getDisplayName() {203 returndisplayName;204 }205 public voidsetDisplayName(String displayName) {206 this.displayName =displayName;207 }208 public MapgetMonths() {209 returnmonths;210 }211 public void setMonths(Mapmonths) {212 this.months =months;213 }214

215 @Override216 publicString toString() {217 return ReflectionToStringBuilder.toString(this, ToStringStyle.SIMPLE_STYLE);218 }219

220 public intgetWeekCount() {221 returnweekCount;222 }223

224 public void setWeekCount(intweekCount) {225 this.weekCount =weekCount;226 }227 }228

229 public static classMonthModel {230

231 private intmonthName;232 privateString displayName;233 private intweekCount;234 private Mapweeks;235

236 public MonthModel(int monthName, String displayName, Mapweeks) {237 super();238 this.monthName =monthName;239 this.displayName =displayName;240 this.weeks =weeks;241 }242

243 public intgetMonthName() {244 returnmonthName;245 }246 public void setMonthName(intmonthName) {247 this.monthName =monthName;248 }249 publicString getDisplayName() {250 returndisplayName;251 }252 public voidsetDisplayName(String displayName) {253 this.displayName =displayName;254 }255 public MapgetWeeks() {256 returnweeks;257 }258 public void setWeeks(Mapweeks) {259 this.weeks =weeks;260 }261 public intgetWeekCount() {262 returnweekCount;263 }264

265 public void setWeekCount(intweekCount) {266 this.weekCount =weekCount;267 }268 @Override269 publicString toString() {270 return ReflectionToStringBuilder.toString(this, ToStringStyle.SIMPLE_STYLE);271 }272 }273

274 public static classWeekModel {275

276 private intweekName;277 privateString displayName;278 private Mapdays;279

280 public WeekModel(int weekName, String displayName, Mapdays) {281 super();282 this.weekName =weekName;283 this.displayName =displayName;284 this.days =days;285 }286 public intgetWeekName() {287 returnweekName;288 }289 public void setWeekName(intweekName) {290 this.weekName =weekName;291 }292 publicString getDisplayName() {293 returndisplayName;294 }295 public voidsetDisplayName(String displayName) {296 this.displayName =displayName;297 }298 public MapgetDays() {299 returndays;300 }301 public void setDays(Mapdays) {302 this.days =days;303 }304 @Override305 publicString toString() {306 return ReflectionToStringBuilder.toString(this, ToStringStyle.SIMPLE_STYLE);307 }308 }309

310 public static classDayModel {311

312 private intdayName;313 privateString displayName;314 privateString simpleName;315 privateDate date;316

317 public DayModel(intdayName, 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 intgetDayName() {325 returndayName;326 }327 public void setDayName(intdayName) {328 this.dayName =dayName;329 }330 publicString getDisplayName() {331 returndisplayName;332 }333 public voidsetDisplayName(String displayName) {334 this.displayName =displayName;335 }336 publicDate getDate() {337 returndate;338 }339 public voidsetDate(Date date) {340 this.date =date;341 }342 publicString getSimpleName() {343 returnsimpleName;344 }345 public voidsetSimpleName(String simpleName) {346 this.simpleName =simpleName;347 }348 @Override349 publicString toString() {350 return ReflectionToStringBuilder.toString(this, ToStringStyle.SIMPLE_STYLE);351 }352 }353

354 public static voidmain(String[] args) {355 System.out.println(get("2016-06-01", "2017-07-03", false));356 }357 }

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:1024 设计师:我叫白小胖 返回首页
评论

打赏作者

friendcallmetwodog

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值