iLeichun

当前位置: 首页 > Java

Java时间处理

分类:Java   来源:网络   时间:2010-10-28 23:34:31

Java时间处理API很丰富,但有时候调用起来不方便。本文代码封装了一些常用的时间处理,方便开发时调用。

  1. package cn.org.jshuwei.j2ee.util;
     

  2.  
  3. import java.sql.Date;
     
  4. import java.text.DateFormat;
     
  5. import java.text.ParseException;
     
  6. import java.text.SimpleDateFormat;
     
  7. import java.util.Calendar;
     

  8.  
  9. /**
     
  10. *
     
  11. * 日期操作的工具类
     
  12. *
     
  13. * @author huwei(jshuwei.org.cn)
     
  14. * @since 1.0
     
  15. *
     
  16. */
     
  17. public class DateUtil {
     

  18.  
  19.         private static String defDtPtn = "yyyy-MM-dd HH:mm:ss";// 缺省日期格式
     

  20.  
  21.         /**
     
  22.          * 计算给定时间至今的天数
     
  23.          *
     
  24.          * @since 1.1
     
  25.          * @param date
     
  26.          *            给定的时间
     
  27.          * @return 给定时间至今的天数
     
  28.          */
     
  29.         public static long date2day(String date) {
     
  30.                 long day = 0;
     
  31.                 DateFormat df = DateFormat.getDateInstance();
     
  32.                 try {
     
  33.                         long old = df.parse(date).getTime();
     
  34.                         long now = new java.util.Date().getTime();
     
  35.                         long secs = now - old;
     
  36.                         day = secs / (1000 * 24 * 60 * 60);
     
  37.                 } catch (ParseException e) {
     
  38.                         e.printStackTrace();
     
  39.                 }
     
  40.                 return day;
     
  41.         }
     

  42.  
  43.         /**
     
  44.          * 格式化给定时间后day天的时间
     
  45.          *
     
  46.          * @since 1.0
     
  47.          * @param date
     
  48.          *            需要格式化的时间
     
  49.          * @param day
     
  50.          *            增加的天数
     
  51.          * @return 给定时间的后day天的格式化字符串(如:2008-11-22)
     
  52.          */
     
  53.         public static String formatDate(java.util.Date date, Integer day) {
     
  54.                 String str = "";
     
  55.                 str = new Date(date.getTime() + day * 24 * 60 * 60).toString();
     
  56.                 return str;
     
  57.         }
     

  58.  
  59.         /**
     
  60.          * 格式化给定时间
     
  61.          *
     
  62.          * @param date
     
  63.          *            需要格式化的时间
     
  64.          * @return 给定时间的格式化字符串(如:2008-11-22)
     
  65.          */
     
  66.         public static String formatDate(java.util.Date date) {
     
  67.                 return new Date(date.getTime()).toString();
     
  68.         }
     

  69.  
  70.         /**
     
  71.          * 得到当前年
     
  72.          *
     
  73.          * @since 1.0
     
  74.          * @return 返回当前年(YYYY)
     
  75.          */
     
  76.         public static int getYear() {
     
  77.                 return Calendar.getInstance().get(Calendar.YEAR);
     
  78.         }
     

  79.  
  80.         /**
     
  81.          * 得到当前月
     
  82.          *
     
  83.          * @since 1.0
     
  84.          * @return 返回当前月(1~12)
     
  85.          */
     
  86.         public static int getMonth() {
     
  87.                 return Calendar.getInstance().get(Calendar.MONTH) + 1;
     
  88.         }
     

  89.  
  90.         /**
     
  91.          * 得到当前日
     
  92.          *
     
  93.          * @since 1.0
     
  94.          * @return 返回当前日(1~31)
     
  95.          */
     
  96.         public static int getDay() {
     
  97.                 return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
     
  98.         }
     

  99.  
  100.         /**
     
  101.          * 得到当前年
     
  102.          *
     
  103.          * @since 1.0
     
  104.          * @return 返回当前年(YY)
     
  105.          */
     
  106.         public static String getYear2() {
     
  107.                 int year = getYear();
     
  108.                 return StringUtil.Integer2String(year, "1986").substring(2, 4);
     
  109.         }
     

  110.  
  111.         /**
     
  112.          * 得到当前月
     
  113.          *
     
  114.          * @since 1.0
     
  115.          * @return 返回当前月(01~12)
     
  116.          */
     
  117.         public static String getMonth2() {
     
  118.                 int month = getMonth();
     
  119.                 if (month < 10) {
     
  120.                         return "0" + StringUtil.Integer2String(month, "00");
     
  121.                 }
     
  122.                 return StringUtil.Integer2String(month, "00");
     
  123.         }
     

  124.  
  125.         /**
     
  126.          * 得到当前日
     
  127.          *
     
  128.          * @since 1.0
     
  129.          * @return 返回当前日(01~31)
     
  130.          */
     
  131.         public static String getDay2() {
     
  132.                 int day = getDay();
     
  133.                 if (day < 10) {
     
  134.                         return "0" + StringUtil.Integer2String(day, "00");
     
  135.                 }
     
  136.                 return StringUtil.Integer2String(day, "00");
     
  137.         }
     

  138.  
  139.         /**
     
  140.          * 得到指定格式的当前时间
     
  141.          *
     
  142.          * @param format
     
  143.          *            格式化形式(年用YY/YYYY表示;月用M/MM表示;日用D/DD表示,之间任意任序组合),<br />
     
  144.          *            如"YYYY-MM-DD"返回形如:1986-12-17<br />
     
  145.          *            如"YY-MM"返回形如:86-12<br />
     
  146.          *            如"YY年MM月"返回形如:86年12月……
     
  147.          * @since 1.0
     
  148.          * @return 返回指定格式的当前时间
     
  149.          *
     
  150.          */
     
  151.         public static String getDate(String format) {
     
  152.                 format = format.replace("YYYY", getYear() + "");
     
  153.                 format = format.replace("YY", getYear2());
     
  154.                 format = format.replace("MM", getMonth2());
     
  155.                 format = format.replace("M", getMonth() + "");
     
  156.                 format = format.replace("DD", getDay2());
     
  157.                 format = format.replace("D", getDay() + "");
     
  158.                 return format;
     
  159.         }
     

  160.  
  161.         /**
     
  162.          * 将字符串按指定格式解析成日期对象
     
  163.          *
     
  164.          * @since 1.1
     
  165.          * @param dateStr
     
  166.          *            需要进行转换的日期字符串
     
  167.          * @param pattern
     
  168.          *            日期字符串的格式
     
  169.          * @return "yyyy-MM-dd HH:mm:ss"形式的日期对象
     
  170.          */
     
  171.         public static java.util.Date parseDate(String dateStr, String pattern) {
     
  172.                 SimpleDateFormat DATEFORMAT = new SimpleDateFormat(defDtPtn);
     
  173.                 DATEFORMAT.applyPattern(pattern);
     
  174.                 java.util.Date ret = null;
     
  175.                 try {
     
  176.                         ret = DATEFORMAT.parse(dateStr);
     
  177.                 } catch (Exception e) {
     
  178.                         e.printStackTrace();
     
  179.                 }
     
  180.                 DATEFORMAT.applyPattern(defDtPtn);
     
  181.                 return ret;
     
  182.         }
     

  183.  
  184.         /**
     
  185.          * 计算详细年龄
     
  186.          *
     
  187.          * @since 1.1
     
  188.          * @param birthdayStr
     
  189.          *            出生日期 格式"YYYY-MM-DD"
     
  190.          * @return 指定日期至今天的年龄
     
  191.          */
     
  192.         public static String countAge(String birthdayStr) {
     
  193.                 String age = "";
     
  194.                 if (birthdayStr != null && birthdayStr.length() == 8) {
     
  195.                         java.util.Date birthday = parseDate(birthdayStr, "YYYY-MM-DD");
     

  196.  
  197.                         if (birthday != null) {
     
  198.                                 Calendar calendar = Calendar.getInstance();
     
  199.                                 int year1 = getYear();
     
  200.                                 int month1 = StringUtil.String2Integer(getMonth2(), 0);
     
  201.                                 int day1 = StringUtil.String2Integer(getDay2(), 00);
     

  202.  
  203.                                 calendar.setTime(birthday);
     
  204.                                 int year2 = calendar.get(Calendar.YEAR);
     
  205.                                 int month2 = calendar.get(Calendar.MONTH) + 1;
     
  206.                                 int day2 = calendar.get(Calendar.DATE);
     

  207.  
  208.                                 int year = year1 - year2;
     
  209.                                 int month = month1 - month2;
     
  210.                                 int day = day1 - day2;
     

  211.  
  212.                                 if (month < 0) {
     
  213.                                         year = year - 1;
     
  214.                                         month = 12 + month1 - month2;
     
  215.                                 }
     

  216.  
  217.                                 if (day < 0) {
     
  218.                                         month = month - 1;
     
  219.                                         if (month < 0) {
     
  220.                                                 year = year - 1;
     
  221.                                                 month = 11;
     
  222.                                         }
     
  223.                                         int lastMonthDay = 0;
     
  224.                                         if (month1 == 0) {
     
  225.                                                 lastMonthDay = getDayOfMonth(12, year1 - 1);
     
  226.                                         } else {
     
  227.                                                 lastMonthDay = getDayOfMonth(month1, year1);
     
  228.                                         }
     
  229.                                         day = lastMonthDay + day1 - day2;
     
  230.                                 }
     

  231.  
  232.                                 if (year > 5) {
     
  233.                                         age = year + "岁";
     
  234.                                 } else if (year > 0) {
     
  235.                                         if (month == 0) {
     
  236.                                                 age = year + "岁";
     
  237.                                         } else {
     
  238.                                                 age = year + "岁" + month + "月";
     
  239.                                         }
     
  240.                                 } else {
     
  241.                                         if (month > 5) {
     
  242.                                                 age = month + "月";
     
  243.                                         } else if (month > 0) {
     
  244.                                                 age = month + "月" + day + "天";
     
  245.                                         } else {
     
  246.                                                 age = day + "天";
     
  247.                                         }
     
  248.                                 }
     
  249.                         }
     
  250.                 }
     

  251.  
  252.                 return age;
     
  253.         }
     

  254.  
  255.         /**
     
  256.          * 得到指定年月的天数
     
  257.          *
     
  258.          * @since 1.1
     
  259.          * @param month
     
  260.          *            指定月份
     
  261.          * @param year
     
  262.          *            指定的年份
     
  263.          * @return 天数
     
  264.          */
     
  265.         public static int getDayOfMonth(int month, int year) {
     
  266.                 int ret = 0;
     
  267.                 boolean flag = false;
     

  268.  
  269.                 if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
     
  270.                         flag = true;
     
  271.                 }
     

  272.  
  273.                 switch (month) {
     
  274.                 case 1:
     
  275.                         ret = 31;
     
  276.                         break;
     
  277.                 case 2:
     
  278.                         if (flag) {
     
  279.                                 ret = 29;
     
  280.                         } else {
     
  281.                                 ret = 28;
     
  282.                         }
     
  283.                         break;
     
  284.                 case 3:
     
  285.                         ret = 31;
     
  286.                         break;
     
  287.                 case 4:
     
  288.                         ret = 30;
     
  289.                         break;
     
  290.                 case 5:
     
  291.                         ret = 31;
     
  292.                         break;
     
  293.                 case 6:
     
  294.                         ret = 30;
     
  295.                         break;
     
  296.                 case 7:
     
  297.                         ret = 31;
     
  298.                         break;
     
  299.                 case 8:
     
  300.                         ret = 31;
     
  301.                         break;
     
  302.                 case 9:
     
  303.                         ret = 30;
     
  304.                         break;
     
  305.                 case 10:
     
  306.                         ret = 31;
     
  307.                         break;
     
  308.                 case 11:
     
  309.                         ret = 30;
     
  310.                         break;
     
  311.                 case 12:
     
  312.                         ret = 31;
     
  313.                         break;
     
  314.                 default:
     
  315.                         break;
     
  316.                 }
     
  317.                 return ret;
     
  318.         }
     

  319.  
  320.         /**
     
  321.          * 计算某天是星期几
     
  322.          *
     
  323.          * @since 1.1
     
  324.          * @param p_date
     
  325.          *            日期字符串
     
  326.          * @return 星期
     
  327.          */
     
  328.         public static int whatDayIsSpecifyDate(String p_date) {
     
  329.                 // 2002-2-22 is friday5
     
  330.                 long differenceDays = 0L;
     
  331.                 long m = 0L;
     
  332.                 differenceDays = signDaysBetweenTowDate(p_date, "2002-2-22");
     

  333.  
  334.                 m = (differenceDays % 7);
     
  335.                 m = m + 5;
     
  336.                 m = m > 7 ? m - 7 : m;
     
  337.                 return Integer.parseInt(m + "");
     
  338.         }
     

  339.  
  340.         /**
     
  341.          * 计算两日期间相差天数.
     
  342.          *
     
  343.          * @since 1.1
     
  344.          * @param d1
     
  345.          *            日期字符串
     
  346.          * @param d2
     
  347.          *            日期字符串
     
  348.          * @return long 天数
     
  349.          */
     
  350.         public static long signDaysBetweenTowDate(String d1, String d2) {
     
  351.                 java.sql.Date dd1 = null;
     
  352.                 java.sql.Date dd2 = null;
     
  353.                 long result = -1l;
     
  354.                 try {
     
  355.                         dd1 = java.sql.Date.valueOf(d1);
     
  356.                         dd2 = java.sql.Date.valueOf(d2);
     
  357.                         result = signDaysBetweenTowDate(dd1, dd2);
     
  358.                 } catch (Exception ex) {
     
  359.                         result = -1;
     
  360.                 }
     
  361.                 return result;
     
  362.         }
     

  363.  
  364.         /**
     
  365.          * 计算两日期间相差天数.
     
  366.          *
     
  367.          * @since 1.1
     
  368.          * @param d1
     
  369.          *            开始日期 日期型
     
  370.          * @param d2
     
  371.          *            结束日期 日期型
     
  372.          * @return long 天数
     
  373.          */
     
  374.         public static long signDaysBetweenTowDate(java.sql.Date d1, java.sql.Date d2) {
     
  375.                 return (d1.getTime() - d2.getTime()) / (3600 * 24 * 1000);
     
  376.         }
     
  377. }
更多