日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java 日期处理工具类_Java日期处理工具类DateUtils详解

發布時間:2024/7/19 java 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 日期处理工具类_Java日期处理工具类DateUtils详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文實例為大家分享了Java日期處理工具類DateUtils的具體代碼,供大家參考,具體內容如下

import java.sql.Timestamp;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

/**

*

*/

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();// 日期型字符串格式錯誤

}

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());

}

/**

*

* @return 最小的當天日期值

*/

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() {

}

}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持聚米學院。

總結

以上是生活随笔為你收集整理的java 日期处理工具类_Java日期处理工具类DateUtils详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产精品1 | 精品少妇一区二区三区密爱 | 精品少妇一区二区三区在线观看 | 国产日韩精品电影 | 久久99精品久久久久子伦 | 白丝动漫美女 | 欧美影视 | 黄色小视频链接 | 欧美日韩国产电影 | 中文字幕一区二区三区免费 | 国产精品va在线观看无码 | 好吊妞在线观看 | 少妇被躁爽到高潮 | av无毛| 一级坐爱片 | 91原创视频在线观看 | 日本免费黄色 | 黄色av网站免费在线观看 | 黑人性生活视频 | 这里有精品 | 亚洲天堂av一区二区 | 日韩成人av网站 | 91精品人妻一区二区三区四区 | 91精品国产一区二区三区香蕉 | 亚洲视频在线观看一区 | 欧美日本高清视频 | 成人免费在线视频 | 成人性视频免费网站 | 日韩成人专区 | 久久免费视频2 | 玖玖爱这里只有精品 | 无码人妻丰满熟妇啪啪网站 | 久久都是精品 | 天天干天天操天天舔 | 日本一区二区三区久久久久 | 毛片av网址 | 尤物193.com | av电影在线播放 | 超碰下载页面 | 国产又粗又黄又爽视频 | 噜噜在线视频 | 亚洲Av无码成人精品区伊人 | 婷婷综合社区 | 超碰超碰超碰超碰 | 成人免费视频一区二区 | 日韩午夜网站 | 可以免费看的黄色网址 | 日本黄区免费视频观看 | 91精品久久久久久久久久久 | 天天操狠狠干 | 国语对白少妇spa私密按摩 | 国产天堂在线 | 欧美激情一区 | 巨物撞击尤物少妇呻吟 | 天天舔天天爱 | 北岛玲av | 日本老太婆做爰视频 | 成人理论影院 | 免费视频二区 | 97成人精品视频在线观看 | www.日日夜夜 | 999毛片 | 亚洲国产精品狼友在线观看 | 亚洲精品污一区二区三区 | 污污的视频在线观看 | 精品免费一区 | 奇米影视77777 | 国产精品爽 | 精品不卡一区二区三区 | 久久精品国产99精品国产亚洲性色 | 理论av | 日本精品二区 | 一区二区免费播放 | 国产大片b站 | 欧美精品免费播放 | 国产富婆一级全黄大片 | 国产盗摄精品一区二区酒店 | 欧美做爰猛烈床戏大尺度 | 91禁看片| 暴操白虎| 日韩一区在线观看视频 | 午夜神马福利 | 91香蕉视频污污 | 日韩精品一区二区在线观看 | 欧美特黄| 91亚洲精选| 久久国产精品网站 | 天天干天天操天天拍 | 波波野结衣 | 欧美在线免费观看 | 亚洲成a人片77777精品 | 同性色老头性xxxx老头 | 大尺度av| 美女黄视频网站 | 国产顶级毛片 | 成人欧美一区二区三区白人 | 国产一区二区精华 | 久久久久久久久99精品 | 国产1区2区|