热门标签 | HotTags
当前位置:  开发笔记 > 数据库 > 正文

Java日期处理工具类DateUtils详解

这篇文章主要为大家详细介绍了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() { 
 } 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文介绍了如何在MySQL中将零值替换为先前的非零值的方法,包括使用内联查询和更新查询。同时还提供了选择正确值的方法。 ... [详细]
  • 本文详细介绍了MysqlDump和mysqldump进行全库备份的相关知识,包括备份命令的使用方法、my.cnf配置文件的设置、binlog日志的位置指定、增量恢复的方式以及适用于innodb引擎和myisam引擎的备份方法。对于需要进行数据库备份的用户来说,本文提供了一些有价值的参考内容。 ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • 本文由编程笔记小编整理,介绍了PHP中的MySQL函数库及其常用函数,包括mysql_connect、mysql_error、mysql_select_db、mysql_query、mysql_affected_row、mysql_close等。希望对读者有一定的参考价值。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • Java String与StringBuffer的区别及其应用场景
    本文主要介绍了Java中String和StringBuffer的区别,String是不可变的,而StringBuffer是可变的。StringBuffer在进行字符串处理时不生成新的对象,内存使用上要优于String类。因此,在需要频繁对字符串进行修改的情况下,使用StringBuffer更加适合。同时,文章还介绍了String和StringBuffer的应用场景。 ... [详细]
  • Oracle分析函数first_value()和last_value()的用法及原理
    本文介绍了Oracle分析函数first_value()和last_value()的用法和原理,以及在查询销售记录日期和部门中的应用。通过示例和解释,详细说明了first_value()和last_value()的功能和不同之处。同时,对于last_value()的结果出现不一样的情况进行了解释,并提供了理解last_value()默认统计范围的方法。该文对于使用Oracle分析函数的开发人员和数据库管理员具有参考价值。 ... [详细]
  • MyBatis错题分析解析及注意事项
    本文对MyBatis的错题进行了分析和解析,同时介绍了使用MyBatis时需要注意的一些事项,如resultMap的使用、SqlSession和SqlSessionFactory的获取方式、动态SQL中的else元素和when元素的使用、resource属性和url属性的配置方式、typeAliases的使用方法等。同时还指出了在属性名与查询字段名不一致时需要使用resultMap进行结果映射,而不能使用resultType。 ... [详细]
  • 本文详细介绍了在ASP.NET中获取插入记录的ID的几种方法,包括使用SCOPE_IDENTITY()和IDENT_CURRENT()函数,以及通过ExecuteReader方法执行SQL语句获取ID的步骤。同时,还提供了使用这些方法的示例代码和注意事项。对于需要获取表中最后一个插入操作所产生的ID或马上使用刚插入的新记录ID的开发者来说,本文提供了一些有用的技巧和建议。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • 本文介绍了通过mysql命令查看mysql的安装路径的方法,提供了相应的sql语句,并希望对读者有参考价值。 ... [详细]
author-avatar
鸟鸟212
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有