package com..util;



import java.sql.Timestamp;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;



public class DataUtil {



    /**

     * 功能: 将日期对象按照某种格式进行转换,返回转换后的字符串

     * 

     * @param date 日期对象

     * @param pattern 转换格式 例:yyyy-MM-dd

     */

    public static String DateToString(Date date, String pattern) {

        String strDateTime = null;

        SimpleDateFormat formater = new SimpleDateFormat(pattern);

        strDateTime = date == null ? null : formater.format(date);

        return strDateTime;

    }



    /**

     * 功能: 将传入的日期对象按照yyyy-MM-dd格式转换成字符串返回

     * 

     * @param date 日期对象

     * @return String

     */

    public static String DateToString(Date date) {

        String _pattern = "yyyy-MM-dd";

        return date == null ? null : DateToString(date, _pattern);

    }



    /**

     * 功能: 将传入的日期对象按照yyyy-MM-dd HH:mm:ss格式转换成字符串返回

     * 

     * @param date 日期对象

     * @return String

     */

    public static String DateTimeToString(Date date) {

        String _pattern = "yyyy-MM-dd HH:mm:ss";

        return date == null ? null : DateToString(date, _pattern);

    }



    /**

     * 功能: 将插入的字符串按格式转换成对应的日期对象

     * 

     * @param str 字符串

     * @param pattern 格式

     * @return Date

     */

    public static Date StringToDate(String str, String pattern) {

        Date dateTime = null;

        try {

            if (str != null && !str.equals("")) {

                SimpleDateFormat formater = new SimpleDateFormat(pattern);

                dateTime = formater.parse(str);

            }

        } catch (Exception ex) {

        }

        return dateTime;

    }



    /**

     * 功能: 将传入的字符串按yyyy-MM-dd格式转换成对应的日期对象

     * 

     * @param str 需要转换的字符串

     * @return Date 返回值

     */

    public static Date StringToDate(String str) {

        String _pattern = "yyyy-MM-dd";

        return StringToDate(str, _pattern);

    }



    /**

     * 功能: 将传入的字符串按yyyy-MM-dd HH:mm:ss格式转换成对应的日期对象

     * 

     * @param str 需要转换的字符串

     * @return Date

     */

    public static Date StringToDateTime(String str) {

        String _pattern = "yyyy-MM-dd HH:mm:ss";

        return StringToDate(str, _pattern);

    }



    /**

     * 功能: 将传入的字符串转换成对应的Timestamp对象

     * 

     * @param str 待转换的字符串

     * @return Timestamp 转换之后的对象

     * @throws Exception

     *             Timestamp

     */

    public static Timestamp StringToDateHMS(String str) throws Exception {

        Timestamp time = null;

        time = Timestamp.valueOf(str);

        return time;

    }



    /**

     * 功能: 根据传入的年月日返回相应的日期对象

     * 

     * @param year 年份

     * @param month 月份

     * @param day 天

     * @return Date 日期对象

     */

    public static Date YmdToDate(int year, int month, int day) {

        Calendar calendar = Calendar.getInstance();

        calendar.set(year, month, day);

        return calendar.getTime();

    }



    /**

     * 功能: 将日期对象按照MM/dd HH:mm:ss的格式进行转换,返回转换后的字符串

     * 

     * @param date 日期对象

     * @return String 返回值

     */

    public static String communityDateToString(Date date) {

        SimpleDateFormat formater = new SimpleDateFormat("MM/dd HH:mm:ss");

        String strDateTime = date == null ? null : formater.format(date);

        return strDateTime;

    }



    public static Date getMaxDateOfDay(Date date) {

        if (date == null) {

            return null;

        } else {

            Calendar calendar = Calendar.getInstance();

            calendar.setTime(date);

            calendar.set(11, calendar.getActualMaximum(11));

            calendar.set(12, calendar.getActualMaximum(12));

            calendar.set(13, calendar.getActualMaximum(13));

            calendar.set(14, calendar.getActualMaximum(14));

            return calendar.getTime();

        }

    }



    public static Date getMinDateOfDay(Date date) {

        if (date == null) {

            return null;

        } else {

            Calendar calendar = Calendar.getInstance();

            calendar.setTime(date);

            calendar.set(11, calendar.getActualMinimum(11));

            calendar.set(12, calendar.getActualMinimum(12));

            calendar.set(13, calendar.getActualMinimum(13));

            calendar.set(14, calendar.getActualMinimum(14));

            return calendar.getTime();

        }

    }



    /**

     * 功能:返回传入日期对象(date)之后afterDays天数的日期对象

     * 

     * @param date 日期对象

     * @param afterDays 往后天数

     * @return java.util.Date 返回值

     */

    public static Date getAfterDay(Date date, int afterDays) {

        Calendar cal = Calendar.getInstance();

        cal.setTime(date);

        cal.add(Calendar.DATE, 1);

        return cal.getTime();

    }



    // day

    /**

     * 功能: 返回date1与date2相差的天数

     * 

     * @param date1

     * @param date2

     * @return int

     */

    public static int DateDiff(Date date1, Date date2) {

        int i = (int) ((date1.getTime() - date2.getTime()) / 3600 / 24 / 1000);

        return i;

    }



    // min

    /**

     * 功能: 返回date1与date2相差的分钟数

     * 

     * @param date1

     * @param date2

     * @return int

     */

    public static int MinDiff(Date date1, Date date2) {

        int i = (int) ((date1.getTime() - date2.getTime()) / 1000 / 60);

        return i;

    }



    // second

    /**

     * 功能: 返回date1与date2相差的秒数

     * 

     * @param date1

     * @param date2

     * @return int

     */

    public static int TimeDiff(Date date1, Date date2) {

        int i = (int) ((date1.getTime() - date2.getTime()));

        return i;

    }



}

java日期操作常用工具的更多相关文章

  1. java日期操作的工具类时间格式的转换

    package cn.itcast.oa.util; import java.text.ParseException; import java.text.SimpleDateFormat;import ...

  2. Java程序员常用工具类库

    有人说当你开始学习Java的时候,你就走上了一条不归路,在Java世界里,包罗万象,从J2SE,J2ME,J2EE三大平台,到J2EE中的13中核心技术,再到Java世界中万紫千红的Framework ...

  3. java日期操作大全

    摘自(http://www.blogjava.net/i369/articles/83483.html) java日期操作 大全 先来一个:  取得指定月份的第一天与取得指定月份的最后一天  http ...

  4. java日期操作 大全

    先来一个:  取得指定月份的第一天与取得指定月份的最后一天  http://iamin.blogdriver.com/iamin/847990.html ));             }       ...

  5. Java日期时间实用工具类

    Java日期时间实用工具类 1.Date (java.util.Date)    Date();        以当前时间构造一个Date对象    Date(long);        构造函数   ...

  6. Java后端开发常用工具

    Java后端开发常用工具推荐: 俗话说,工欲善其事,必先利其器.不过初学时候不大建议过度依赖IDE等过多工具,这会让自己的编程基础功变得很差,比如各种语法的不熟悉,各种关键字比如synchronize ...

  7. Java程序员常用工具类库 - 目录

    有人说当你开始学习Java的时候,你就走上了一条不归路,在Java世界里,包罗万象,从J2SE,J2ME,J2EE三大平台,到J2EE中的13中核心技术,再到Java世界中万紫千红的Framework ...

  8. java 微信开发 常用工具类(xml传输和解析 json转换对象)

    与微信通信常用工具(xml传输和解析) package com.lownsun.wechatOauth.utl; import java.io.IOException; import java.io. ...

  9. java性能优化常用工具jmap、jstack

    jmap:java内存映像工具 jmap用于生成堆转储快照,比较常用的option包括-heap,-histo,-dump [root@localhost script]# jmap -h Usage ...

随机推荐

  1. Java之恋

    初次见面那是一个河北的夏天风随沙散落天涯蝴蝶依旧恋着花回首走过的日子手指和键盘之间的梦想之光已恍如昨日 那年我还是一个刚踏进这个曾经只在地理课本上狂念南稻北麦,南油北花的土地那年你只是我必须要学的编程 ...

  2. View绘制流程

    1. View 树的绘图流程 当 Activity 接收到焦点的时候,它会被请求绘制布局,该请求由 Android framework 处理.绘制是从根节点开始,对布局树进行 measure 和 dr ...

  3. install_driver(Oracle) failed: Can't load `.../DBD/Oracle/Oracle.so' for module DBD::Oracle

    Description This section is from the "Practical mod_perl " book, by Stas Bekman and Eric C ...

  4. 精通CSS+DIV网页样式与布局--页面和浏览器元素

    在页面和浏览器中,除了文字.图片.表格.表单等,还有很多各种各样的元素,在上篇博文中,小编主要简单的介绍了一下在CSS中如何设置表格和表单,今天小编主要简单介绍一下丰富的超链接特效.鼠标特效.页面滚动 ...

  5. python的str()和repr()的区别

    str()一般是将数值转成字符串. repr()是将一个对象转成字符串显示,注意只是显示用,有些对象转成字符串没有直接的意思.如list,dict使用str()是无效的,但使用repr可以,这是为了看 ...

  6. spring2.5与hibernate3升级后的bug

    手头有一个项目,使用的是struts2 hibernate3 spring2.5 是之前的老项目了,spring与hibernate的版本都比较低 自己看了最新的spring4与hibernate4, ...

  7. ant+eclipse知识点详解及使用案例

    ant的优点和地位就不再阐述,下面直接上知识点: 在java中使用xml文件开发,有以下基本语法 (1)project:每个ant程序有且只有一个此标签,而且是类似于html的总标签,有name,de ...

  8. ISLR系列:(4.3)模型选择 PCR & PLS

    Linear Model Selection and Regularization 此博文是 An Introduction to Statistical Learning with Applicat ...

  9. JavaScript进阶(七)JS截取字符串substr 和 substring方法的区别

    JS截取字符串substr 和 substring方法的区别 substr方法 返回一个从指定位置开始的指定长度的子字符串. stringvar.substr(start [, length ]) 参 ...

  10. Emotiv脑电设备与RDS机器人仿真初步测试

    Emotiv脑电设备与RDS机器人仿真初步测试 在脑电设备相关算法进行真实机器人测试前,有必要进行大量仿真验证算法,节约开发时间. 这里给我启发的Emotiv使用所参考的一些网址. 官网:https: ...