本文共 11098 字,大约阅读时间需要 36 分钟。
package com.example.dateutils;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Calendar;import java.util.GregorianCalendar;public class VeDateUtils { private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); private static final SimpleDateFormat DATETIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss"); private static final SimpleDateFormat MONTH_DAY_FORMAT = new SimpleDateFormat("MM-dd"); private static final SimpleDateFormat YYYYMMDDHHMMSS_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss"); private VeDateUtils() { // 私密构造,防止单例 } /** * 将短时间格式字符串转换为日期 * @param strDate 输入字符串,格式为"yyyy-MM-dd" * @return 日期对象 */ public static Date strToDate(String strDate) { try { ParsePosition pos = new ParsePosition(0); return DATETIME_FORMAT.parse(strDate, pos); } catch (Throwable e) { throw new IllegalArgumentException("无法解析日期字符串:" + strDate, e); } } /** * 将短时间格式日期转换为字符串 * @param dateDate 日期对象 * @return 字符串,格式为"yyyy-MM-dd" */ public static String dateToStr(Date dateDate) { return DATE_FORMAT.format(dateDate); } /** * 将长时间格式字符串转换为日期 * @param strDate 输入字符串,格式为"yyyy-MM-dd HH:mm:ss" * @return 日期对象 */ public static Date strToDateLong(String strDate) { try { ParsePosition pos = new ParsePosition(0); return DATETIME_FORMAT.parse(strDate, pos); } catch (Throwable e) { throw new IllegalArgumentException("无法解析日期字符串:" + strDate, e); } } /** * 将日期对象转换为长时间格式字符串 * @param dateDate 日期对象 * @return 字符串,格式为"yyyy-MM-dd HH:mm:ss" */ public static String dateToStrLong(Date dateDate) { return DATETIME_FORMAT.format(dateDate); } /** * 获取当前时间的长时间格式字符串 * @return 字符串,格式为"yyyy-MM-dd HH:mm:ss" */ public static String getCurrentDateTime() { return DATETIME_FORMAT.format(new Date()); } /** * 获取当前日期的短时间格式字符串 * @return 字符串,格式为"yyyy-MM-dd" */ public static String getCurrentDate() { return DATE_FORMAT.format(new Date()); } /** * 获取当前时间的时间格式字符串 * @return 字符串,格式为"HH:mm:ss" */ public static String getCurrentTime() { return TIME_FORMAT.format(new Date()); } /** * 获取当前日期的月份与日期字符串,格式为"MM-dd" * @return 字符串,格式为"MM-dd" */ public static String getCurrentMonthDay() { return MONTH_DAY_FORMAT.format(new Date()); } /** * 获取当前日期的完整年月日字符串,格式为"yyyyMMdd" * @return 字符串,格式为"yyyyMMdd" */ public static String getCurrentFullYearMD() { return YYYYMMDDHHMMSS_FORMAT.format(new Date()).substring(0, 8); } /** * 获取当前日期的完整时间戳字符串,格式为"yyyyMMddHHmmss" * @return 字符串,格式为"yyyyMMddHHmmss" */ public static String getCurrentTimestamp() { return YYYYMMDDHHMMSS_FORMAT.format(new Date()); } /** * 计算两个日期之间的天数差 * @param date1 日期1 * @param date2 日期2 * @return 天数差 */ public static long getDaysBetween(Date date1, Date date2) { return (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000); } /** * 判断两个日期是否在同一周 * @param date1 日期1 * @param date2 日期2 * @return 是否在同一周 */ public static boolean isSameWeek(Date date1, Date date2) { Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); cal1.setTime(date1); cal2.setTime(date2); int week1 = cal1.get(Calendar.WEEK_OF_YEAR); int week2 = cal2.get(Calendar.WEEK_OF_YEAR); int yearDiff = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR); if (yearDiff == 0) { return week1 == week2; } else if (yearDiff == 1 && cal2.get(Calendar.MONTH) == Calendar.DECEMBER) { return week1 == week2; } else if (yearDiff == -1 && cal1.get(Calendar.MONTH) == Calendar.DECEMBER) { return week1 == week2; } return false; } /** * 判断给定日期是否为闰年 * @param ddate 日期字符串,格式为"yyyy-MM-dd" * @return 是否为闰年 */ public static boolean isLeapYear(String ddate) { Date date = strToDateLong(ddate); GregorianCalendar gc = new GregorianCalendar(); gc.setTime(date); int year = gc.get(Calendar.YEAR); if ((year % 400) == 0) { return true; } else if ((year % 4) == 0) { if ((year % 100) == 0) { return false; } else { return true; } } else { return false; } } /** * 获取某月的最后一天 * @param dat 日期字符串,格式为"yyyy-MM-dd" * @return 最后一天的日期字符串,格式为"yyyy-MM-dd" */ public static String getEndDateOfMonth(String dat) { String yearStr = dat.substring(0, 4); String monthStr = dat.substring(5, 7); int month = Integer.parseInt(monthStr); boolean isLeapYear = isLeapYear(dat); if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { return yearStr + "-" + "31"; } else if (month == 4 || month == 6 || month == 9 || month == 11) { return yearStr + "-" + "30"; } else { return isLeapYear ? yearStr + "-" + "29" : yearStr + "-" + "28"; } } /** * 获取当前日期的月份天数字符串 * @return 字符串,格式为"MM-dd" */ public static String getCurrentMonthDay() { return MONTH_DAY_FORMAT.format(new Date()); } /** * 获取特定日期的月份天数字符串 * @param dat 日期字符串,格式为"yyyy-MM-dd" * @return 字符串,格式为"MM-dd" */ public static String getMonthDayStr(String dat) { String[] parts = dat.split("-"); String month = parts[1]; String day = parts[2]; return month + "-" + day; } /** * 获取特定日期的年月日字符串 * @param dat 日期字符串,格式为"yyyy-MM-dd" * @return 字符串,格式为"yyyy-MM-dd" */ public static String getFullYearMD(String dat) { return dat.substring(0, 8); } /** * 获取特定日期的完整时间戳字符串 * @param dat 日期字符串,格式为"yyyy-MM-dd HH:mm:ss" * @return 字符串,格式为"yyyyMMddHHmmss" */ public static String getTimestamp(String dat) { return YYYYMMDDHHMMSS_FORMAT.format(strToDateLong(dat)); } /** * 获取当前时间的年月日字符串 * @return 字符串,格式为"yyyy-MM-dd" */ public static String getCurrentFullYearMD() { return getCurrentTimestamp().substring(0, 8); } /** * 获取当前时间的完整时间戳字符串 * @return 字符串,格式为"yyyyMMddHHmmss" */ public static String getCurrentTimestamp() { return YYYYMMDDHHMMSS_FORMAT.format(new Date()); } /** * 获取特定日期的时间戳字符串 * @param dat 日期字符串,格式为"yyyy-MM-dd HH:mm:ss" * @return 字符串,格式为"yyyyMMddHHmmss" */ public static String getTimestamp(String dat) { return YYYYMMDDHHMMSS_FORMAT.format(strToDateLong(dat)); } /** * 计算两个日期之间的天数 * @param date1 日期1 * @param date2 日期2 * @return 天数差 */ public static long getDaysBetween(Date date1, Date date2) { return (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000); } /** * 获取特定日期的日期字符串 * @param dat 日期字符串,格式为"yyyy-MM-dd" * @return 字符串,格式为"yyyy-MM-dd" */ public static String getDateStr(String dat) { return DATE_FORMAT.format(strToDateLong(dat)); } /** * 将日期字符串转换为日期对象 * @param dateStr 日期字符串,格式为"yyyy-MM-dd" * @return 日期对象 */ public static Date toDate(String dateStr) { try { ParsePosition pos = new ParsePosition(0); return DATETIME_FORMAT.parse(dateStr, pos); } catch (Throwable e) { throw new IllegalArgumentException("无法解析日期字符串:" + dateStr, e); } } /** * 将日期对象转换为日期字符串 * @param date 日期对象 * @return 字符串,格式为"yyyy-MM-dd" */ public static String dateToStr(Date date) { return DATE_FORMAT.format(date); } /** * 获取特定日期的周序列 * @param sdate 日期字符串,格式为"yyyy-MM-dd" * @return 字符串,格式为"yyww" */ public static String getSeqWeek(String sdate) { Date date = strToDateLong(sdate); Calendar c = Calendar.getInstance(Locale.CHINA); c.setTime(date); String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR)); if (week.length() == 1) { week = "0" + week; } String year = Integer.toString(c.get(Calendar.YEAR)); return year + week; } /** * 获取特定日期的星期几字符串 * @param sdate 日期字符串,格式为"yyyy-MM-dd" * @return 星期几字符串,格式为"EEEE" */ public static String getWeekDayStr(String sdate) { Date date = strToDateLong(sdate); Calendar c = Calendar.getInstance(); c.setTime(date); return new SimpleDateFormat("EEEE").format(c.getTime()); } /** * 获取特定日期的星期几(1-7) * @param sdate 日期字符串,格式为"yyyy-MM-dd" * @return 星期几数字 */ public static int getWeekDayNum(String sdate) { Date date = strToDateLong(sdate); Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.DAY_OF_WEEK); } /** * 获取特定日期的星期几名称 * @param sdate 日期字符串,格式为"yyyy-MM-dd" * @return 星期几名称字符串,格式为"EEEE" */ public static String getWeekDayName(String sdate) { return getWeekDayStr(sdate); } /** * 获取特定日期的日期编号 * @param sdate 日期字符串,格式为"yyyy-MM-dd" * @return 日期编号,格式为"yyyyMMdd" */ public static String get日期编号(String sdate) { return getCurrentTimestamp().substring(0, 8); } /** * 获取特定日期的时间戳 * @param sdate 日期字符串,格式为"yyyy-MM-dd HH:mm:ss" * @return 时间戳字符串,格式为"yyyyMMddHHmmss" */ public static String getTimestamp(String sdate) { return YYYYMMDDHHMMSS_FORMAT.format(strToDateLong(sdate)); } /** * 获取特定日期的月份天数字符串 * @param sdate 日期字符串,格式为"yyyy-MM-dd" * @return 字符串,格式为"MM-dd" */ public static String getMonthDayStr(String sdate) { String[] parts = sdate.split("-"); return parts[1] + "-" + parts[2]; } /** * 获取特定日期的年月日字符串 * @param sdate 日期字符串,格式为"yyyy-MM-dd" * @return 字符串,格式为"yyyy-MM-dd" */ public static String getFullYearMD(String sdate) { return sdate.substring(0, 8); } /** * 获取特定日期的完整时间戳字符串 * @param sdate 日期字符串,格式为"yyyy-MM-dd HH:mm:ss" * @return 字符串,格式为"yyyyMMddHHmmss" */ public static String getTimestamp(String sdate) { return YYYYMMDDHHMMSS_FORMAT.format(strToDateLong(sdate)); } /** * 获取特定日期的月份天数字符串 * @param sdate 日期字符串,格式为"yyyy-MM-dd" * @return 字符串,格式为"MM-dd" */ public static String getMonthDayStr(String sdate) { String[] parts = sdate.split("-"); return parts[1] + "-" + parts[2]; } /** * 获取特定日期的年月日字符串 * @param sdate 日期字符串,格式为"yyyy-MM-dd" * @return 字符串,格式为"yyyy-MM-dd" */ public static String getFullYearMD(String sdate) { return sdate.substring(0, 8); } /** * 获取特定日期的完整时间戳字符串 * @param sdate 日期字符串,格式为"yyyy-MM-dd HH:mm:ss" * @return 字符串,格式为"yyyyMMddHHmmss" */ public static String getTimestamp(String sdate) { return YYYYMMDDHHMMSS_FORMAT.format(strToDateLong(sdate)); }} 转载地址:http://ddkt.baihongyu.com/