Java日期处理工具类DateUtils详解

 更新时间:2020年06月28日 09:40:04   作者:一汪清水  
这篇文章主要为大家详细介绍了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 Spring事务使用及验证过程详解

    Java Spring事务使用及验证过程详解

    这篇文章主要介绍了Java Spring事务使用及验证过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-12-12
  • Java 数据结构深入理解ArrayList与顺序表

    Java 数据结构深入理解ArrayList与顺序表

    ArrayList 类是一个可以动态修改的数组,与普通数组的区别就是它是没有固定大小的限制,我们可以添加或删除元素。ArrayList 继承了 AbstractList ,并实现了 List 接口,顺序表是将元素顺序地存放在一块连续的存储区里,元素间的顺序关系由它们的存储顺序自然表示
    2022-04-04
  • SpringBoot使用GTS的示例详解

    SpringBoot使用GTS的示例详解

    这篇文章主要介绍了SpringBoot使用GTS的示例详解,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-10-10
  • spring boot请求异常处理并返回对应的html页面

    spring boot请求异常处理并返回对应的html页面

    这篇文章主要介绍了spring boot处理请求异常并返回对应的html页面,包括404异常处理和500异常处理,需要的朋友可以参考下
    2017-07-07
  • Java面试题之MD5加密的安全性详解

    Java面试题之MD5加密的安全性详解

    MD5 是 Message Digest Algorithm 的缩写,译为信息摘要算法,它是 Java 语言中使用很广泛的一种加密算法。本文将通过示例讨论下MD5的安全性,感兴趣的可以了解一下
    2022-10-10
  • idea如何自定义代码模板

    idea如何自定义代码模板

    这篇文章主要介绍了idea如何自定义代码模板问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • Java Guava的使用技巧整理

    Java Guava的使用技巧整理

    Guava是Google发布的一个开源库,主要提供了一些在Java开发中非常有用的工具类和API,不管是工作还是学习都是非常值得我们去熟悉的,一起来看看吧
    2023-03-03
  • Spring @Scheduler使用cron表达式时的执行问题详解

    Spring @Scheduler使用cron表达式时的执行问题详解

    Spring给程序猿们带来了许多便利。下面这篇文章主要给大家介绍了关于Spring @Scheduler使用cron表达式时的执行问题的相关资料,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧
    2018-09-09
  • 详解SpringBoot2.0的@Cacheable(Redis)缓存失效时间解决方案

    详解SpringBoot2.0的@Cacheable(Redis)缓存失效时间解决方案

    这篇文章主要介绍了详解SpringBoot2.0的@Cacheable(Redis)缓存失效时间解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • Java 字符串反转实现代码

    Java 字符串反转实现代码

    这篇文章主要介绍了 Java 字符串反转实现代码的相关资料,需要的朋友可以参考下
    2017-03-03

最新评论