`
tang9140
  • 浏览: 33221 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

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日期处理工具类DateUtils详解

    主要为大家详细介绍了Java日期处理工具类DateUtils的相关代码,包含日期和时间常用操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    java时间操作工具类 DateUtils

    java中常用的时间操作;如日期,时间戳,日历,字符串相互转化;时间差等常用日期功能。

    Java 日期比较工具类

    Java 日期比较工具类 各种日期操作及计算( 获取两个日期 间隔天数 间隔月数 格式化日期 )

    Java日期工具类DateUtils实例详解

    主要为大家详细介绍了Java日期工具类DateUtils实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    DateUtils.java 日期处理相关工具类

    * 文件名:DateUtils.java 日期处理相关工具类 * 版本信息:V1.0 * 日期:2013-03-11 * Copyright BDVCD Corporation 2013 * 版权所有 http://www.bdvcd.com */ public class DateUtils { /**定义常量**/ ...

    DateUtils Java时间工具类

    非常好用的Date工具类 1、计算两个日期之间相差的天数 2、判断日期是否为周六日 3、获取当前周开始日期 4、获取当前周结束日期 5、判断年份是否是闰年 6、根据年份和月份计算天数 7、判断日期为该年的第几周 等等

    java日期时间工具类超级全

    java日期时间工具类超级全。其中包含:获得当前日期 yyyy-MM-dd HH:mm:ss;获取系统当前时间戳;获取当前日期 yy-MM-dd;得到两个时间差 格式yyyy-MM-dd HH:mm:ss;转化long值的日期为yyyy-MM-dd HH:mm:ss.SSS格式的...

    java日期工具类

    这是自己整理的,结合公司日常开发,用到的一些常用的日期工具类。

    java时间处理工具类--DateUtils

    * @(#)DateUtil.java * * * @author kidd * @version 1.00 2007/8/8 */ import java.util.*; import java.text.*; import java.sql.Timestamp; public class DateUtils { /** * 时间范围:年 */ ...

    自己封装的DateUtils工具类

    自己封装的DateUtils工具类,内含多种日期格式转换方法,自己在项目开发过程中使用,方便日期转换

    DateUtils.java——日期处理

    属于时间转换工具类文件,其中包含格式转换,时区转换,时间计算等。

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

    主要介绍了Java 中DateUtils日期工具类的实例详解的相关资料,有时候开发java项目使用日期类型,这里介绍下日期工具类,需要的朋友可以参考下

    javaScript日期工具类DateUtils详解

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

    31个java工具类大汇总

    汇总了Java学习开发过程中常用的一些工具类,如字符串处理的StringUtils,日期处理的DateUtils,生成32位不重复的随机数、文件操作,xml操作,文件上传下载,收发邮件等。

    FormatDateUtil.java

    类别:日期工具类, java, DateUtils, FormatDateUtil 传入参数:Date对象和样式,生成不同样式的日期字符串 用途:java开发工具类

    JAVA工具类

    DateUtils - 日期工具类 HtmlFilterUtils - 过滤html标签 JsoupUtils - 基于jsoup过滤html标签 MoneyUtils - 获取大写金额 NumberUtils - 数字工具类 PinyinUtils - 汉字转拼音 media MediaUtils - 基于ffmpeg,...

    DateUtils.java

    各种对日期操作的API,开发时如果需要对日期处理,使用此工具类可以大大提供开发效率,欢迎大家下载使用。

    commonutils:Java实用程序

    ConvertDateUtils转换日期工具类 IdcardUtils身份证识别工具类 MoneyUtils钱处理工具类 日期时间转换 DateFormatUtils日期时间转换工具类 DateUtils日期转换工具类 TimestampUtils初步转换工具类 crypto加密解密 ...

Global site tag (gtag.js) - Google Analytics