Java日期處理工具類DateUtils詳解
本文實(shí)例為大家分享了Java日期處理工具類DateUtils的具體代碼,供大家參考,具體內(nèi)容如下
import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * <日期時(shí)間處理工具類> */ public class DateUtils { /** * Date format pattern this is often used. */ public static final String PATTERN_YMD = "yyyy-MM-dd"; /** * Date format pattern this is often used. */ public static final String PATTERN_YMDHMS="yyyy-MM-dd HH:mm:ss"; /** * Formats the given date according to the YMD pattern. * * @param date The date to format. * @return An YMD formatted date string. * * @see #PATTERN_YMD */ public static String formatDate(Date date) { return formatDate(date, PATTERN_YMD); } /** * Formats the given date according to the specified pattern. The pattern * must conform to that used by the {@link SimpleDateFormat simple date * format} class. * * @param date The date to format. * @param pattern The pattern to use for formatting the date. * @return A formatted date string. * * @throws IllegalArgumentException If the given date pattern is invalid. * * @see SimpleDateFormat */ public static String formatDate(Date date, String pattern) { if (date == null) throw new IllegalArgumentException("date is null"); if (pattern == null) throw new IllegalArgumentException("pattern is null"); SimpleDateFormat formatter = new SimpleDateFormat(pattern); return formatter.format(date); } /** * Parses a date value. The format used for parsing the date value are retrieved from * the default PATTERN_YMD. * * @param dateValue the date value to parse * * @return the parsed date * * @throws IllegalArgumentException If the given dateValue is invalid. */ public static Date parseDate(String dateValue) { return parseDate(dateValue, null); } /** * Parses the date value using the given date format. * * @param dateValue the date value to parse * @param dateFormat the date format to use * * @return the parsed date. if parse is failed , return null * * @throws IllegalArgumentException If the given dateValue is invalid. */ public static Date parseDate(String dateValue, String dateFormat) { if (dateValue == null) { throw new IllegalArgumentException("dateValue is null"); } if (dateFormat == null) { dateFormat = PATTERN_YMD; } SimpleDateFormat df = new SimpleDateFormat(dateFormat); Date result = null; try { result = df.parse(dateValue); } catch (ParseException pe) { pe.printStackTrace();// 日期型字符串格式錯(cuò)誤 } return result; } /** * Adds a number of years to a date returning a new object. * The original date object is unchanged. * * @param date the date, not null * @param amount the amount to add, may be negative * @return the new date object with the amount added * @throws IllegalArgumentException if the date is null */ public static Date addYears(Date date, int amount) { return add(date, Calendar.YEAR, amount); } /** * Adds a number of years to a timestamp returning a new object. * The original timestamp object is unchanged. * * @param timestamp the timestamp, not null * @param amount the amount to add, may be negative * @return the new timestamp object with the amount added * @throws IllegalArgumentException if the timestamp is null */ public static Timestamp addYears(Timestamp timestamp, int amount) { return add(timestamp, Calendar.YEAR, amount); } //----------------------------------------------------------------------- /** * Adds a number of months to a date returning a new object. * The original date object is unchanged. * * @param date the date, not null * @param amount the amount to add, may be negative * @return the new date object with the amount added * @throws IllegalArgumentException if the date is null */ public static Date addMonths(Date date, int amount) { return add(date, Calendar.MONTH, amount); } /** * Adds a number of months to a timestamp returning a new object. * The original timestamp object is unchanged. * * @param timestamp the timestamp, not null * @param amount the amount to add, may be negative * @return the new timestamp object with the amount added * @throws IllegalArgumentException if the timestamp is null */ public static Timestamp addMonths(Timestamp timestamp, int amount) { return add(timestamp, Calendar.MONTH, amount); } //----------------------------------------------------------------------- /** * Adds a number of days to a date returning a new object. * The original date object is unchanged. * * @param date the date, not null * @param amount the amount to add, may be negative * @return the new date object with the amount added * @throws IllegalArgumentException if the date is null */ public static Date addDays(Date date, int amount) { return add(date, Calendar.DATE, amount); } /** * Adds a number of days to a timestamp returning a new object. * The original timestamp object is unchanged. * * @param timestamp the timestamp, not null * @param amount the amount to add, may be negative * @return the new timestamp object with the amount added * @throws IllegalArgumentException if the timestamp is null */ public static Timestamp addDays(Timestamp timestamp, int amount) { return add(timestamp, Calendar.DATE, amount); } //----------------------------------------------------------------------- /** * Adds a number of minutes to a timestamp returning a new object. * The original timestamp object is unchanged. * * @param timestamp the timestamp, not null * @param amount the amount to add, may be negative * @return the new timestamp object with the amount added * @throws IllegalArgumentException if the timestamp is null */ public static Timestamp addMinutes(Timestamp timestamp, int amount) { return add(timestamp, Calendar.MINUTE, amount); } /** * Adds a number of days to current time returning a new object. * * @param amount the amount to add, may be negative * @return the new timestamp object with the amount added */ public static Timestamp addDays(int amount) { Calendar c = Calendar.getInstance(); c.add(Calendar.DATE, amount); return new Timestamp(c.getTimeInMillis()); } //----------------------------------------------------------------------- /** * Adds to a date returning a new object. * The original date object is unchanged. * * @param date the date, not null * @param calendarField the calendar field to add to * @param amount the amount to add, may be negative * @return the new date object with the amount added * @throws IllegalArgumentException if the date is null */ private static Date add(Date date, int calendarField, int amount) { if (date == null) { throw new IllegalArgumentException("The date must not be null"); } Calendar c = Calendar.getInstance(); c.setTime(date); c.add(calendarField, amount); return c.getTime(); } /** * Adds to a timestamp returning a new object. * The original timestamp object is unchanged. * * @param timestamp the timestamp, not null * @param calendarField the calendar field to add to * @param amount the amount to add, may be negative * @return the new timestamp object with the amount added * @throws IllegalArgumentException if the timestamp is null */ private static Timestamp add(Timestamp timestamp, int calendarField, int amount) { if (timestamp == null) { throw new IllegalArgumentException("The timestamp must not be null"); } Calendar c = Calendar.getInstance(); c.setTime(timestamp); c.add(calendarField, amount); return new Timestamp(c.getTimeInMillis()); } /** * <生成最小的當(dāng)天日期值> * @return 最小的當(dāng)天日期值 */ public static Timestamp now() { Calendar c = Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); return new Timestamp(c.getTimeInMillis()); } /** This class should not be instantiated. */ private DateUtils() { } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Nacos?動(dòng)態(tài)服務(wù)發(fā)現(xiàn)、配置和服務(wù)管理平臺(tái)初體驗(yàn)
這篇文章主要介紹了Nacos?動(dòng)態(tài)服務(wù)發(fā)現(xiàn)、配置和服務(wù)管理平臺(tái)初體驗(yàn)的相關(guān)資料,需要的朋友可以參考下2022-09-09SpringBoot+Redis實(shí)現(xiàn)布隆過(guò)濾器的示例代碼
本文主要介紹了SpringBoot+Redis實(shí)現(xiàn)布隆過(guò)濾器的示例代碼,文中根據(jù)實(shí)例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03SpringBoot @PostConstruct原理用法解析
這篇文章主要介紹了SpringBoot @PostConstruct原理用法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08Java 多線程并發(fā)AbstractQueuedSynchronizer詳情
這篇文章主要介紹了Java 多線程并發(fā)AbstractQueuedSynchronizer詳情,文章圍繞主題展開(kāi)想象的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-06-06Mybatis示例講解注解開(kāi)發(fā)中的單表操作
這篇文章主要介紹了使用Mybatis對(duì)數(shù)據(jù)庫(kù)進(jìn)行單表操作的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Java超詳細(xì)教你寫一個(gè)銀行存款系統(tǒng)案例
這篇文章主要介紹了怎么用Java來(lái)寫一個(gè)銀行的存款系統(tǒng),銀行存款主要有賬號(hào)和存款金額兩個(gè)屬性,感興趣的朋友跟隨文章往下看看吧2022-03-03利用ClasserLoader實(shí)現(xiàn)jar包加載并調(diào)用里面的方法
classloader即是類加載,虛擬機(jī)把描述類的數(shù)據(jù)從class字節(jié)碼文件加載到內(nèi)存,并對(duì)數(shù)據(jù)進(jìn)行檢驗(yàn)、轉(zhuǎn)換解析和初始化,了解java的類加載機(jī)制,可以快速解決運(yùn)行時(shí)的各種加載問(wèn)題并快速定位其背后的本質(zhì)原因,本文介紹了如何利用ClasserLoader來(lái)實(shí)現(xiàn)jar包加載并調(diào)用里面的方法2024-09-09