Java时间工具类

来源:互联网 发布:linux下测试网速 编辑:程序博客网 时间:2024/05/16 16:16
  1. import java.sql.Timestamp;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.Calendar;  
  4. import java.util.Date;  
  5.   
  6. public class DataUtil {  
  7.   
  8.     /** 
  9.      * 功能: 将日期对象按照某种格式进行转换,返回转换后的字符串 
  10.      *  
  11.      * @param date 日期对象 
  12.      * @param pattern 转换格式 例:yyyy-MM-dd 
  13.      */  
  14.     public static String DateToString(Date date, String pattern) {  
  15.         String strDateTime = null;  
  16.         SimpleDateFormat formater = new SimpleDateFormat(pattern);  
  17.         strDateTime = date == null ? null : formater.format(date);  
  18.         return strDateTime;  
  19.     }  
  20.   
  21.     /** 
  22.      * 功能: 将传入的日期对象按照yyyy-MM-dd格式转换成字符串返回 
  23.      *  
  24.      * @param date 日期对象 
  25.      * @return String 
  26.      */  
  27.     public static String DateToString(Date date) {  
  28.         String _pattern = "yyyy-MM-dd";  
  29.         return date == null ? null : DateToString(date, _pattern);  
  30.     }  
  31.   
  32.     /** 
  33.      * 功能: 将传入的日期对象按照yyyy-MM-dd HH:mm:ss格式转换成字符串返回 
  34.      *  
  35.      * @param date 日期对象 
  36.      * @return String 
  37.      */  
  38.     public static String DateTimeToString(Date date) {  
  39.         String _pattern = "yyyy-MM-dd HH:mm:ss";  
  40.         return date == null ? null : DateToString(date, _pattern);  
  41.     }  
  42.   
  43.     /** 
  44.      * 功能: 将插入的字符串按格式转换成对应的日期对象 
  45.      *  
  46.      * @param str 字符串 
  47.      * @param pattern 格式 
  48.      * @return Date 
  49.      */  
  50.     public static Date StringToDate(String str, String pattern) {  
  51.         Date dateTime = null;  
  52.         try {  
  53.             if (str != null && !str.equals("")) {  
  54.                 SimpleDateFormat formater = new SimpleDateFormat(pattern);  
  55.                 dateTime = formater.parse(str);  
  56.             }  
  57.         } catch (Exception ex) {  
  58.         }  
  59.         return dateTime;  
  60.     }  
  61.   
  62.     /** 
  63.      * 功能: 将传入的字符串按yyyy-MM-dd格式转换成对应的日期对象 
  64.      *  
  65.      * @param str 需要转换的字符串 
  66.      * @return Date 返回值 
  67.      */  
  68.     public static Date StringToDate(String str) {  
  69.         String _pattern = "yyyy-MM-dd";  
  70.         return StringToDate(str, _pattern);  
  71.     }  
  72.   
  73.     /** 
  74.      * 功能: 将传入的字符串按yyyy-MM-dd HH:mm:ss格式转换成对应的日期对象 
  75.      *  
  76.      * @param str 需要转换的字符串 
  77.      * @return Date 
  78.      */  
  79.     public static Date StringToDateTime(String str) {  
  80.         String _pattern = "yyyy-MM-dd HH:mm:ss";  
  81.         return StringToDate(str, _pattern);  
  82.     }  
  83.   
  84.     /** 
  85.      * 功能: 将传入的字符串转换成对应的Timestamp对象 
  86.      *  
  87.      * @param str 待转换的字符串 
  88.      * @return Timestamp 转换之后的对象 
  89.      * @throws Exception 
  90.      *             Timestamp 
  91.      */  
  92.     public static Timestamp StringToDateHMS(String str) throws Exception {  
  93.         Timestamp time = null;  
  94.         time = Timestamp.valueOf(str);  
  95.         return time;  
  96.     }  
  97.   
  98.     /** 
  99.      * 功能: 根据传入的年月日返回相应的日期对象 
  100.      *  
  101.      * @param year 年份 
  102.      * @param month 月份 
  103.      * @param day 天 
  104.      * @return Date 日期对象 
  105.      */  
  106.     public static Date YmdToDate(int year, int month, int day) {  
  107.         Calendar calendar = Calendar.getInstance();  
  108.         calendar.set(year, month, day);  
  109.         return calendar.getTime();  
  110.     }  
  111.   
  112.     /** 
  113.      * 功能: 将日期对象按照MM/dd HH:mm:ss的格式进行转换,返回转换后的字符串 
  114.      *  
  115.      * @param date 日期对象 
  116.      * @return String 返回值 
  117.      */  
  118.     public static String communityDateToString(Date date) {  
  119.         SimpleDateFormat formater = new SimpleDateFormat("MM/dd HH:mm:ss");  
  120.         String strDateTime = date == null ? null : formater.format(date);  
  121.         return strDateTime;  
  122.     }  
  123.   
  124.     public static Date getMaxDateOfDay(Date date) {  
  125.         if (date == null) {  
  126.             return null;  
  127.         } else {  
  128.             Calendar calendar = Calendar.getInstance();  
  129.             calendar.setTime(date);  
  130.             calendar.set(11, calendar.getActualMaximum(11));  
  131.             calendar.set(12, calendar.getActualMaximum(12));  
  132.             calendar.set(13, calendar.getActualMaximum(13));  
  133.             calendar.set(14, calendar.getActualMaximum(14));  
  134.             return calendar.getTime();  
  135.         }  
  136.     }  
  137.   
  138.     public static Date getMinDateOfDay(Date date) {  
  139.         if (date == null) {  
  140.             return null;  
  141.         } else {  
  142.             Calendar calendar = Calendar.getInstance();  
  143.             calendar.setTime(date);  
  144.             calendar.set(11, calendar.getActualMinimum(11));  
  145.             calendar.set(12, calendar.getActualMinimum(12));  
  146.             calendar.set(13, calendar.getActualMinimum(13));  
  147.             calendar.set(14, calendar.getActualMinimum(14));  
  148.             return calendar.getTime();  
  149.         }  
  150.     }  
  151.   
  152.     /** 
  153.      * 功能:返回传入日期对象(date)之后afterDays天数的日期对象 
  154.      *  
  155.      * @param date 日期对象 
  156.      * @param afterDays 往后天数 
  157.      * @return java.util.Date 返回值 
  158.      */  
  159.     public static Date getAfterDay(Date date, int afterDays) {  
  160.         Calendar cal = Calendar.getInstance();  
  161.         cal.setTime(date);  
  162.         cal.add(Calendar.DATE, 1);  
  163.         return cal.getTime();  
  164.     }  
  165.   
  166.     // day  
  167.     /** 
  168.      * 功能: 返回date1与date2相差的天数 
  169.      *  
  170.      * @param date1 
  171.      * @param date2 
  172.      * @return int 
  173.      */  
  174.     public static int DateDiff(Date date1, Date date2) {  
  175.         int i = (int) ((date1.getTime() - date2.getTime()) / 3600 / 24 / 1000);  
  176.         return i;  
  177.     }  
  178.   
  179.     // min  
  180.     /** 
  181.      * 功能: 返回date1与date2相差的分钟数 
  182.      *  
  183.      * @param date1 
  184.      * @param date2 
  185.      * @return int 
  186.      */  
  187.     public static int MinDiff(Date date1, Date date2) {  
  188.         int i = (int) ((date1.getTime() - date2.getTime()) / 1000 / 60);  
  189.         return i;  
  190.     }  
  191.   
  192.     // second  
  193.     /** 
  194.      * 功能: 返回date1与date2相差的秒数 
  195.      *  
  196.      * @param date1 
  197.      * @param date2 
  198.      * @return int 
  199.      */  
  200.     public static int TimeDiff(Date date1, Date date2) {  
  201.         int i = (int) ((date1.getTime() - date2.getTime()));  
  202.         return i;  
  203.     }  
  204.   
  205. }