最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java日期處理工具類DateUtils詳解

 更新時(shí)間:2020年06月28日 09:40:04   作者:一汪清水  
這篇文章主要為大家詳細(xì)介紹了Java日期處理工具類DateUtils的相關(guān)代碼,包含日期和時(shí)間常用操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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)文章

  • Java線程的6種狀態(tài)及轉(zhuǎn)化方式

    Java線程的6種狀態(tài)及轉(zhuǎn)化方式

    本文詳細(xì)介紹了Java線程的六種狀態(tài)以及狀態(tài)之間的轉(zhuǎn)換關(guān)系,線程狀態(tài)包括NEW(新建)、RUNNABLE(運(yùn)行)、BLOCKED(阻塞)、WAITING(等待)、TIMED_WAITING(超時(shí)等待)和TERMINATED(終止)
    2024-09-09
  • springData使用QueryDsl的示例代碼

    springData使用QueryDsl的示例代碼

    這篇文章主要介紹了springData使用QueryDsl的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • SpringBoot整合郵件發(fā)送的四種方法

    SpringBoot整合郵件發(fā)送的四種方法

    這篇文章主要介紹了SpringBoot整合郵件發(fā)送的四種方法,SpringBoot中集成了發(fā)送郵件的功能,本文做了進(jìn)一步優(yōu)化,需要的朋友可以參考下
    2023-03-03
  • Spring使用支付寶掃碼支付

    Spring使用支付寶掃碼支付

    這篇文章主要為大家詳細(xì)介紹了Spring使用支付寶掃碼支付的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Springboot+rabbitmq實(shí)現(xiàn)延時(shí)隊(duì)列的兩種方式

    Springboot+rabbitmq實(shí)現(xiàn)延時(shí)隊(duì)列的兩種方式

    這篇文章主要介紹了Springboot+rabbitmq實(shí)現(xiàn)延時(shí)隊(duì)列的兩種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • java面向?qū)ο蟮娜筇匦灾焕^承用法實(shí)例分析

    java面向?qū)ο蟮娜筇匦灾焕^承用法實(shí)例分析

    這篇文章主要介紹了java面向?qū)ο蟮娜筇匦灾焕^承用法,結(jié)合實(shí)例形式分析了java面向?qū)ο蟪绦蛟O(shè)計(jì)中繼承的基本原理與具體使用方法,需要的朋友可以參考下
    2019-11-11
  • Java實(shí)現(xiàn)浪漫流星表白的示例代碼

    Java實(shí)現(xiàn)浪漫流星表白的示例代碼

    本文將利用Java語(yǔ)言實(shí)現(xiàn)浪漫流星表白,可以實(shí)現(xiàn)這些功能:播放音樂(lè)、自定義流星數(shù)量、飛行速度、光暈大小、流星大小,自定義表白話語(yǔ),感興趣的可以學(xué)習(xí)一下
    2022-05-05
  • 五分鐘手?jǐn)]一個(gè)Spring容器(萌芽版)

    五分鐘手?jǐn)]一個(gè)Spring容器(萌芽版)

    Spring的兩大內(nèi)核分別是IOC和AOP,其中最最核心的是IOC。這篇文章主要介紹了五分鐘,手?jǐn)]一個(gè)Spring容器的相關(guān)知識(shí),需要的朋友可以參考下
    2022-03-03
  • SpringBoot整合Java DL4J實(shí)現(xiàn)文本分類系統(tǒng)

    SpringBoot整合Java DL4J實(shí)現(xiàn)文本分類系統(tǒng)

    在當(dāng)今信息爆炸的時(shí)代,自然語(yǔ)言處理領(lǐng)域中的文本分類顯得尤為重要,文本分類能夠高效地組織和管理海量的文本數(shù)據(jù),隨著互聯(lián)網(wǎng)的飛速發(fā)展,我們每天都被大量的文本信息所包圍,本文將介紹如何使用 Spring Boot 整合 Java Deeplearning4j 來(lái)構(gòu)建一個(gè)文本分類系統(tǒng)
    2024-10-10
  • 一篇文章帶你入門java模板模式

    一篇文章帶你入門java模板模式

    這篇文章主要為大家詳細(xì)介紹了java模板模式的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08

最新評(píng)論

抚松县| 万年县| 赣州市| 南京市| 庐江县| 龙川县| 钦州市| 枣阳市| 安福县| 新乡市| 阳朔县| 平阴县| 松原市| 南涧| 乌拉特中旗| 张北县| 望奎县| 北安市| 大港区| 门源| 双流县| 兴仁县| 安溪县| 布尔津县| 温宿县| 嘉禾县| 定州市| 保亭| 鹤山市| 新河县| 白朗县| 新丰县| 深泽县| 香格里拉县| 牡丹江市| 石阡县| 白山市| 鄢陵县| 岐山县| 郓城县| 昌黎县|