| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- /*
- * Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * Neither the name of the dreamlu.net developer nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- * Author: Chill 庄骞 (smallchill@163.com)
- */
- package org.springblade.common.utils;
- import org.springblade.core.tool.utils.DateUtil;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- /**
- * 通用工具类
- *
- * @author Chill
- */
- public class CommonUtil {
- public static final String pattern = "yyyy-MM-dd";
- /**
- * 获取传入时间的第二天
- * @param today
- * @return
- */
- public static String getNextDay(String today){
- Calendar cd = Calendar.getInstance();
- cd.setTime(DateUtil.parse(today, pattern));
- cd.add(Calendar.DATE, 1);
- String nextDay = DateUtil.format(cd.getTime(), pattern);
- return nextDay;
- }
- /**
- * 计算传入时间的相对位移时间,后几天或者前几天
- * @param theDay
- * @param interval
- * @return
- */
- public static String getDay(String theDay, int interval){
- Calendar cd = Calendar.getInstance();
- cd.setTime(DateUtil.parse(theDay, pattern));
- cd.add(Calendar.DATE, interval);
- String ThatDay = DateUtil.format(cd.getTime(), pattern);
- return ThatDay;
- }
- /**
- * 计算两个日期相差天数
- * @param beginDate
- * @param endDate
- * @return
- */
- public static long getInterval(String beginDate, String endDate){
- Date begin = DateUtil.parse(beginDate, pattern);
- Date end = DateUtil.parse(endDate, pattern);
- return (end.getTime() - begin.getTime()) / (24 * 60 * 60 * 1000);
- }
- /**
- * 判断日期是否为周五
- * @param theDay
- * @return
- */
- public static boolean isWeekLastDay(String theDay){
- Calendar cal = Calendar.getInstance();
- cal.setTime(DateUtil.parse(theDay, pattern));
- int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
- int[] weekDays = { 7, 1, 2, 3, 4, 5, 6 };
- return weekDays[w] == 5;
- }
- /**
- * 根据传入时间,算出这周开始日期和结束日期
- * @param theDay
- * @return
- */
- public static String[] getWeekRange(String theDay){
- SimpleDateFormat format = new SimpleDateFormat(pattern);
- Calendar cal = Calendar.getInstance();
- cal.setTime(DateUtil.parse(theDay, pattern));
- int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
- int[] weekDays = { 7, 1, 2, 3, 4, 5, 6 };
- int today = weekDays[w];
- int begin = today == 7 ? 0 : -today;
- int end = today == 7 ? 6 : 6 - today;
- Calendar beginDate = Calendar.getInstance();
- beginDate.setTime(DateUtil.parse(theDay, pattern));
- Calendar endDate = Calendar.getInstance();
- endDate.setTime(DateUtil.parse(theDay, pattern));
- beginDate.add(Calendar.DAY_OF_MONTH, begin);
- endDate.add(Calendar.DAY_OF_MONTH, end);
- String beginDateStr = format.format(beginDate.getTime());
- String endDateStr = format.format(endDate.getTime());
- return new String[]{beginDateStr, endDateStr};
- }
- /**
- * 根据传入时间,算出这个月的开始日期和结束日期
- * @param theDay
- * @return
- */
- public static String[] getMonthRange(String theDay){
- String[] split = theDay.split("-");
- String beginDateStr = split[0] + "-" + split[1] + "-" + "01";
- Calendar endDate = Calendar.getInstance();
- endDate.setTime(DateUtil.parse(theDay, pattern));
- int actualMaximum = endDate.getActualMaximum(Calendar.DAY_OF_MONTH);
- String endDateStr = split[0] + "-" + split[1] + "-" + actualMaximum;
- return new String[]{beginDateStr, endDateStr};
- }
- /**
- * 判断日期是否为周五
- * @param theDay
- * @return
- */
- public static boolean isMonthLastDay(String theDay){
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(DateUtil.parse(theDay, pattern));
- return calendar.get(Calendar.DAY_OF_MONTH) == calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
- }
- }
|