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() {
 }
}

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

(0)

相关推荐

  • Java 中DateUtils日期工具类的实例详解

    Java 中DateUtils日期工具类的实例详解 介绍 在java中队日期类型的处理并不方便,通常都需要借助java.text.SimpleDateFormat类来实现日期类型 和字符串类型之间的转换,但是在jdk1.8之后有所改善,jdk1.7以及之前的版本处理日期类型并不方便, 可以借助Joda Time组件来处理,尤其是日期类型的一些数学操作就更是不方便. java代码 /** * * 日期工具类 java对日期的操作一直都很不理想,直到jdk1.8之后才有了本质的改变. * 如果使用的

  • Java日期工具类DateUtils实例详解

    在项目开发中,日期是我们必不可少的的一部分,本文将总结代码开发中的关于日期常用的一些方法,以方便自己后期使用.下面直接上菜了: package com.example.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.ut

  • java针对于时间转换的DateUtils工具类

    本文实例为大家分享了时间转换的DateUtils工具类,供大家参考,具体内容如下 import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Map; import net.sf.json.JSONObject; /** * 时间日期工具类 * *

  • java中DateUtils时间工具类详解

    本文实例为大家分享了DateUtils时间工具类的具体代码,供大家参考,具体内容如下 package com.example.administrator.myapp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * Date 工具类 * Created by lychun on 2017/12/07

  • java DateUtil工具类时间戳类型转换详解

    本文实例为大家分享了DateUtil工具类时间戳类型转换的具体代码,供大家参考,具体内容如下 package com.sinosoft.media.sms.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateUtil { //当前时间 //public static Date DATE_NOW=new Date(); /*

  • Java工具类DateUtils实例详解

    本文实例为大家分享了Java工具类DateUtils的具体代码,供大家参考,具体内容如下 import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 描述:公共日期工具类 */ public class DateUtils { public static String DATE_FORMAT = "yyyy-M

  • Java日期时间格式化操作DateUtils 的整理

    Java日期时间格式化操作DateUtils 的整理 直接上代码,总结了开发过程中经常用到的日期时间格式化操作! import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.concurrent.TimeUnit; /** * ClassName: DateUtils <br/> * D

  • Java使用DateUtils对日期进行数学运算经典应用示例【附DateUtils相关包文件下载】

    本文实例讲述了Java使用DateUtils对日期进行数学运算的方法.分享给大家供大家参考,具体如下: 最近在写数据上传的程序,需要对Date进行一些数学运算,个人感觉在java中,日期的数学运算还是比较常用的,所以把Date的数学运算都玩了一下.试了一下,发现DateUtils这个工具类对于Date的数学运算非常方便,见代码吧. package date; import java.text.SimpleDateFormat; import java.util.Date; import org.

  • 实例解析Java日期格式工具类DateUtil.java

    话不多说,请看代码: DateUtil.java package pers.kangxu.datautils.utils; import java.text.SimpleDateFormat; import java.util.Date; import pers.kangxu.datautils.common.exception.DefineException; /** * * <b> * 处理日期 工具类 * </b> * @author kangxu * */ public c

  • 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 { /** * Dat

  • Java多线程同步工具类CountDownLatch详解

    目录 简介 核心方法 CountDownLatch如何使用 CountDownLatch运行流程 运用场景 总结 简介 CountDownLatch是一个多线程同步工具类,在多线程环境中它允许多个线程处于等待状态,直到前面的线程执行结束.从类名上看CountDown既是数量递减的意思,我们可以把它理解为计数器. 核心方法 countDown():计数器递减方法. await():使调用此方法的线程进入等待状态,直到计数器计数为0时主线程才会被唤醒. await(long, TimeUnit):在

  • Java操作XML工具类XmlUtil详解

    本文实例为大家分享了Java操作XML工具类的具体代码,供大家参考,具体内容如下 一.代码 public class XmlUtil { /** * 将XML文件输出到指定的路径 * * @param doc * @param fileName * @throws Exception */ public static void outputXml(Document doc, String fileName) throws Exception { TransformerFactory tf = T

  • javaScript日期工具类DateUtils详解

    本文实例为大家分享了javaScript日期工具类的具体代码,供大家参考,具体内容如下 DateUtils = { patterns: { PATTERN_ERA: 'G', //Era 标志符 Era strings. For example: "AD" and "BC" PATTERN_YEAR: 'y', //年 PATTERN_MONTH: 'M', //月份 PATTERN_DAY_OF_MONTH: 'd', //月份的天数 PATTERN_HOUR_O

  • Java异步编程工具Twitter Future详解

    目录 异步编程(Twitter Future) 为啥要异步 基本用法 1.封装计算逻辑,异步返回. 2.异步计算结果串联异步处理 3.并行多个异步任务,统一等待结果 4.异步错误处理 Twitter包装 pom依赖 1.封装计算逻辑,异步返回 2.异步计算结果串联异步处理 3.并行多个异步任务 4.错误处理 其他用法 其他工具 异步编程(Twitter Future) 为啥要异步 异步编程有点难以理解,这东西感觉不符合常理,因为我们思考都是按照串行的逻辑,事都是一件一件办.但在异步计算的情况下,

  • java开发中嵌套类的详解及实例

     java开发中嵌套类的详解 在java语言规范里面,嵌套类(Nested Classes)定义是: A nested class is any class whose declaration occurs within the body of another class or interface. A top level class is a class that is not a nested class. 说的简单一点,就是定义在类里面的类.一般把定义内部类的外围类成为包装类(enclos

  • Java日期操作方法工具类实例【包含日期比较大小,相加减,判断,验证,获取年份等】

    本文实例讲述了Java日期操作方法工具类.分享给大家供大家参考,具体如下: package com.gcloud.common; import org.apache.http.util.TextUtils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.regex.Patte

  • java线程池ThreadPoolExecutor类使用详解

    在<阿里巴巴java开发手册>中指出了线程资源必须通过线程池提供,不允许在应用中自行显示的创建线程,这样一方面是线程的创建更加规范,可以合理控制开辟线程的数量:另一方面线程的细节管理交给线程池处理,优化了资源的开销.而线程池不允许使用Executors去创建,而要通过ThreadPoolExecutor方式,这一方面是由于jdk中Executor框架虽然提供了如newFixedThreadPool().newSingleThreadExecutor().newCachedThreadPool(

  • java日期处理工具类

    本文针对日期处理进行学习使用,主要分为两部分,下面为大家具体介绍一下 第一部分:日期处理基础知识 Date 类 作用:最主要的作用就是获得当前时间 将日期转换为标准格式 Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str = sdf.format(date); System.out.println("2015-10-16 1

  • java Arrays工具类实例详解

    Arrays工具类属于java中常用的工具类 public static void sort(int[] a) public static void sort(int[] a,int fromIndex, int toIndex) public static void sort(long[] a) public static void sort(long[] a,int fromIndex, int toIndex) public static void sort(short[] a) publ

随机推荐