欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java日期時(shí)間操作工具類

 更新時(shí)間:2018年12月15日 15:51:47   作者:一掬凈土  
這篇文章主要為大家詳細(xì)介紹了java日期時(shí)間操作工具類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java日期時(shí)間操作工具類,供大家參考,具體內(nèi)容如下

雖然jdk1.8開始,加入了time包,里面對(duì)時(shí)區(qū),本地化時(shí)間,格式化,以及時(shí)間等做了很好的封裝,但仍然要寫一個(gè)工具類。大家看著用。應(yīng)該沒有bug。如果發(fā)現(xiàn)了,請(qǐng)您一定告知,互相學(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 日期時(shí)間工具類
 * @author wangdy
 */
public class TimeUtil {
 
 /**
 * 獲取當(dāng)前系統(tǒng)時(shí)間的小時(shí)(HH)
 * 
 * @return int 當(dāng)前系統(tǒng)小時(shí)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í)間的分鐘數(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í)間的秒數(shù)(ss)
 * 
 * @return int 當(dāng)前系統(tǒng)時(shí)間的秒數(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;
 }
 
 /**
 * 獲取兩個(gè)日期之間的天數(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));
 }
 
 /**
 * 獲取兩個(gè)月份之間的月數(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;
 }
 
 /**比較兩個(gè)日期
 * @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;
 }
 
 /**獲取兩個(gè)日期之間的分鐘數(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)時(shí)間的字符串 
 * 
 * @return String -- 當(dāng)天的整個(gè)日期字符串,年月日時(shí)分秒,返回格式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)時(shí)間的指定類型字符串 
 * @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)時(shí)間的字符串
 * 
 * @return String 當(dāng)天的時(shí)分秒字符串,返回格式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)時(shí)間的字符串
 * 
 * @return String -- 當(dāng)天的時(shí)分秒字符串,返回格式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)指定格式的時(shí)間的字符串
 * @param pattern 指定格式
 * @return String 當(dāng)前系統(tǒng)指定格式時(shí)間字符串
 * @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;
 }
 
 /**
 * 獲取指定日期的時(shí)刻字符串
 * 
 * @param dayStr
 *      (yyyy-MM-dd HH:mm:ss)
 * @return String -- 當(dāng)天的時(shí)分秒字符串,返回格式: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));
 }
 
 
 /**
 * 獲取輸入日期的前后幾小時(shí)的日期時(shí)間
 * 
 * @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í)+-數(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;
 }
 
 /**
 * 獲取月所在年的最后一個(gè)月
 * @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ù)年月字符串得到那個(gè)月的總天數(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;
 }
 
 /**
 * 獲取指定時(shí)刻前后指定步長(zhǎng)的時(shí)刻
 * 
 * @param time
 *      時(shí)刻HH:mm:ss
 * @param m
 *      步長(zhǎng)(+-m) 0:輸出原來的時(shí)刻,+1輸出后一個(gè)時(shí)刻,-1輸出前一個(gè)時(shí)刻
 * @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());
 }
 
 /**
 * 獲取指定格式的前后時(shí)間
 * 
 * @param time
 * @param m
 *      (0:輸出原來的時(shí)刻,+1輸出后一個(gè)時(shí)刻,-1輸出前一個(gè)時(shí)刻)
 * @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());
 }
 
}

這就是整個(gè)工具類了,供君參考。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 如何用java實(shí)現(xiàn)分頁查詢

    如何用java實(shí)現(xiàn)分頁查詢

    這篇文章主要介紹了如何用java實(shí)現(xiàn)分頁查詢,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • Lombok注解之@SuperBuilder--解決無法builder父類屬性問題

    Lombok注解之@SuperBuilder--解決無法builder父類屬性問題

    這篇文章主要介紹了Lombok注解之@SuperBuilder--解決無法builder父類屬性問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • 解決Required request body is missing錯(cuò)誤的問題

    解決Required request body is missing錯(cuò)誤的問題

    這篇文章主要介紹了解決Required request body is missing錯(cuò)誤的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 詳解springboot如何更新json串里面的內(nèi)容

    詳解springboot如何更新json串里面的內(nèi)容

    這篇文章主要為大家介紹了springboot 如何更新json串里面的內(nèi)容,文中有詳細(xì)的解決方案供大家參考,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2023-10-10
  • Java線程池框架核心代碼解析

    Java線程池框架核心代碼解析

    這篇文章主要針對(duì)Java線程池框架核心代碼進(jìn)行詳細(xì)解析,分析Java線程池框架的實(shí)現(xiàn)ThreadPoolExecutor,感興趣的小伙伴們可以參考一下
    2016-07-07
  • 解決FileWriter 寫入文本不換行的問題

    解決FileWriter 寫入文本不換行的問題

    這篇文章主要介紹了解決FileWriter 寫入文本不換行的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 深入理解Java設(shè)計(jì)模式之橋接模式

    深入理解Java設(shè)計(jì)模式之橋接模式

    這篇文章主要介紹了JAVA設(shè)計(jì)模式之橋接模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下
    2021-11-11
  • SpringBoot+微信小程序?qū)崿F(xiàn)文件上傳與下載功能詳解

    SpringBoot+微信小程序?qū)崿F(xiàn)文件上傳與下載功能詳解

    這篇文章主要為大家介紹了SpringBoot整合微信小程序?qū)崿F(xiàn)文件上傳與下載功能,文中的實(shí)現(xiàn)步驟講解詳細(xì),快跟隨小編一起學(xué)習(xí)一下吧
    2022-03-03
  • springboot @Controller和@RestController的區(qū)別及應(yīng)用詳解

    springboot @Controller和@RestController的區(qū)別及應(yīng)用詳解

    這篇文章主要介紹了springboot @Controller和@RestController的區(qū)別及應(yīng)用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Spring中的Actuator使用詳解

    Spring中的Actuator使用詳解

    這篇文章主要介紹了Spring中的Actuator使用詳解,在生產(chǎn)環(huán)境中運(yùn)行的程序,并不總是穩(wěn)定、安靜、正確的,往往會(huì)遇到各式各樣的現(xiàn)場(chǎng)狀況,這個(gè)時(shí)候,就需要獲取該程序足夠多的運(yùn)行狀態(tài)信息,然后分析并對(duì)其進(jìn)行有效管理,需要的朋友可以參考下
    2023-09-09

最新評(píng)論