`
bjtdeyx
  • 浏览: 167236 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

java 日期处理工具类 DateUtil

    博客分类:
  • java
阅读更多
  1. package com.util;  
  2.   
  3. import java.text.ParseException;  
  4. import java.text.ParsePosition;  
  5. import java.text.SimpleDateFormat;  
  6. import java.util.Calendar;  
  7. import java.util.Date;  
  8. import java.util.GregorianCalendar;  
  9.   
  10. import org.apache.commons.lang.StringUtils;  
  11. import org.apache.log4j.Logger;  
  12.   
  13.   
  14. /** 
  15.  * 日期处理工具类 
  16.  * @author dylan_xu 
  17.  * @date Mar 11, 2012 
  18.  * @modified by 
  19.  * @modified date 
  20.  * @since JDK1.6 
  21.  * @see com.util.DateUtil 
  22.  */  
  23.   
  24. public class DateUtil {  
  25.     // ~ Static fields/initializers  
  26.     // =============================================  
  27.   
  28.     private static Logger logger = Logger.getLogger(DateUtil.class);  
  29.     private static String defaultDatePattern = null;  
  30.     private static String timePattern = "HH:mm";  
  31.     private static Calendar cale = Calendar.getInstance();  
  32.     public static final String TS_FORMAT = DateUtil.getDatePattern() + " HH:mm:ss.S";  
  33.     /** 日期格式yyyy-MM字符串常量 */  
  34.     private static final String MONTH_FORMAT = "yyyy-MM";  
  35.     /** 日期格式yyyy-MM-dd字符串常量 */  
  36.     private static final String DATE_FORMAT = "yyyy-MM-dd";  
  37.     /** 日期格式HH:mm:ss字符串常量 */  
  38.     private static final String HOUR_FORMAT = "HH:mm:ss";  
  39.     /** 日期格式yyyy-MM-dd HH:mm:ss字符串常量 */  
  40.     private static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";  
  41.     /** 某天开始时分秒字符串常量  00:00:00 */  
  42.     private static final String DAY_BEGIN_STRING_HHMMSS = " 00:00:00";  
  43.     /**  某天结束时分秒字符串常量  23:59:59  */  
  44.     public static final String DAY_END_STRING_HHMMSS = " 23:59:59";  
  45.     private static SimpleDateFormat sdf_date_format = new SimpleDateFormat(DATE_FORMAT);  
  46.     private static SimpleDateFormat sdf_hour_format = new SimpleDateFormat(HOUR_FORMAT);  
  47.     private static SimpleDateFormat sdf_datetime_format = new SimpleDateFormat(DATETIME_FORMAT);  
  48.       
  49.   
  50.     // ~ Methods  
  51.     // ================================================================  
  52.   
  53.     public DateUtil() {  
  54.     }  
  55.   
  56.     /** 
  57.      * 获得服务器当前日期及时间,以格式为:yyyy-MM-dd HH:mm:ss的日期字符串形式返回 
  58.      * @author dylan_xu 
  59.      * @date Mar 11, 2012 
  60.      * @return 
  61.      */  
  62.     public static String getDateTime() {  
  63.         try {  
  64.             return sdf_datetime_format.format(cale.getTime());  
  65.         } catch (Exception e) {  
  66.             logger.debug("DateUtil.getDateTime():" + e.getMessage());  
  67.             return "";  
  68.         }  
  69.     }  
  70.   
  71.     /** 
  72.      * 获得服务器当前日期,以格式为:yyyy-MM-dd的日期字符串形式返回 
  73.      * @author dylan_xu 
  74.      * @date Mar 11, 2012 
  75.      * @return 
  76.      */  
  77.     public static String getDate() {  
  78.         try {  
  79.             return sdf_date_format.format(cale.getTime());  
  80.         } catch (Exception e) {  
  81.             logger.debug("DateUtil.getDate():" + e.getMessage());  
  82.             return "";  
  83.         }  
  84.     }  
  85.   
  86.     /** 
  87.      * 获得服务器当前时间,以格式为:HH:mm:ss的日期字符串形式返回 
  88.      * @author dylan_xu 
  89.      * @date Mar 11, 2012 
  90.      * @return 
  91.      */  
  92.     public static String getTime() {  
  93.         String temp = " ";  
  94.         try {  
  95.             temp += sdf_hour_format.format(cale.getTime());  
  96.             return temp;  
  97.         } catch (Exception e) {  
  98.             logger.debug("DateUtil.getTime():" + e.getMessage());  
  99.             return "";  
  100.         }  
  101.     }  
  102.   
  103.     /** 
  104.      * 统计时开始日期的默认值 
  105.      * @author dylan_xu 
  106.      * @date Mar 11, 2012 
  107.      * @return 
  108.      */  
  109.     public static String getStartDate() {  
  110.         try {  
  111.             return getYear() + "-01-01";  
  112.         } catch (Exception e) {  
  113.             logger.debug("DateUtil.getStartDate():" + e.getMessage());  
  114.             return "";  
  115.         }  
  116.     }  
  117.   
  118.     /** 
  119.      * 统计时结束日期的默认值 
  120.      * @author dylan_xu 
  121.      * @date Mar 11, 2012 
  122.      * @return 
  123.      */  
  124.     public static String getEndDate() {  
  125.         try {  
  126.             return getDate();  
  127.         } catch (Exception e) {  
  128.             logger.debug("DateUtil.getEndDate():" + e.getMessage());  
  129.             return "";  
  130.         }  
  131.     }  
  132.   
  133.     /** 
  134.      * 获得服务器当前日期的年份 
  135.      * @author dylan_xu 
  136.      * @date Mar 11, 2012 
  137.      * @return 
  138.      */  
  139.     public static String getYear() {  
  140.         try {  
  141.             return String.valueOf(cale.get(Calendar.YEAR));  
  142.         } catch (Exception e) {  
  143.             logger.debug("DateUtil.getYear():" + e.getMessage());  
  144.             return "";  
  145.         }  
  146.     }  
  147.   
  148.     /** 
  149.      * 获得服务器当前日期的月份 
  150.      * @author dylan_xu 
  151.      * @date Mar 11, 2012 
  152.      * @return 
  153.      */  
  154.     public static String getMonth() {  
  155.         try {  
  156.             java.text.DecimalFormat df = new java.text.DecimalFormat();  
  157.             df.applyPattern("00;00");  
  158.             return df.format((cale.get(Calendar.MONTH) + 1));  
  159.         } catch (Exception e) {  
  160.             logger.debug("DateUtil.getMonth():" + e.getMessage());  
  161.             return "";  
  162.         }  
  163.     }  
  164.   
  165.     /** 
  166.      * 获得服务器在当前月中天数 
  167.      * @author dylan_xu 
  168.      * @date Mar 11, 2012 
  169.      * @return 
  170.      */  
  171.     public static String getDay() {  
  172.         try {  
  173.             return String.valueOf(cale.get(Calendar.DAY_OF_MONTH));  
  174.         } catch (Exception e) {  
  175.             logger.debug("DateUtil.getDay():" + e.getMessage());  
  176.             return "";  
  177.         }  
  178.     }  
  179.   
  180.     /** 
  181.      * 比较两个日期相差的天数 
  182.      * @author dylan_xu 
  183.      * @date Mar 11, 2012 
  184.      * @param date1 
  185.      * @param date2 
  186.      * @return 
  187.      */  
  188.     public static int getMargin(String date1, String date2) {  
  189.         int margin;  
  190.         try {  
  191.             ParsePosition pos = new ParsePosition(0);  
  192.             ParsePosition pos1 = new ParsePosition(0);  
  193.             Date dt1 = sdf_date_format.parse(date1, pos);  
  194.             Date dt2 = sdf_date_format.parse(date2, pos1);  
  195.             long l = dt1.getTime() - dt2.getTime();  
  196.             margin = (int) (l / (24 * 60 * 60 * 1000));  
  197.             return margin;  
  198.         } catch (Exception e) {  
  199.             logger.debug("DateUtil.getMargin():" + e.toString());  
  200.             return 0;  
  201.         }  
  202.     }  
  203.   
  204.     /** 
  205.      * 比较两个日期相差的天数 
  206.      * @author dylan_xu 
  207.      * @date Mar 11, 2012 
  208.      * @param date1 
  209.      * @param date2 
  210.      * @return 
  211.      */  
  212.     public static double getDoubleMargin(String date1, String date2) {  
  213.         double margin;  
  214.         try {  
  215.             ParsePosition pos = new ParsePosition(0);  
  216.             ParsePosition pos1 = new ParsePosition(0);  
  217.             Date dt1 = sdf_datetime_format.parse(date1, pos);  
  218.             Date dt2 = sdf_datetime_format.parse(date2, pos1);  
  219.             long l = dt1.getTime() - dt2.getTime();  
  220.             margin = (l / (24 * 60 * 60 * 1000.00));  
  221.             return margin;  
  222.         } catch (Exception e) {  
  223.             logger.debug("DateUtil.getMargin():" + e.toString());  
  224.             return 0;  
  225.         }  
  226.     }  
  227.   
  228.     /** 
  229.      * 比较两个日期相差的月数 
  230.      * @author dylan_xu 
  231.      * @date Mar 11, 2012 
  232.      * @param date1 
  233.      * @param date2 
  234.      * @return 
  235.      */  
  236.     public static int getMonthMargin(String date1, String date2) {  
  237.         int margin;  
  238.         try {  
  239.             margin = (Integer.parseInt(date2.substring(04)) - Integer.parseInt(date1.substring(04))) * 12;  
  240.             margin += (Integer.parseInt(date2.substring(47).replaceAll("-0",  
  241.                     "-")) - Integer.parseInt(date1.substring(47).replaceAll("-0""-")));  
  242.             return margin;  
  243.         } catch (Exception e) {  
  244.             logger.debug("DateUtil.getMargin():" + e.toString());  
  245.             return 0;  
  246.         }  
  247.     }  
  248.   
  249.     /** 
  250.      * 返回日期加X天后的日期 
  251.      * @author dylan_xu 
  252.      * @date Mar 11, 2012 
  253.      * @param date 
  254.      * @param i 
  255.      * @return 
  256.      */  
  257.     public static String addDay(String date, int i) {  
  258.         try {  
  259.             GregorianCalendar gCal = new GregorianCalendar(  
  260.                     Integer.parseInt(date.substring(04)),   
  261.                     Integer.parseInt(date.substring(57)) - 1,   
  262.                     Integer.parseInt(date.substring(810)));  
  263.             gCal.add(GregorianCalendar.DATE, i);  
  264.             return sdf_date_format.format(gCal.getTime());  
  265.         } catch (Exception e) {  
  266.             logger.debug("DateUtil.addDay():" + e.toString());  
  267.             return getDate();  
  268.         }  
  269.     }  
  270.   
  271.     /** 
  272.      * 返回日期加X月后的日期 
  273.      * @author dylan_xu 
  274.      * @date Mar 11, 2012 
  275.      * @param date 
  276.      * @param i 
  277.      * @return 
  278.      */  
  279.     public static String addMonth(String date, int i) {  
  280.         try {  
  281.             GregorianCalendar gCal = new GregorianCalendar(  
  282.                     Integer.parseInt(date.substring(04)),   
  283.                     Integer.parseInt(date.substring(57)) - 1,   
  284.                     Integer.parseInt(date.substring(810)));  
  285.             gCal.add(GregorianCalendar.MONTH, i);  
  286.             return sdf_date_format.format(gCal.getTime());  
  287.         } catch (Exception e) {  
  288.             logger.debug("DateUtil.addMonth():" + e.toString());  
  289.             return getDate();  
  290.         }  
  291.     }  
  292.   
  293.     /** 
  294.      * 返回日期加X年后的日期 
  295.      * @author dylan_xu 
  296.      * @date Mar 11, 2012 
  297.      * @param date 
  298.      * @param i 
  299.      * @return 
  300.      */  
  301.     public static String addYear(String date, int i) {  
  302.         try {  
  303.             GregorianCalendar gCal = new GregorianCalendar(  
  304.                     Integer.parseInt(date.substring(04)),   
  305.                     Integer.parseInt(date.substring(57)) - 1,   
  306.                     Integer.parseInt(date.substring(810)));  
  307.             gCal.add(GregorianCalendar.YEAR, i);  
  308.             return sdf_date_format.format(gCal.getTime());  
  309.         } catch (Exception e) {  
  310.             logger.debug("DateUtil.addYear():" + e.toString());  
  311.             return "";  
  312.         }  
  313.     }  
  314.   
  315.     /** 
  316.      * 返回某年某月中的最大天 
  317.      * @author dylan_xu 
  318.      * @date Mar 11, 2012 
  319.      * @param year 
  320.      * @param month 
  321.      * @return 
  322.      */  
  323.     public static int getMaxDay(int iyear, int imonth) {  
  324.         int day = 0;  
  325.         try {  
  326.             if (imonth == 1 || imonth == 3 || imonth == 5 || imonth == 7  
  327.                     || imonth == 8 || imonth == 10 || imonth == 12) {  
  328.                 day = 31;  
  329.             } else if (imonth == 4 || imonth == 6 || imonth == 9 || imonth == 11) {  
  330.                 day = 30;  
  331.             } else if ((0 == (iyear % 4)) && (0 != (iyear % 100)) || (0 == (iyear % 400))) {  
  332.                 day = 29;  
  333.             } else {  
  334.                 day = 28;  
  335.             }  
  336.             return day;  
  337.         } catch (Exception e) {  
  338.             logger.debug("DateUtil.getMonthDay():" + e.toString());  
  339.             return 1;  
  340.         }  
  341.     }  
  342.   
  343.     /** 
  344.      * 格式化日期 
  345.      * @author dylan_xu 
  346.      * @date Mar 11, 2012 
  347.      * @param orgDate 
  348.      * @param Type 
  349.      * @param Span 
  350.      * @return 
  351.      */  
  352.     @SuppressWarnings("static-access")  
  353.     public String rollDate(String orgDate, int Type, int Span) {  
  354.         try {  
  355.             String temp = "";  
  356.             int iyear, imonth, iday;  
  357.             int iPos = 0;  
  358.             char seperater = '-';  
  359.             if (orgDate == null || orgDate.length() < 6) {  
  360.                 return "";  
  361.             }  
  362.   
  363.             iPos = orgDate.indexOf(seperater);  
  364.             if (iPos > 0) {  
  365.                 iyear = Integer.parseInt(orgDate.substring(0, iPos));  
  366.                 temp = orgDate.substring(iPos + 1);  
  367.             } else {  
  368.                 iyear = Integer.parseInt(orgDate.substring(04));  
  369.                 temp = orgDate.substring(4);  
  370.             }  
  371.   
  372.             iPos = temp.indexOf(seperater);  
  373.             if (iPos > 0) {  
  374.                 imonth = Integer.parseInt(temp.substring(0, iPos));  
  375.                 temp = temp.substring(iPos + 1);  
  376.             } else {  
  377.                 imonth = Integer.parseInt(temp.substring(02));  
  378.                 temp = temp.substring(2);  
  379.             }  
  380.   
  381.             imonth--;  
  382.             if (imonth < 0 || imonth > 11) {  
  383.                 imonth = 0;  
  384.             }  
  385.   
  386.             iday = Integer.parseInt(temp);  
  387.             if (iday < 1 || iday > 31)  
  388.                 iday = 1;  
  389.   
  390.             Calendar orgcale = Calendar.getInstance();  
  391.             orgcale.set(iyear, imonth, iday);  
  392.             temp = this.rollDate(orgcale, Type, Span);  
  393.             return temp;  
  394.         } catch (Exception e) {  
  395.             return "";  
  396.         }  
  397.     }  
  398.   
  399.     public static String rollDate(Calendar cal, int Type, int Span) {  
  400.         try {  
  401.             String temp = "";  
  402.             Calendar rolcale;  
  403.             rolcale = cal;  
  404.             rolcale.add(Type, Span);  
  405.             temp = sdf_date_format.format(rolcale.getTime());  
  406.             return temp;  
  407.         } catch (Exception e) {  
  408.             return "";  
  409.         }  
  410.     }  
  411.   
  412.     /** 
  413.      * 返回默认的日期格式 
  414.      * @author dylan_xu 
  415.      * @date Mar 11, 2012 
  416.      * @return 
  417.      */  
  418.     public static synchronized String getDatePattern() {  
  419.         defaultDatePattern = "yyyy-MM-dd";  
  420.         return defaultDatePattern;  
  421.     }  
  422.   
  423.     /** 
  424.      * 将指定日期按默认格式进行格式代化成字符串后输出如:yyyy-MM-dd 
  425.      * @author dylan_xu 
  426.      * @date Mar 11, 2012 
  427.      * @param aDate 
  428.      * @return 
  429.      */  
  430.     public static final String getDate(Date aDate) {  
  431.         SimpleDateFormat df = null;  
  432.         String returnValue = "";  
  433.         if (aDate != null) {  
  434.             df = new SimpleDateFormat(getDatePattern());  
  435.             returnValue = df.format(aDate);  
  436.         }  
  437.         return (returnValue);  
  438.     }  
  439.   
  440.     /** 
  441.      * 取得给定日期的时间字符串,格式为当前默认时间格式 
  442.      * @author dylan_xu 
  443.      * @date Mar 11, 2012 
  444.      * @param theTime 
  445.      * @return 
  446.      */  
  447.     public static String getTimeNow(Date theTime) {  
  448.         return getDateTime(timePattern, theTime);  
  449.     }  
  450.   
  451.     /** 
  452.      * 取得当前时间的Calendar日历对象 
  453.      * @author dylan_xu 
  454.      * @date Mar 11, 2012 
  455.      * @return 
  456.      * @throws ParseException 
  457.      */  
  458.     public Calendar getToday() throws ParseException {  
  459.         Date today = new Date();  
  460.         SimpleDateFormat df = new SimpleDateFormat(getDatePattern());  
  461.         String todayAsString = df.format(today);  
  462.         Calendar cal = new GregorianCalendar();  
  463.         cal.setTime(convertStringToDate(todayAsString));  
  464.         return cal;  
  465.     }  
  466.   
  467.     /** 
  468.      * 将日期类转换成指定格式的字符串形式 
  469.      * @author dylan_xu 
  470.      * @date Mar 11, 2012 
  471.      * @param aMask 
  472.      * @param aDate 
  473.      * @return 
  474.      */  
  475.     public static final String getDateTime(String aMask, Date aDate) {  
  476.         SimpleDateFormat df = null;  
  477.         String returnValue = "";  
  478.   
  479.         if (aDate == null) {  
  480.             logger.error("aDate is null!");  
  481.         } else {  
  482.             df = new SimpleDateFormat(aMask);  
  483.             returnValue = df.format(aDate);  
  484.         }  
  485.         return (returnValue);  
  486.     }  
  487.   
  488.     /** 
  489.      * 将指定的日期转换成默认格式的字符串形式 
  490.      * @author dylan_xu 
  491.      * @date Mar 11, 2012 
  492.      * @param aDate 
  493.      * @return 
  494.      */  
  495.     public static final String convertDateToString(Date aDate) {  
  496.         return getDateTime(getDatePattern(), aDate);  
  497.     }  
  498.   
  499.     /** 
  500.      * 将日期字符串按指定格式转换成日期类型 
  501.      * @author dylan_xu 
  502.      * @date Mar 11, 2012 
  503.      * @param aMask 指定的日期格式,如:yyyy-MM-dd 
  504.      * @param strDate 待转换的日期字符串 
  505.      * @return 
  506.      * @throws ParseException 
  507.      */  
  508.     public static final Date convertStringToDate(String aMask, String strDate)  
  509.             throws ParseException {  
  510.         SimpleDateFormat df = null;  
  511.         Date date = null;  
  512.         df = new SimpleDateFormat(aMask);  
  513.   
  514.         if (logger.isDebugEnabled()) {  
  515.             logger.debug("converting '" + strDate + "' to date with mask '" + aMask + "'");  
  516.         }  
  517.         try {  
  518.             date = df.parse(strDate);  
  519.         } catch (ParseException pe) {  
  520.             logger.error("ParseException: " + pe);  
  521.             throw pe;  
  522.         }  
  523.         return (date);  
  524.     }  
  525.   
  526.     /** 
  527.      * 将日期字符串按默认格式转换成日期类型 
  528.      * @author dylan_xu 
  529.      * @date Mar 11, 2012 
  530.      * @param strDate 
  531.      * @return 
  532.      * @throws ParseException 
  533.      */  
  534.     public static Date convertStringToDate(String strDate)  
  535.             throws ParseException {  
  536.         Date aDate = null;  
  537.   
  538.         try {  
  539.             if (logger.isDebugEnabled()) {  
  540.                 logger.debug("converting date with pattern: " + getDatePattern());  
  541.             }  
  542.             aDate = convertStringToDate(getDatePattern(), strDate);  
  543.         } catch (ParseException pe) {  
  544.             logger.error("Could not convert '" + strDate + "' to a date, throwing exception");  
  545.             throw new ParseException(pe.getMessage(), pe.getErrorOffset());  
  546.         }  
  547.         return aDate;  
  548.     }  
  549.   
  550.     /** 
  551.      * 返回一个JAVA简单类型的日期字符串 
  552.      * @author dylan_xu 
  553.      * @date Mar 11, 2012 
  554.      * @return 
  555.      */  
  556.     public static String getSimpleDateFormat() {  
  557.         SimpleDateFormat formatter = new SimpleDateFormat();  
  558.         String NDateTime = formatter.format(new Date());  
  559.         return NDateTime;  
  560.     }  
  561.       
  562.     /** 
  563.      * 将指定字符串格式的日期与当前时间比较 
  564.      * @author DYLAN 
  565.      * @date Feb 17, 2012 
  566.      * @param strDate 需要比较时间 
  567.      * @return  
  568.      *      <p> 
  569.      *      int code 
  570.      *      <ul> 
  571.      *      <li>-1 当前时间 < 比较时间 </li> 
  572.      *      <li> 0 当前时间 = 比较时间 </li> 
  573.      *      <li>>=1当前时间 > 比较时间 </li> 
  574.      *      </ul> 
  575.      *      </p> 
  576.      */  
  577.     public static int compareToCurTime (String strDate) {  
  578.         if (StringUtils.isBlank(strDate)) {  
  579.             return -1;  
  580.         }  
  581.         Date curTime = cale.getTime();  
  582.         String strCurTime = null;  
  583.         try {  
  584.             strCurTime = sdf_datetime_format.format(curTime);  
  585.         } catch (Exception e) {  
  586.             if (logger.isDebugEnabled()) {  
  587.                 logger.debug("[Could not format '" + strDate + "' to a date, throwing exception:" + e.getLocalizedMessage() + "]");  
  588.             }  
  589.         }  
  590.         if (StringUtils.isNotBlank(strCurTime)) {  
  591.             return strCurTime.compareTo(strDate);  
  592.         }  
  593.         return -1;  
  594.     }  
  595.       
  596.     /** 
  597.      * 为查询日期添加最小时间 
  598.      *  
  599.      * @param 目标类型Date 
  600.      * @param 转换参数Date 
  601.      * @return 
  602.      */  
  603.     @SuppressWarnings("deprecation")  
  604.     public static Date addStartTime(Date param) {  
  605.         Date date = param;  
  606.         try {  
  607.             date.setHours(0);  
  608.             date.setMinutes(0);  
  609.             date.setSeconds(0);  
  610.             return date;  
  611.         } catch (Exception ex) {  
  612.             return date;  
  613.         }  
  614.     }  
  615.   
  616.     /** 
  617.      * 为查询日期添加最大时间 
  618.      *  
  619.      * @param 目标类型Date 
  620.      * @param 转换参数Date 
  621.      * @return 
  622.      */  
  623.     @SuppressWarnings("deprecation")  
  624.     public static Date addEndTime(Date param) {  
  625.         Date date = param;  
  626.         try {  
  627.             date.setHours(23);  
  628.             date.setMinutes(59);  
  629.             date.setSeconds(0);  
  630.             return date;  
  631.         } catch (Exception ex) {  
  632.             return date;  
  633.         }  
  634.     }  
  635.   
  636.     /** 
  637.      * 返回系统现在年份中指定月份的天数 
  638.      *  
  639.      * @param 月份month 
  640.      * @return 指定月的总天数 
  641.      */  
  642.     @SuppressWarnings("deprecation")  
  643.     public static String getMonthLastDay(int month) {  
  644.         Date date = new Date();  
  645.         int[][] day = { { 0302831303130313130313031 },  
  646.                 { 0312931303130313130313031 } };  
  647.         int year = date.getYear() + 1900;  
  648.         if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {  
  649.             return day[1][month] + "";  
  650.         } else {  
  651.             return day[0][month] + "";  
  652.         }  
  653.     }  
  654.   
  655.     /** 
  656.      * 返回指定年份中指定月份的天数 
  657.      *  
  658.      * @param 年份year 
  659.      * @param 月份month 
  660.      * @return 指定月的总天数 
  661.      */  
  662.     public static String getMonthLastDay(int year, int month) {  
  663.         int[][] day = { { 0302831303130313130313031 },  
  664.                 { 0312931303130313130313031 } };  
  665.         if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {  
  666.             return day[1][month] + "";  
  667.         } else {  
  668.             return day[0][month] + "";  
  669.         }  
  670.     }  
  671.   
  672.     /** 
  673.      * 判断是平年还是闰年 
  674.      * @author dylan_xu 
  675.      * @date Mar 11, 2012 
  676.      * @param year 
  677.      * @return 
  678.      */   
  679.     public static boolean isLeapyear(int year) {  
  680.         if ((year % 4 == 0 && year % 100 != 0) || (year % 400) == 0) {  
  681.             return true;  
  682.         } else {  
  683.             return false;  
  684.         }  
  685.     }  
  686.   
  687.     /** 
  688.      * 取得当前时间的日戳 
  689.      * @author dylan_xu 
  690.      * @date Mar 11, 2012 
  691.      * @return 
  692.      */  
  693.     @SuppressWarnings("deprecation")  
  694.     public static String getTimestamp() {  
  695.         Date date = cale.getTime();  
  696.         String timestamp = "" + (date.getYear() + 1900) + date.getMonth()  
  697.                 + date.getDate() + date.getMinutes() + date.getSeconds()  
  698.                 + date.getTime();  
  699.         return timestamp;  
  700.     }  
  701.   
  702.     /** 
  703.      * 取得指定时间的日戳 
  704.      *  
  705.      * @return 
  706.      */  
  707.     @SuppressWarnings("deprecation")  
  708.     public static String getTimestamp(Date date) {  
  709.         String timestamp = "" + (date.getYear() + 1900) + date.getMonth()  
  710.                 + date.getDate() + date.getMinutes() + date.getSeconds()  
  711.                 + date.getTime();  
  712.         return timestamp;  
  713.     }  
  714.   
  715.     public static void main(String[] args) {  
  716.         System.out.println(getYear() + "|" + getMonth() + "|" + getDate());  
  717.     }  
  718. }  

 

附上100%完整的系统项目源码:

JSP图书馆管理系统:http://blog.sina.com.cn/s/blog_4b5bc0110101a97e.html

 

JSP酒店宾馆管理系统:http://blog.sina.com.cn/s/blog_4b5bc0110101a97m.html

 

JSP学生信息管理系统:http://blog.sina.com.cn/s/blog_4b5bc0110101a97r.html

 

 

JSP房屋出售租赁管理系统:http://blog.sina.com.cn/s/blog_4b5bc0110101a98c.html

 

J2EE酒店在线预订系统:http://blog.sina.com.cn/s/blog_4b5bc0110101a97v.html

 

SSH/J2EE人力资源管理系统:http://blog.sina.com.cn/s/blog_4b5bc01101019ztu.html

 

ssh2图书管理系统(图书馆管理系统): http://blog.sina.com.cn/s/blog_4b5bc0110101adf0.html

 

毕业设计-JSP图书馆管理系统:http://blog.sina.com.cn/s/blog_4b5bc0110101a979.html

 

毕业设计-JSP酒店宾馆管理系统:http://blog.sina.com.cn/s/blog_4b5bc0110101a97z.html

 

毕业设计-JSP学生信息管理系统:http://blog.sina.com.cn/s/blog_4b5bc0110101a987.html

 

毕业设计-JSP房屋出售租赁管理系统:http://blog.sina.com.cn/s/blog_4b5bc0110101a98a.html

 

毕业设计-s2sh/j2ee图书管理系统 :http://blog.sina.com.cn/s/blog_4b5bc0110101ain5.html

 

ssh2图书管理系统(图书馆管理系统):http://blog.sina.com.cn/s/blog_4b5bc0110101adf0.html

 

毕业设计-jsp信息发布系统(信息供求网站系统):http://blog.sina.com.cn/s/blog_4b5bc0110101aiou.html

 

struts2+servlet+jsp信息发布系统:http://blog.sina.com.cn/s/blog_4b5bc0110101aiop.html

 

毕业设计-jsp电子商城 网上商城系统:http://blog.sina.com.cn/s/blog_4b5bc0110101aiof.html

 

jsp电子商城 网上商城系统(struts+servlet+jsp):http://blog.sina.com.cn/s/blog_4b5bc0110101aio9.html

 

毕业设计-图书管理系统 jsp图书馆系统Struts2+Spring+Ibatis+extjs(ssi):

http://blog.sina.com.cn/s/blog_4b5bc0110101ainu.html

 

Struts2+Spring+Ibatis+extjs(ssi)图书管理系统 jsp图书馆系统:

http://blog.sina.com.cn/s/blog_4b5bc0110101ainh.html

 

ssh2图书管理系统(图书馆管理系统):http://blog.sina.com.cn/s/blog_4b5bc0110101adf0.html

 

毕业设计-s2sh/j2ee图书管理系统 struts2+spring+hibernate:

http://blog.sina.com.cn/s/blog_4b5bc0110101ain5.html

 

 jsp酒店在线预订系统 酒店客房预定系统:http://blog.sina.com.cn/s/blog_4b5bc0110101atb8.html

 

毕业设计 jsp酒店在线预订系统 酒店客房预定系统:http://blog.sina.com.cn/s/blog_4b5bc0110101atbb.html

 

人力资源管理系统 S2SH/J2EE/JAVA:http://blog.sina.com.cn/s/blog_4b5bc0110101azoz.html

 

毕业设计-人力资源管理系统 S2SH/J2EE/JSP:http://blog.sina.com.cn/s/blog_4b5bc0110101azp0.html

 

ssh网上商城 电子商城struts hibernate  :http://blog.sina.com.cn/s/blog_4b5bc0110101b5gr.html

 

毕业设计 ssh网上商城 电子商城struts hibernate:http://blog.sina.com.cn/s/blog_4b5bc0110101b5h2.html

 

毕业设计 ssh电子相册管理系统:http://blog.sina.com.cn/s/blog_4b5bc0110101bkbl.html

 

实用技术:

J2EE/JSP应用技术70实例(源码)(实用): http://blog.sina.com.cn/s/blog_4b5bc0110101acms.html

分享到:
评论

相关推荐

    Java日期处理工具类

    基本涵盖了各种场景的日期处理需要,包括时间类型转换,获取N天前后,月初月末,某段时间按天拆分等功能,欢迎使用。

    实例解析Java日期格式工具类DateUtil.java

    本文主要对Java日期格式工具类DateUtil.java进行实例解析。具有一定的参考价值,下面跟着小编一起来看下吧

    日期 dateutil Java 日期工具类

    快速日期格式化类 ,线程安全的 包括:获取 DateUtil.STYLE格式的日期 字符转日期 日期转字符 字符日期从src_format改为dest_format 返回当前系统日期时间等

    Java开发工具类

    - QrcodeUtils.java\防止SQL注入和XSS攻击Filter\获取文件绝对路径最后的文件夹名称\加密工具类 - CryptoUtils.java\日期工具类 - DateUtil.java\图片处理工具类 - ImageUtils.java\文件相关操作工具类——FileUtils...

    DateUtil java日期工具类

    个人整理的java的关于Date对象的日期时间处理工具类,若存在问题还望大家在评论中提出,我将一直进行修改维护

    DateUtil.java 时间日期处理工具类

    包含"yyyy-MM-dd"、"yyyy-MM-dd HH:mm:ss"、"yyyyMMdd"、"yyyyMMddHHmmss"、"yyyy-MM"、"yyyyMM"等时间格式的转换。 其中: 1.String转Timestamp stringToTimestamp(String dateStr); 2.比较传入时间与当前时间前一...

    Android-Java中的日期转化格式DateUtil工具类

    Java中的日期转化格式DateUtil工具类

    【强2】30个java工具类

    [工具类] 日期DateUtil.java [工具类] 文件FileUtil.java [工具类] 通信客户端simpleClient.java [工具类] 通信服务端simpleServer.java [工具类] 框架StringUtil.java [工具类] 时间Time.java [工具类] 时间工具...

    30个java工具类

    [工具类] 日期DateUtil.java [工具类] 文件FileUtil.java [工具类] 通信客户端simpleClient.java [工具类] 通信服务端simpleServer.java [工具类] 框架StringUtil.java [工具类] 时间Time.java [工具类] 时间工具...

    Java - DateUtil 日期时间转换工具类

    资源名称:DateUtil 日期时间转换工具类 内容概要:日期时间转换工具类,包括基本的Date类型,String类型,TimeStamp类型,LocalDateTime类型,LocalDate类型之间的互相转换,还提供了许多与时间获取,时间计算有关...

    java操作 工具类1.0

    工具类 DateUtil(日期处理) CalendarUtil(时间处理) PinyinUtils(汉字转拼音) PropertiesFileUitl(键值对文件存取)等,继续更新

    DateUtil.Java 日期时间操作函数库代码.rar

    一个Java 日期时间操作函数集 DateUtil.java代码,用于时间和日期操作的工具类,DateUtil类包含了标准的时间和日期格式,以及这些格式在字符串及日期之间转换的方法,格式化后的系统当前时间,如果有异常产生,返回...

    java util工具类

    java util帮助类,包括日期工具类、字符串处理工具类、上传工具类、http请求工具类、hibernate工具类、MD5工具类、分页工具类等。 CodeStringUtil.java CreateFileUtil.java DateUtil.java FileCopy.java ...

    DateUtil日期工具类

    安卓的时间工具类,包括日期格式化,时间戳转化成日期字符串,获取时间,获取年月份,上一个月,下一个月,闰年等等与日期时间相关的工具

    我积攒的java工具类 基本满足开发需要的工具类

    D:\002 我的工具类\003 日期\DateUtil.java D:\002 我的工具类\004 加密 D:\002 我的工具类\004 加密\DESUtils.java D:\002 我的工具类\004 加密\PasswordHash.java D:\002 我的工具类\005 随机数 D:\002 我的工具类...

    java DateUtil工具类时间戳类型转换详解

    主要为大家详细介绍了java DateUtil工具类时间戳类型转换的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    邮件发送 poi 二维码 条形码等java常用的工具类

    DateUtil:时间处理工具类 DBConnection:jdbc工具类 FileOperater:文件处理工具类 包括 读取文本文件,写出文本文件, 大文件切分,文件下载,文件或文件夹比较,文件或文件夹遍历筛选 ...... HttpClientCard...

    java转换万能工具类附带jar包

    一个json相互转换的万能java类(JsonUtil.java), 这个下面就是万能转换类的方法注释,懂java的相信都能看懂。 /** *//** * 从一个JSON 对象字符格式中得到一个java对象 * @param jsonString * @param ...

    DateUtil.java

    DateUtil.java DateStyle.java Week.java 日期工具类及依赖类

    DateUtil获取时间工具类

    使用预设格式将字符串转为Date,使用参数Format将字符串转为Date,字符串转换成日期 如果转换格式为空,则利用默认格式进行转换操作,日期转换为字符串

Global site tag (gtag.js) - Google Analytics