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线程的6种状态及转化方式

    Java线程的6种状态及转化方式

    本文详细介绍了Java线程的六种状态以及状态之间的转换关系,线程状态包括NEW(新建)、RUNNABLE(运行)、BLOCKED(阻塞)、WAITING(等待)、TIMED_WAITING(超时等待)和TERMINATED(终止)
    2024-09-09
  • springData使用QueryDsl的示例代码

    springData使用QueryDsl的示例代码

    这篇文章主要介绍了springData使用QueryDsl的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • SpringBoot整合邮件发送的四种方法

    SpringBoot整合邮件发送的四种方法

    这篇文章主要介绍了SpringBoot整合邮件发送的四种方法,SpringBoot中集成了发送邮件的功能,本文做了进一步优化,需要的朋友可以参考下
    2023-03-03
  • Spring使用支付宝扫码支付

    Spring使用支付宝扫码支付

    这篇文章主要为大家详细介绍了Spring使用支付宝扫码支付的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • Springboot+rabbitmq实现延时队列的两种方式

    Springboot+rabbitmq实现延时队列的两种方式

    这篇文章主要介绍了Springboot+rabbitmq实现延时队列的两种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • java面向对象的三大特性之一继承用法实例分析

    java面向对象的三大特性之一继承用法实例分析

    这篇文章主要介绍了java面向对象的三大特性之一继承用法,结合实例形式分析了java面向对象程序设计中继承的基本原理与具体使用方法,需要的朋友可以参考下
    2019-11-11
  • Java实现浪漫流星表白的示例代码

    Java实现浪漫流星表白的示例代码

    本文将利用Java语言实现浪漫流星表白,可以实现这些功能:播放音乐、自定义流星数量、飞行速度、光晕大小、流星大小,自定义表白话语,感兴趣的可以学习一下
    2022-05-05
  • 五分钟手撸一个Spring容器(萌芽版)

    五分钟手撸一个Spring容器(萌芽版)

    Spring的两大内核分别是IOC和AOP,其中最最核心的是IOC。这篇文章主要介绍了五分钟,手撸一个Spring容器的相关知识,需要的朋友可以参考下
    2022-03-03
  • SpringBoot整合Java DL4J实现文本分类系统

    SpringBoot整合Java DL4J实现文本分类系统

    在当今信息爆炸的时代,自然语言处理领域中的文本分类显得尤为重要,文本分类能够高效地组织和管理海量的文本数据,随着互联网的飞速发展,我们每天都被大量的文本信息所包围,本文将介绍如何使用 Spring Boot 整合 Java Deeplearning4j 来构建一个文本分类系统
    2024-10-10
  • 一篇文章带你入门java模板模式

    一篇文章带你入门java模板模式

    这篇文章主要为大家详细介绍了java模板模式的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08

最新评论