日期工具类
package net.yunxinyong.accesscard.utils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 日期工具类 */ public final class DateUtils { /** * 英文简写 如:2010-12 */ public static String FORMAT_SHORT_NO_MONTH = "yyyy-MM"; /** * 英文简写(默认)如:2010-12-01 */ public static String FORMAT_SHORT = "yyyy-MM-dd"; /** * 英文全称 如:2010-12-01 23:15:06 */ public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss"; /** * 精确到毫秒的完整时间 如:yyyy-MM-dd HH:mm:ss.S */ public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S"; /** * 中文简写 如:2010年12月01日 */ public static String FORMAT_SHORT_CN = "yyyy年MM月dd"; /** * 中文全称 如:2010年12月01日 23时15分06秒 */ public static String FORMAT_LONG_CN = "yyyy年MM月dd日 HH时mm分ss秒"; /** * 精确到毫秒的完整中文时间 */ public static String FORMAT_FULL_CN = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒"; /** * 获得默认的 date pattern */ public static String getDatePattern() { return FORMAT_LONG; } /** * 根据预设格式返回当前日期 * * @return */ public static String getNow() { return format(new Date()); } /** * 根据用户格式返回当前日期 * * @param format * @return */ public static String getNow(String format) { return format(new Date(), format); } /** * 使用预设格式格式化日期 * * @param date * @return */ public static String format(Date date) { return format(date, getDatePattern()); } /** * 使用用户格式格式化日期 * * @param date 日期 * @param pattern 日期格式 * @return */ public static String format(Date date, String pattern) { String returnValue = ""; if (date != null) { SimpleDateFormat df = new SimpleDateFormat(pattern); returnValue = df.format(date); } return (returnValue); } /** * 使用预设格式提取字符串日期 * * @param strDate 日期字符串 * @return */ public static Date parse(String strDate) { return parse(strDate, getDatePattern()); } /** * 使用用户格式提取字符串日期 * * @param strDate 日期字符串 * @param pattern 日期格式 * @return */ public static Date parse(String strDate, String pattern) { SimpleDateFormat df = new SimpleDateFormat(pattern); try { return df.parse(strDate); } catch (ParseException e) { // e.printStackTrace(); return null; } } /** * 在日期上增加数个整月 * * @param date 日期 * @param n 要增加的月数 * @return */ public static Date addMonth(Date date, int n) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MONTH, n); return cal.getTime(); } /** * 在日期上增加天数 * * @param date 日期 * @param n 要增加的天数 * @return */ public static Date addDay(Date date, int n) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.DATE, n); return cal.getTime(); } /** * 获取时间戳 */ public static String getTimeString() { SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL); Calendar calendar = Calendar.getInstance(); return df.format(calendar.getTime()); } /** * 获取日期年份 * * @param date 日期 * @return */ public static String getYear(Date date) { return format(date).substring(0, 4); } /** * 按默认格式的字符串距离今天的天数 * * @param date 日期字符串 * @return */ public static int countDays(String date) { long t = Calendar.getInstance().getTime().getTime(); Calendar c = Calendar.getInstance(); c.setTime(parse(date)); long t1 = c.getTime().getTime(); return (int) (t / 1000 - t1 / 1000) / 3600 / 24; } /** * 按用户格式字符串距离今天的天数 * * @param date 日期字符串 * @param format 日期格式 * @return */ public static int countDays(String date, String format) { long t = Calendar.getInstance().getTime().getTime(); Calendar c = Calendar.getInstance(); c.setTime(parse(date, format)); long t1 = c.getTime().getTime(); return (int) (t / 1000 - t1 / 1000) / 3600 / 24; } /** * 查询两个日期之间间隔几天 * @param date1 * @param date2 * @return */ public static int differentDays(Date date1,Date date2) { Calendar cal1 = Calendar.getInstance(); cal1.setTime(date1); Calendar cal2 = Calendar.getInstance(); cal2.setTime(date2); int day1= cal1.get(Calendar.DAY_OF_YEAR); int day2 = cal2.get(Calendar.DAY_OF_YEAR); int year1 = cal1.get(Calendar.YEAR); int year2 = cal2.get(Calendar.YEAR); if(year1 != year2) //同一年 { int timeDistance = 0 ; for(int i = year1 ; i < year2 ; i ++) { if(i%4==0 && i%100!=0 || i%400==0) //闰年 { timeDistance += 366; } else //不是闰年 { timeDistance += 365; } } return timeDistance + (day2-day1) ; } else //不同年 { return day2-day1; } } /** * 这是得到下个月的1号 * @return */ public static Date getNextMonthFirstDay(Integer year, Integer month){ Calendar instance = Calendar.getInstance(); instance.set(year,month,1); Date time = instance.getTime(); return time; } /** * 获取当年的1月1号 * @param year * @return */ public static Date getThisYearFisrtDay(Integer year){ Calendar instance = Calendar.getInstance(); instance.set(year,0,1); Date time = instance.getTime(); return time; } /** * 输入一个年份,一个月份 然后得到之 这一年这个月份的一月一日 * 比如输入 2018 10 得到的是 2018-10-01 * @param year * @param month * @return */ public static Date getMonth(Integer year, Integer month){ Calendar instance = Calendar.getInstance(); instance.set(year, (month -1),1); return instance.getTime(); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)