本文實例為大家分享了java日期時間操作工具類,供大家參考,具體內(nèi)容如下
雖然jdk1.8開始,加入了time包,里面對時區(qū),本地化時間,格式化,以及時間等做了很好的封裝,但仍然要寫一個工具類。大家看著用。應(yīng)該沒有bug。如果發(fā)現(xiàn)了,請您一定告知,互相學(xué)習(xí)!好了,上代碼:
package com.wdy.tools.utils.timeutil; import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar; /** * TimeUtil 日期時間工具類 * @author wangdy */public class TimeUtil { /** * 獲取當(dāng)前系統(tǒng)時間的小時(HH) * * @return int 當(dāng)前系統(tǒng)小時HH格式 * @throws Exception */ public static int getHH() throws Exception { DateFormat df = new SimpleDateFormat("HH"); Date date = new Date(System.currentTimeMillis()); String str = df.format(date); return Integer.parseInt(str); } /** * 獲取當(dāng)前系統(tǒng)時間的分鐘數(shù)(mm) * * @return int 當(dāng)前系統(tǒng)分鐘mm格式 * @throws Exception */ public static int getMM() throws Exception { DateFormat df = new SimpleDateFormat("mm"); Date date = new Date(System.currentTimeMillis()); String str = df.format(date); // return Integer.parseInt(str.split(":")[0]) * 4 + // Integer.parseInt(str.split(":")[1]) / 15; return Integer.parseInt(str); } /** * 獲取當(dāng)前系統(tǒng)時間的秒數(shù)(ss) * * @return int 當(dāng)前系統(tǒng)時間的秒數(shù)(ss) * @throws Exception */ public static int getSS() throws Exception { DateFormat df = new SimpleDateFormat("ss"); Date date = new Date(System.currentTimeMillis()); String str = df.format(date); // return Integer.parseInt(str.split(":")[0]) * 4 + // Integer.parseInt(str.split(":")[1]) / 15; return Integer.parseInt(str); } /** * 獲取輸入日期的前后日期 * * @param date * 基準(zhǔn)日期 yyyy-MM-dd * @param dayMark * +代表往后,-代表往前 * @return String 前后日期(yyyy-MM-dd) * @throws Exception */ public static String getOtherDay(String date, int dayMark) throws Exception { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date dt = df.parse(date); Calendar c = Calendar.getInstance(); c.setTime(dt); c.add(Calendar.DAY_OF_MONTH, dayMark); String mDateTime = df.format(c.getTime()); String strStart = mDateTime.substring(0, 10); return strStart; } /** * 獲取日期所在周的第一天(周一) * * @param date基準(zhǔn)日期yyyy * -MM-dd * @return String 周一(yyyy-MM-dd) * @throws Exception * * */ public static String getWeekFirstDate(String date) throws Exception { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date dt = df.parse(date); Calendar c = Calendar.getInstance(); c.setTime(dt); int days = c.get(Calendar.DAY_OF_WEEK); String strStart = ""; if (days == 1) { strStart = getOtherDay(date, -days - 5); } else { strStart = getOtherDay(date, -days + 2); } return strStart; } /** * 獲取日期所在周的最后一天(周日) * * @param date基準(zhǔn)日期yyyy * -MM-dd * @return String(yyyy-MM-dd) * @throws Exception * * */ public static String getWeekLastDate(String date) throws Exception { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date dt = df.parse(date); Calendar c = Calendar.getInstance(); c.setTime(dt); int days = c.get(Calendar.DAY_OF_WEEK); String strStart = ""; if (days == 1) { strStart = getOtherDay(date, 0); } else { strStart = getOtherDay(date, 8 - days); } return strStart; } /** * 獲取日期所在周(年的周數(shù))的前后周的周一 * * @param date基準(zhǔn)日期yyyy * -MM-dd * @param weekMark找基準(zhǔn)日期 * +代表往后,-代表往前 * @return String 前后周的周一(yyyy-MM-dd) * @throws Exception * * */ public static String getOtherWeekFirstDate(String date, int weekMark) throws Exception { String firstDate = getWeekFirstDate(date); DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date dt = df.parse(firstDate); Calendar c = Calendar.getInstance(); c.setTime(dt); c.add(Calendar.WEEK_OF_YEAR, weekMark); String mDateTime = df.format(c.getTime()); String strStart = mDateTime.substring(0, 10); return strStart; } /** * 獲取日期所在季的第一天 * * @param date基準(zhǔn)日期yyyy * -MM-dd * @return String * @throws Exception * * */ public static String getSeasonFirstDate(String date) throws Exception { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date dt = df.parse(date); Calendar c = Calendar.getInstance(); c.setTime(dt); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); if (month < 3) { month = 0; } else if (month >= 3 && month < 6) { month = 3; } else if (month >= 6 && month < 9) { month = 6; } else if (month >= 9 && month < 12) { month = 9; } c.set(year, month, 1); String mDateTime = df.format(c.getTime()); String strStart = mDateTime.substring(0, 10); return strStart; } /** * 獲取日期所在季的前后季度的第一天(xxxx-xx-01) * * @param date * 基準(zhǔn)日期yyyy-MM-dd * @param seasonMark * 找基準(zhǔn)日期+代表往后,-代表往前 * @return String * @throws Exception * * */ public static String getOtherSeasonFirstDate(String date, int seasonMark) throws Exception { String firstDate = getSeasonFirstDate(date); DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date dt = df.parse(firstDate); Calendar c = Calendar.getInstance(); c.setTime(dt); int month = seasonMark * 3; if (month != 0) { c.add(Calendar.MONTH, month); } String mDateTime = df.format(c.getTime()); String strStart = mDateTime.substring(0, 10); return strStart; } /** * 獲取日期所在月的第一天 date基準(zhǔn)日期 * * @param date * yyyy-MM-dd * @return String (yyyy-MM) * @throws Exception * * */ public static String getMonthFirstDate(String date) throws Exception { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date dt = df.parse(date); Calendar c = Calendar.getInstance(); c.setTime(dt); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); c.set(year, month, 1); String mDateTime = df.format(c.getTime()); String strStart = mDateTime.substring(0, 10); return strStart; } /** * 獲取日期所在月的最后一天 * * @param date基準(zhǔn)日期yyyy * -MM-dd * @return String yyyy-MM-dd * @throws Exception * * */ public static String getMonthLastDate(String date) throws Exception { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date dt = df.parse(date); Calendar c = Calendar.getInstance(); c.setTime(dt); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int dayNum = c.getActualMaximum(Calendar.DAY_OF_MONTH); c.set(year, month, dayNum); String mDateTime = df.format(c.getTime()); String strStart = mDateTime.substring(0, 10); return strStart; } /** * 獲取日期所在月的前后月份的第一天(yyyy-MM-01) * * @param date基準(zhǔn)日期yyyy * -MM-dd * @param monthMark找基準(zhǔn)日期 * +代表往后,-代表往前 * @return String * @throws Exception * * */ public static String getOtherMonthFirstDate(String date, int monthMark) throws Exception { String firstDate = getMonthFirstDate(date); DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date dt = df.parse(firstDate); Calendar c = Calendar.getInstance(); c.setTime(dt); c.add(Calendar.MONTH, monthMark); String mDateTime = df.format(c.getTime()); String strStart = mDateTime.substring(0, 10); return strStart; } /** * 獲取日期所在年的第一天 * * @param date基準(zhǔn)日期yyyy * -MM-dd * @return String * @throws Exception * * */ public static String getYearFirstDate(String date) throws Exception { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date dt = df.parse(date); Calendar c = Calendar.getInstance(); c.setTime(dt); int year = c.get(Calendar.YEAR); c.set(year, 0, 1); String mDateTime = df.format(c.getTime()); String strStart = mDateTime.substring(0, 10); return strStart; } /** * 獲取日期所在年的前后年的第一天(yyyy-01-01) * * @param date * 基準(zhǔn)日期yyyy-MM-dd * @param monthMark * 找基準(zhǔn)日期+代表往后,-代表往前 * @return String * @throws Exception * * */ public static String getOtherYearFirstDate(String date, int yearMark) throws Exception { String firstDate = getMonthFirstDate(date); DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date dt = df.parse(firstDate); Calendar c = Calendar.getInstance(); c.setTime(dt); c.add(Calendar.YEAR, yearMark); String mDateTime = df.format(c.getTime()); String strStart = mDateTime.substring(0, 4); return strStart + "-01-01"; } /** * 取得同期日期 年同期 * * @param date * yyyy-MM-dd * @param year * 年份 * @return 年同期日期yyyy-MM-dd * @throws Exception */ public static String getYearTqDay(String date, int year) throws Exception { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date dt = df.parse(date); Calendar c = Calendar.getInstance(); c.setTime(dt); c.add(Calendar.YEAR, year); String mDateTime = df.format(c.getTime()); return mDateTime; } /** * 取得同期日期 月同期 * * @param date * yyyy-MM-dd * @param month * 月份 * @return 月同期日期yyyy-MM-dd * @throws Exception */ public static String getMonthTqDay(String date, int month) throws Exception { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date dt = df.parse(date); Calendar c = Calendar.getInstance(); c.setTime(dt); c.add(Calendar.MONTH, month); String mDateTime = df.format(c.getTime()); return mDateTime; } /** * 取得同比月份 * * @param month * 月份 * @return 同比月份 * @throws Exception */ public static String getTbMonth(String month) throws Exception { DateFormat df = new SimpleDateFormat("yyyy-MM"); Date dt = df.parse(month); Calendar c = Calendar.getInstance(); c.setTime(dt); c.add(Calendar.YEAR, -1); String mDateTime = df.format(c.getTime()); return mDateTime; } /** * 取得環(huán)比月份 * * @param month * 月份 * @return 環(huán)比月份 * @throws Exception */ public static String getHbMonth(String month) throws Exception { DateFormat df = new SimpleDateFormat("yyyy-MM"); Date dt = df.parse(month); Calendar c = Calendar.getInstance(); c.setTime(dt); c.add(Calendar.MONTH, -1); String mDateTime = df.format(c.getTime()); return mDateTime; } /** * 獲取兩個日期之間的天數(shù) * * @param sDate * -- 起始日期yyyy-MM-dd * @param eDate * -- 結(jié)束日期yyyy-MM-dd * @return int--天數(shù) * @throws Exception * */ public static int getDaysOfTwoDate(String sDate, String eDate) throws Exception { DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd"); DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd"); Date date1 = df1.parse(sDate); Date date2 = df2.parse(eDate); if (null == date1 || null == date2) { return -1; } long intervalMilli = date2.getTime() - date1.getTime(); return (int) (intervalMilli / (24 * 60 * 60 * 1000)); } /** * 獲取兩個月份之間的月數(shù) * * @param sDate * -- 起始月yyyy-MM * @param eDate * -- 結(jié)束月yyyy-MM * @return int--天數(shù) * @throws Exception * */ public static int getMonthOfTwoMonth(String sDate, String eDate) throws Exception { DateFormat df = new SimpleDateFormat("yyyy-MM"); Date date1 = df.parse(sDate); Date date2 = df.parse(eDate); if (null == date1 || null == date2) { return -1; } Calendar c1 = Calendar.getInstance(); c1.setTime(date1); Calendar c2 = Calendar.getInstance(); c2.setTime(date2); int month1 = c1.get(Calendar.YEAR) * 12 + c1.get(Calendar.MONTH); int month2 = c2.get(Calendar.YEAR) * 12 + c2.get(Calendar.MONTH); return month2 - month1; } /**比較兩個日期 * @param sDate 起始日期 * @param eDate 結(jié)束日期 * @param pattern 日期格式 * @return boolean 返回比較結(jié)果 * @throws Exception */ public static boolean compareDate(String sDate, String eDate, String pattern) throws Exception { DateFormat df1 = new SimpleDateFormat(pattern); Date date1 = df1.parse(sDate); Date date2 = df1.parse(eDate); if (null == date1 || null == date2) { return false; } long intervalMilli = date2.getTime() - date1.getTime(); if (intervalMilli > 0) { return true; } return false; } /**獲取兩個日期之間的分鐘數(shù) * @param sDate 起始日期 yyyy-MM-dd HH:mm:ss * @param eDate 結(jié)束日期 yyyy-MM-dd HH:mm:ss * @return int--分鐘數(shù) * @throws Exception */ public static int getMinsOfTwoDate(String sDate, String eDate) throws Exception { DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date1 = df1.parse(sDate); Date date2 = df2.parse(eDate); if (null == date1 || null == date2) { return -1; } long intervalMilli = date2.getTime() - date1.getTime(); return (int) (intervalMilli / (60 * 1000)); } /** * 獲取當(dāng)前系統(tǒng)時間的字符串 * * @return String -- 當(dāng)天的整個日期字符串,年月日時分秒,返回格式y(tǒng)yyy-MM-dd HH:mm:ss * @throws Exception * */ public static String getToDayAllStr() throws Exception { DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(System.currentTimeMillis()); String str = df.format(date); return str; } /** * 獲取當(dāng)前系統(tǒng)日期的字符串 * * @return String-- 當(dāng)天的年月日字符串,返回格式 yyyy-MM-dd * @throws Exception * */ public static String getToDayDateStr() throws Exception { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date date = new Date(System.currentTimeMillis()); String str = df.format(date); return str; } /** * 獲取當(dāng)前系統(tǒng)日期的字符串 * * @return String -- 當(dāng)天的年月日字符串,返回格式 yyyyMMdd * */ public static String getToDayYmd() throws Exception { DateFormat df = new SimpleDateFormat("yyyyMMdd"); Date date = new Date(System.currentTimeMillis()); String str = df.format(date); return str; } /**獲取當(dāng)前系統(tǒng)時間的指定類型字符串 * @param pattern 指定的格式 * @return String-- 當(dāng)天的指定類型的字符串 * @throws Exception */ public static String getToDayStrByPattern(String pattern) throws Exception { DateFormat df = new SimpleDateFormat(pattern); Date date = new Date(System.currentTimeMillis()); String str = df.format(date); return str; } /** * 獲取當(dāng)前系統(tǒng)時間的字符串 * * @return String 當(dāng)天的時分秒字符串,返回格式HH:mm:ss * */ public static String getToDayHmsStr() throws Exception { DateFormat df = new SimpleDateFormat("HH:mm:ss"); Date date = new Date(System.currentTimeMillis()); String str = df.format(date); return str; } /** * 獲取當(dāng)前系統(tǒng)時間的字符串 * * @return String -- 當(dāng)天的時分秒字符串,返回格式HHmmss * */ public static String getToDayHms() throws Exception { DateFormat df = new SimpleDateFormat("HHmmss"); Date date = new Date(System.currentTimeMillis()); String str = df.format(date); return str; } /**獲取當(dāng)前系統(tǒng)指定格式的時間的字符串 * @param pattern 指定格式 * @return String 當(dāng)前系統(tǒng)指定格式時間字符串 * @throws Exception */ public static String getToDayHmsByPattern(String pattern) throws Exception { DateFormat df = new SimpleDateFormat(pattern); Date date = new Date(System.currentTimeMillis()); String str = df.format(date); return str; } /** * 獲取指定日期的時刻字符串 * * @param dayStr * (yyyy-MM-dd HH:mm:ss) * @return String -- 當(dāng)天的時分秒字符串,返回格式:HHmmss * */ public static String getHmsStrForDateTime(String dayStr) throws Exception { DateFormat df = new SimpleDateFormat("HHmmss"); DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str = df.format(df2.parse(dayStr)); return str; } /** * 日期格式轉(zhuǎn)換 oldPattern 轉(zhuǎn)成 newPattern * * @param str 要轉(zhuǎn)換格式的日期字符串 * @param oldPattern 原有格式 * @param newPattern 目標(biāo)格式 * @return String轉(zhuǎn)換格式化后的字符串 * @throws Exception */ public static String changeDateType(String str, String oldPattern, String newPattern) throws Exception { DateFormat df = new SimpleDateFormat(oldPattern); DateFormat df1 = new SimpleDateFormat(newPattern); return df1.format(df.parse(str)); } /** * 獲取輸入日期的前后幾小時的日期時間 * * @param date基準(zhǔn)日期yyyy-MM-dd HH:mm:ss * @param dayMark找基準(zhǔn)日期 * +代表往后,-代表往前 * @return * @throws Exception * * */ public static String getOtherHour(String date, int dayMark) throws Exception { DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date dt = df.parse(date); Calendar c = Calendar.getInstance(); c.setTime(dt); c.add(Calendar.HOUR_OF_DAY, dayMark); String mDateTime = df.format(c.getTime()); String strStart = mDateTime; return strStart; } /** * 獲取前后分鐘數(shù) * * @param date yyyy-MM-dd HH:mm:ss * @param minuteMark 前后標(biāo)識+-數(shù)值 * @return 返回 * @throws Exception */ public static String getOtherMinute(String date, int minuteMark) throws Exception { DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date dt = df.parse(date); Calendar c = Calendar.getInstance(); c.setTime(dt); c.add(Calendar.MINUTE, minuteMark); String mDateTime = df.format(c.getTime()); String strStart = mDateTime; return strStart; } /** * 解析字符串為Date類型 * @param date 要被解析的日期字符串 * @param pattern 類型格式,默認(rèn)yyyy-MM-dd HH:mm:ss * @return Date 被解析后的日期 * @throws Exception */ public static Date parseDate(String date, String pattern) throws Exception { Date returnDate = null; if (pattern == null || pattern.equals("") || pattern.equals("null")) { pattern = "yyyy-MM-dd HH:mm:ss"; } java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(pattern); try { returnDate = sdf.parse(date); } catch (Exception e) { e.printStackTrace(); } return returnDate; } /** * 格式化Date類型日期 * @param date Date類型日期 * @param pattern 類型格式 * @return String,被格式化后的日期 * @throws Exception */ public static String formatDate(Date date, String pattern) throws Exception { String returnDate = null; if (date == null) { return ""; } if (pattern == null || pattern.equals("") || pattern.equals("null")) { pattern = "yyyy-MM-dd HH:mm:ss"; } java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(pattern); returnDate = sdf.format(date); return returnDate; } /** * 得到當(dāng)前月份yyyy-MM; * * @return String */ public static String getSystemMonth() { java.util.Date date = new java.util.Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM"); String mDateTime1 = formatter.format(date); return mDateTime1; } /** * 獲取月所在年的最后一個月 * @param month 月份 * @param m * @return * @throws Exception */ public static String getYearLastMonth(String month, int m) throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); Date newDate = new Date(); newDate = format.parse(month); Calendar c = Calendar.getInstance(); c.setTime(newDate); c.add(Calendar.YEAR, m); newDate.setTime(c.getTimeInMillis()); return format.format(newDate).substring(0, 4) + "-12"; } /** * 獲取前后月份 * + 往后推遲m月 * * @param month * @param m * @return * @throws Exception */ public static String getOtherMonth(String month, int m) throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); Date newDate = new Date(); newDate = format.parse(month); Calendar c = Calendar.getInstance(); c.setTime(newDate); c.add(Calendar.MONTH, m); newDate.setTime(c.getTimeInMillis()); return format.format(newDate); } /** * 獲取前后月份+ 往后推遲m月 * * @param month * @param m * @return * @throws Exception */ public static String getOtherMonthYMD(String month, int m) throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date newDate = new Date(); newDate = format.parse(month); Calendar c = Calendar.getInstance(); c.setTime(newDate); c.add(Calendar.MONTH, m); newDate.setTime(c.getTimeInMillis()); return format.format(newDate); } /** * 根據(jù)年月字符串得到那個月的總天數(shù) * @param monthStr yyyy-MM * @return int 總天數(shù) * @throws Exception */ public static int getDaysOfMonth(String monthStr) throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); Date date = format.parse(monthStr); Calendar calendar = new GregorianCalendar(); calendar.setTime(date); int dayNum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); return dayNum; } /** * 獲取指定時刻前后指定步長的時刻 * * @param time * 時刻HH:mm:ss * @param m * 步長(+-m) 0:輸出原來的時刻,+1輸出后一個時刻,-1輸出前一個時刻 * @return String HH:mm:ss * @throws Exception */ public static String getBeforeAfterTimeForMin(String time, int m) throws Exception { Calendar now = Calendar.getInstance(); SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); Date newDate = new Date(); newDate = format.parse(time); now.setTime(newDate); now.add(Calendar.MINUTE, m); return format.format(now.getTime()); } /** * 獲取指定格式的前后時間 * * @param time * @param m * (0:輸出原來的時刻,+1輸出后一個時刻,-1輸出前一個時刻) * @param pattern * 類型的格式必須被time的格式所包含 * @return * @throws Exception */ public static String getBeforeAfterDateTimeByTime(String time, int m, String pattern) throws Exception { Calendar now = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(pattern); Date newDate = new Date(); newDate = sdf.parse(time); now.setTime(newDate); now.add(Calendar.MINUTE, m); return sdf.format(now.getTime()); } }
這就是整個工具類了,供君參考。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。
新聞熱點
疑難解答
圖片精選