本文实例为大家分享了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. java一个方法排他调用_Java编程实现排他锁代码详解

    一 .前言 某年某月某天,同事说需要一个文件排他锁功能,需求如下: (1)写操作是排他属性 (2)适用于同一进程的多线程/也适用于多进程的排他操作 (3)容错性:获得锁的进程若Crash,不影响到后续 ...

  2. java中 enum什么意思_Java中枚举Enum的使用详解

    在某些情况下,一个类的对象时有限且固定的,如季节类,它只有春夏秋冬4个对象这种实例有限且固定的类,在 Java 中被称为枚举类: 在 Java 中使用 enum 关键字来定义枚举类,其地位与 clas ...

  3. java原生类型没有封装_Java基本数据类型与封装类型详解(int和Integer区别)

    Java基本数据类型与封装类型详解(int和Integer区别) 发布于 2020-4-19| 复制链接 摘记: int是java提供的8种原始数据类型之一.Java为每个原始类型提供了封装类,Int ...

  4. java 封装表单数据类型_Java基本数据类型与封装类型详解(int和Integer区别)

    int是java提供的8种原始数据类型之一. Java为每个原始类型提供了封装类,Integer是java为int提供的封装类(即Integer是一个java对象,而int只是一个基本数据类型).in ...

  5. java工厂模式静态工厂_Java设计模式之静态工厂模式详解

    本文实例讲述了Java设计模式之静态工厂模式.分享给大家供大家参考,具体如下: 静态工厂模式(static factory)也叫简单工厂模式. 涉及到3个角色:工厂类角色,抽象产品类角色和具体产品类角 ...

  6. java this关键字的使用_Java this 关键字的使用方法详解

    Java this 关键字的使用方法详解 构造方法中的this关键字 构造方法是一个类的对象在通过new关键字创建时自动调用的,在程序中不能向调用其他方法一样通过方法名(也就是类名)来调用.但如果一个 ...

  7. java怎么导入包语句_java包(package),包导入详解

    之前给大家介绍了一下java包包定义的内容,下面的话,就接着来给大家讲一下包导入的内容,一起来了解一下吧. 假如,使用不同包当中的其他类,就要使用这个类的全名,也就是包名加上类名,下面是代码:exam ...

  8. java 获取oracle表结构_Java导出oracle表结构实例详解

    Java导出oracle表结构实例详解 发布于 2020-7-20| 复制链接 摘记:  Java导出oracle表结构实例详解最近用到的,因为plsql是收费的,不让用,找了很多方法终于发现了这个. ...

  9. java case 多个值_Java switch多值匹配操作详解

    这篇文章主要介绍了Java switch多值匹配操作详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 我们都知道 switch 用来走流程分支,大 ...

  10. java集合框架的接口_Java集合框架之Collection接口详解

    Java是一门面向对象的语言,那么我们写程序的时候最经常操作的便是对象了,为此,Java提供了一些专门用来处理对象的类库,这些类库的集合我们称之为集合框架.Java集合工具包位于Java.util包下 ...

最新文章

  1. 关于androidManifest.xml的概叙以及intent-filter的详细分析
  2. 计算机职称 计算机二级证,国家计算机二级证书含金量有多高
  3. 剑指Offer - 面试题60. n个骰子的点数(动态规划)
  4. 前端:CSS/14/综合案例:传智首页
  5. BCG-MFC 库对《支持重新启动管理器》都做了些什么
  6. Spring Data Jpa 审计功能
  7. 、简述global关键字的作用_二十三、Python变量作用域(局部变量和全局变量)
  8. 【OpenCV/C++】KNN算法识别数字的实现原理与代码详解
  9. windbg入门之旅:(2)一个简单的integer divide-by-zero exception分析案例
  10. Delphi 重启应用程序
  11. 029--PM话术模板
  12. mysql 合服_风云私服合区的方法详解(mysql数据库合并)
  13. ubuntu20.04安装并运行ORB_SLAM3(一路顺风版)
  14. 论文阅读Construction of Refined Protein Interaction Network for Predicting Essential Proteins
  15. 【Python 实战基础】Pandas如何统筛选复制某个数据
  16. 2021算法竞赛入门班第一节课枚举贪心习题
  17. 语音转文字怎么转?三个方法让你学会怎么语音转文字
  18. 爪哇国新游记之二十五----图及其遍历查找
  19. GameObject.Find()能否查找隐藏属性的游戏对象?
  20. linux下dhcp服务器分配出去的IP地址及剩余IP地址

热门文章

  1. python考试名词解释_python公开课|python专有名词居然有这么多,python专有名词解释已做好...
  2. vuedraggle choose_如何拆分员工工资条,教你一招一学就会。(五)函数CHOOSE
  3. 李宏毅机器学习(八)ELMo、BERT、GPT、XLNet、MASS、BART、UniLM、ELECTRA、others
  4. Hystrix降级逻辑中如何获取触发的异常 1
  5. 阿里P8架构师谈:什么是缓存雪崩?服务器雪崩的场景与解决方案
  6. 【Java】列表、集合、哈希表和可变参数
  7. 课程设计-毕业设计-JAVA画板课程设计---总之岁月漫长,然而值得等待。
  8. Dubbo:com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method 问题的解决
  9. MySql笔记:Can't create table 'mydb3.#sql-f48_1' (errno: 150
  10. mysql5.7 备份