日期型別的轉換與加減
阿新 • • 發佈:2019-01-09
工具類可直接通過類名呼叫方法,不需要建立一個物件來進行呼叫工具類的方法,因為該工具類裡的方法都是靜態的方法(static)
靜態方法,比較適合工具類、靜態工廠等,必須考慮多執行緒問題。靜態方法屬於類本身,而非類的例項,所以可以用“類名.方法名”呼叫
DateUtils time = new DateUtils();
//計算相差天數 new Date() 獲取當前日期
int days = time.compareDay(new Date(), pay.getPlanPayTimechange());
//計算相差天數 new Date() 獲取當前日期
int days = DateUtils.compareDay(new Date(), pay.getPlanPayTimechange());
import java.sql.Timestamp; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.Random; /** * 日期處理 * * @author Howesen [email protected] * */ public class DateUtils { /** * 長日期格式化:yyyy-MM-dd HH:mm:ss */ public static String FORMATTER_L = "yyyy-MM-dd HH:mm:ss"; /** * 公安部要求的日期格式 */ public static String DATESTR_FORMATTER_L = "yyyyMMddHHmmss"; /** * 短日期格式化:yyyy-MM-dd */ public static String FORMATTER_S = "yyyy-MM-dd"; /** * 開始時間字尾 */ public static String DATE_BEGIN= "000000"; /** * 結束時間字尾 */ public static String DATE_END = "235959"; /** * 自定義格式化日期輸出 * * @param date * @param format * @return */ public static String doFormatDate(Date date, String format) { return (new SimpleDateFormat(format)).format(date); } /** * 將日期格式化為"yyyy-MM"字串 * * @param date * @return */ public static String formatYearMonth(Date date) { return doFormatDate(date, "yyyy-MM"); } /** * 給一個日期加上N天或減去N天得到一個新的日期 * * @param startDate * 需要增加的日期時間 * @param addnos * 新增的天數,可以是正數也可以是負數 * @return 操作後的日期 */ public static Date addDay(Date startDate, int addnos) { if (startDate == null) return null; Calendar cc = Calendar.getInstance(); cc.setTime(startDate); cc.add(Calendar.DATE, addnos); return cc.getTime(); } /** * 給一個日期加上N月後或減去N月後得到的一個新日期 * * @param startDate * 需要增加的日期時間 * @param addnos * 新增的月數,可以是正數也可以是負數 * @return 操作後的日期 */ public static Date addMonth(Date startDate, int addnos) { if (startDate == null) return null; Calendar cc = Calendar.getInstance(); cc.setTime(startDate); cc.add(Calendar.MONTH, addnos); return cc.getTime(); } /** * 給一個日期加上N年後或減去N年後得到的一個新日期 * * @param startDate * 需要增加的日期時間 * @param adddays * 新增的年數,可以是正數也可以是負數 * @return 操作後的日期 */ public static Date addYear(Date startDate, int addnos) { if (startDate == null) return null; Calendar cc = Calendar.getInstance(); cc.setTime(startDate); cc.add(Calendar.YEAR, addnos); return cc.getTime(); } /** * 給一個日期加上N年後或減去N年後得到的一個新日期 * * @param startDate * 需要增加的日期時間 * @param adddays * 新增的年數,可以是正數也可以是負數 * @return 操作後的日期 */ public static Date addYear(Date startDate, int addnos) { if (startDate == null) return null; Calendar cc = Calendar.getInstance(); cc.setTime(startDate); cc.add(Calendar.YEAR, addnos); return cc.getTime(); } /** * 計算兩個日期相差的月數 * * @param st * 起始日期 * @param end * 結束日期 * @return */ public static int compareMonth(Date st, Date end) { int y = Math.abs((getYear(end) < 0 ? 0 : getYear(end)) - (getYear(st) < 0 ? 0 : getYear(st))); int m = 0; if (y > 0) { y--; m = Math.abs(12 - getMonth(st) + getMonth(end)); } else { m = Math.abs(getMonth(end) - getMonth(st)); } return (y * 12) + m; } /** * * compareDay:(計算兩個日期相差的天數). <br/> * @author zhenwang9 * @param start * @param end * @return * @since JDK 1.6 */ public static int compareDay(Date start, Date end){ if(start == null || end ==null || start.after(end) ){ return 0; }else{ long compare = compare(start,end); return (int) (compare/1000/3600/24); } } /** * 計算兩個日期相差的毫秒數 * * @param start * 啟始時間 * @param end * 結束時間 * @return */ public static long compare(Date start, Date end) { if (start != null && end != null) { return end.getTime() - start.getTime(); } return 0l; } /** * 得到當前系統的日期或時間 * * @param b * 為true 時返回詳細時間格式,為false時返回日期格式,不含時分秒 * @return 當前的日期或時間 */ public static String getDates(boolean b) { return doFormatDate(new Date(), b); } /** * 將java.util.Date型別的日期格式轉換成java.util.Calendar格式的日期 * * @param dd * @return */ public static Calendar DateToCalendar(Date dd) { Calendar cc = Calendar.getInstance(); cc.setTime(dd); return cc; } /** * 將一個長整型資料轉為日期 * * @param datenum * @return */ public static Date longToDate(long datenum) { Calendar cc = Calendar.getInstance(); cc.setTimeInMillis(datenum); return cc.getTime(); } /** * 將一個長整型資料轉為日期格式的字串 * * @param datenum * @return */ public static String longToDateString(long datenum) { return doFormatDate(longToDate(datenum), true); } /** * 將日期格式轉為java.sql.Date * * @param de * @return */ public static java.sql.Date dateToSqlDate(Date de) { return new java.sql.Date(de.getTime()); } /** * 格式化日期字串 yyyymmddhh24miss * * @param date * @return */ public static String formatDS(String date) { if (date == null) return ""; return date.replace("-", "").replace(":", "").replace(" ", ""); } /** * 將公安部的時間格式轉為日期格式 * * @param datestr * @return */ public static Date strTdate(String datestr) { return str2Date(datestr, SystemConfig.DATESTR_FORMATTER_L); } /** * 將長時間格式轉為日期格式 * * @param datestr * @return */ public static Date strLTdate(String datestr) { return str2Date(datestr, SystemConfig.FORMATTER_L); } /** * 將短時間格式轉為日期格式 * * @param datestr * @return */ public static Date strSTdate(String datestr) { return str2Date(datestr, SystemConfig.FORMATTER_S); } }