代码如下:(文章底部提供工具类下载)
import lombok.Data; import org.apache.commons.lang.time.DateUtils; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; /** * @author xujx * @create 2018-2-3 * @Company: iufc * @description: **/ @Data public class DateUtil { public static final String YYYY_MM_DD = "yyyy-MM-dd"; public static final String YYYYMMDD = "yyyy/MM/dd"; public static final String HH_MM_SS = "HH:mm:ss"; public static final String HH_MM = "HH:mm"; public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm"; public static final String YYYY_MMDD = "yyyyMMdd"; public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss"; public static final String HHMMSS = "HHmmss"; public static final String YYYYMMDD_HH_MM_SS = "yyyy/MM/dd HH:mm:ss"; public static final String YYYY_MM = "yyyy-MM"; public static final String YYYY = "yyyy"; public static final String MM = "MM"; /** * 返回自定义格式的当前日期时间字符串 * * @param format 格式规则 * @return String 返回当前字符串型日期时间 */ public static String getCurrentTime(String format) { String returnStr = null; SimpleDateFormat f = new SimpleDateFormat(format); Date date = new Date(); returnStr = f.format(date); return returnStr; } public static String getCurrentTime(Date date, String format) { String returnStr = null; SimpleDateFormat f = new SimpleDateFormat(format); returnStr = f.format(date); return returnStr; } public static long getTimeDifferenceBetweenEndTimeAndStartTime(Date startTime, Date endTime) { long difference = endTime.getTime() - startTime.getTime(); return difference; } /** * 计算2个日期年份相差多少年 * 列:2011-02-02 ~ 2017-03-02 大约相差 6年 * * @param fromDate * @param toDate * @return */ public static int yearCompare(Date fromDate, Date toDate) { SimpleDateFormat sdf = new SimpleDateFormat(YYYY); String formatDateStr = sdf.format(fromDate); String toDateStr = sdf.format(toDate); // int day = Integer.parseInt(toDateStr) - Integer.parseInt(formatDateStr); // if (day < 0) { // return 0; // } return countYear(formatDateStr, toDateStr); } public static int countYear(String fromDateStr, String toDateStr) { int day = Integer.parseInt(toDateStr) - Integer.parseInt(fromDateStr); if (day < 0) { return 0; } return day; } public static Date timeToDate(String time, String format) throws ParseException { DateFormat dateFormat = new SimpleDateFormat(format); Date date = null; date = dateFormat.parse(time); return date; } public static String dateToTime(Date date, String format) { DateFormat dateFormat = new SimpleDateFormat(format); String time = null; time = dateFormat.format(date); return time; } public static String thisYear() { Calendar year = Calendar.getInstance(); return String.valueOf(year.get(Calendar.YEAR)); } // 获取当前时间所在年的周数 public static int getWeekOfYear(Date date) { Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.MONDAY); c.setMinimalDaysInFirstWeek(7); c.setTime(date); return c.get(Calendar.WEEK_OF_YEAR); } // 获取当前时间所在周的开始日期 public static Date getFirstDayOfWeek(Date date) { Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.MONDAY); c.setTime(date); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); return c.getTime(); } // 获取当前时间所在周的结束日期 public static Date getLastDayOfWeek(Date date) { Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.MONDAY); c.setTime(date); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); return c.getTime(); } /* * * @author 张国召 2018/3/30 15:47 * @methodName getRecentlyNumberYear * @param [date, num] * @return java.lang.String * @description 获取指定的最近年份。如。date为2018年,num=3,则返回2015,2016,2017的年数组值, */ public static String[] getRecentlyNumberYear(Date date, int num) { String[] result = new String[num]; if (date != null) { int year = date.getYear() + 1900; for (int i = 0; i < num; i++) { result[i] = String.valueOf(year - (num - i)); } } return result; } /* * * @author 张国召 2018/4/16 11:16 * @methodName getFirstDayOfMonth * @param [date] * @return java.util.Date * @description 获取传入日期 所在月份的第一天 */ public static Date getFirstDayOfMonth(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int last = calendar.getActualMinimum(Calendar.DAY_OF_MONTH); calendar.set(Calendar.DAY_OF_MONTH, last); return calendar.getTime(); } /* * * @author 张国召 2018/4/16 11:17 * @methodName getEndDayOfMonth * @param [date] * @return java.util.Date * @description 获取传入日期 所在月份的最后一天 */ public static Date getLastDayOfMonth(Date date) { final Calendar cal = Calendar.getInstance(); cal.setTime(date); final int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH); cal.set(Calendar.DAY_OF_MONTH, last); return cal.getTime(); } /* * * @author 张国召 2018/6/29 10:57 * @methodName getFirstDayOfMonthStr * @param [yearMonth] * @returnjava.lang.String * @description 传入 年月格式的String 返回月第一天 年月日格式的String 如:(2018-06)(2018-06-01) */ public static String getFirstDayOfMonthStr(String yearMonth) throws ParseException { if (yearMonth.length() != 7) return null; SimpleDateFormat yearMonthDateFormat = new SimpleDateFormat("yyyy-MM"); Calendar calendar = Calendar.getInstance(); calendar.setTime(yearMonthDateFormat.parse(yearMonth)); int first = calendar.getActualMinimum(Calendar.DAY_OF_MONTH); calendar.set(Calendar.DAY_OF_MONTH, first); SimpleDateFormat yearMonthDayDateFormat = new SimpleDateFormat("yyyy-MM-dd"); return yearMonthDayDateFormat.format(calendar.getTime()); } /* * * @author 张国召 2018/6/29 10:57 * @methodName getFirstDayOfMonthStr * @param [yearMonth] * @returnjava.lang.String * @description 传入 年月格式的String 返回月的最后一天 年月日格式的String 如:(2018-06)(2018-06-30) */ public static String getLastDayOfMonthStr(String yearMonth) throws ParseException { if (yearMonth.length() != 7) return null; SimpleDateFormat yearMonthDateFormat = new SimpleDateFormat("yyyy-MM"); Calendar calendar = Calendar.getInstance(); calendar.setTime(yearMonthDateFormat.parse(yearMonth)); int last = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); calendar.set(Calendar.DAY_OF_MONTH, last); SimpleDateFormat yearMonthDayDateFormat = new SimpleDateFormat("yyyy-MM-dd"); return yearMonthDayDateFormat.format(calendar.getTime()); } /* * * @author 张国召 2018/6/26 17:55 * @methodName getYearArrBetweenTwoDate * @param [startDate, endDate] * @return java.lang.String[] * @description 获取两个传入日期间的所有年份;如 startDate: 2016-05-06 endData:2018-08-08则返回 reslt=2016,2017,2018 */ public static String getYearsStrBetweenTwoDate(Date startDate, Date endDate) { StringBuilder result = new StringBuilder(); Calendar startCal = Calendar.getInstance(); startCal.setTime(startDate); Calendar endCal = Calendar.getInstance(); endCal.setTime(endDate); for (int i = startCal.get(Calendar.YEAR); i <= endCal.get(Calendar.YEAR); i++) { result.append(i + ","); } return result.substring(0, result.length() - 1); } /* * * @author 张国召 2018/5/9 20:54 * @methodName getDateString * @param [date] * @return java.lang.String * @description 年月日 */ public static String getDateString(Date date) { return new SimpleDateFormat("yyyy年MM月dd日").format(date); } /** * 判断time是否在from,to之内 * * @param time 指定日期 * @param from 开始日期 * @param to 结束日期 * @return */ public static boolean belongCalendar(Date time, Date from, Date to) { Calendar date = Calendar.getInstance(); date.setTime(time); Calendar after = Calendar.getInstance(); after.setTime(from); Calendar before = Calendar.getInstance(); before.setTime(to); if (time.getTime() >= from.getTime() && time.getTime() <= to.getTime()) { return true; } else { return false; } // if (date.after(after) && date.before(before)) { // return true; // } else { // return false; // } } /** * 字符串转化成日期 * @param strDate * @param pattern * @return */ public static LocalDate formatDate(String strDate, String pattern) { DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern); return LocalDate.parse(strDate,dateTimeFormatter); } }
文章来源:
不凡
版权声明:本站所发布的全部内容源于互联网搬运,仅供用于学习和交流,如果有侵权之处请第一时间联系我们删除。敬请谅解! E-mail:xujinxiao@126.com
还木有评论哦,快来抢沙发吧~