`
tang9140
  • 浏览: 34569 次
  • 性别: 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时间操作工具类 DateUtils

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

    Java日期工具类DateUtils实例详解

    Java 日期工具类 DateUtils 实例详解 本文主要介绍了 Java 日期工具类 DateUtils 实例的实现和使用,涵盖了日期工具类的常用方法和变量,包括日期格式化、字符串转换、日期比较等。 日期工具类 DateUtils ...

    时间工具类 DateUtils

    在Java编程语言中,时间工具类是用于处理日期和时间操作的重要工具,它们极大地简化了开发者对日期和时间的操作。本篇文章将详细讲解基于提供的"时间工具类 DateUtils"的知识点,包括DateUtils的主要功能、如何使用...

    Java 日期比较工具类

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

    flex 学习项目中总结的时间处理工具类DateUtils

    在这个名为"flex 学习项目中总结的时间处理工具类DateUtils"的项目中,我们可能找到了一个针对Flex开发环境优化的日期处理工具。Flex是一种基于Adobe ActionScript的开源框架,主要用于构建富互联网应用(RIA)。 `...

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

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

    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工具类

    在Java编程中,DateUtils工具类是一个非常实用的辅助类,它封装了各种日期和时间处理的方法,大大简化了开发者在处理日期时的工作。这里我们深入探讨一下自定义的DateUtils工具类及其重要功能。 首先,`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工具类DateUtils实例详解

    Java工具类DateUtils实例是Java编程语言中一个非常实用的工具类,主要用于处理日期和时间相关的操作。本文将对Java工具类DateUtils实例进行详细的解释,包括其主要方法和使用场景。 DateUtils类是Java中的一个工具...

    java日期工具类

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

    日期处理工具类

    "日期处理工具类"通常是指自定义的类或使用Java内置的日期时间API来执行与日期相关的操作,如日期加减、日期格式化以及获取特定周或日等功能。在本案例中,我们有一个名为`DateUtils`的类,它可能包含了这些实用方法...

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

    Java 中的 DateUtils 工具类是 Java 语言中的一种常用工具类,用于处理日期和时间的转换。该工具类提供了多种日期和时间的转换方法,包括 String 转 Timestamp、String 转 Date、Date 转 String、Date 转 Timestamp ...

    50个左右的JAVA工具类,相对比较全

    2. **日期时间工具类**:如Java 8中的java.time包,或者Apache Commons Lang的DateUtils,提供日期和时间的转换、比较、格式化等操作。 3. **集合工具类**:Java的Collections类提供了集合的一些基本操作,而Guava...

    DateUtils.java——日期处理

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

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

    Java 中DateUtils日期工具类是 Java 语言中对日期类型的操作的重要组件,主要用于处理日期类型和字符串类型之间的转换。在 Java 项目中,日期类型的处理往往非常不方便,特别是在 JDK 1.8 之前的版本中,需要借助 ...

    JAVA 工具类 项目

    在你提到的"JAVA 工具类 项目"中,很可能包含了一系列常见的工具类,如FileUtils和DateUtils,这些都是Java开发中非常实用的部分。 1. **FileUtils**: 这个工具类主要处理与文件和目录相关的操作。它可能提供了如...

    Date日期操作工具类

    在Java编程中,Date类是处理日期和时间的基础类,但在实际开发中,由于Date类本身的API设计并不十分友好,通常我们会使用工具类来简化日期的处理工作。本主题聚焦于一个名为"Date日期操作工具类"的实用工具,它提供...

Global site tag (gtag.js) - Google Analytics