• 时间工具类--java


    直接上代码:

       1 package com.hyhl.util;
       2 
       3 import java.text.DateFormat;
       4 import java.text.ParseException;
       5 import java.text.SimpleDateFormat;
       6 import java.util.*;
       7 
       8 public class DateTimeUtil {
       9 
      10     public final static int TIME_DAY_MILLISECOND = 86400000;
      11 
      12     public final static int TIME_HOUR_MILLISECOND = 1000 * 60 * 60 * 1;
      13 
      14     public final static int TIME_SECONDS_MILLISECOND = 1000;
      15 
      16     // /
      17     // 定义时间日期显示格式
      18     // /
      19     private final static String DATE_FORMAT = "yyyy-MM-dd";
      20 
      21     private final static String DATE_FORMAT_CN = "yyyy年MM月dd日";
      22 
      23     private final static String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
      24 
      25     private final static String TIME_FORMAT_CN = "yyyy年MM月dd日 HH:mm:ss";
      26 
      27     private final static String MONTH_FORMAT = "yyyy-MM";
      28 
      29     private final static String DAY_FORMAT = "yyyyMMdd";
      30 
      31     private final static String TIME_FORMAT_NUM = "MMddHHmmss";
      32 
      33     // private final static String TIME_FORMAT_MILLI = "yyyy-MM-dd
      34     // HH:mm:ss.SSS";
      35 
      36     /**
      37      * 取得当前系统时间,返回java.util.Date类型
      38      *
      39      * @return java.util.Date 返回服务器当前系统时间
      40      * @see java.util.Date
      41      */
      42     public static java.util.Date getCurrDate() {
      43         return new java.util.Date();
      44     }
      45 
      46     /**
      47      * 取得当前系统时间戳
      48      *
      49      * @return java.sql.Timestamp 系统时间戳
      50      * @see java.sql.Timestamp
      51      */
      52     public static java.sql.Timestamp getCurrTimestamp() {
      53         return new java.sql.Timestamp(System.currentTimeMillis());
      54     }
      55 
      56     /**
      57      * 返回当前时间是上午还是下午
      58      *
      59      * @return
      60      */
      61     public static Integer getCurrDateAMorPM() {
      62         Calendar calendar = Calendar.getInstance();
      63         return calendar.get(Calendar.AM_PM);
      64     }
      65 
      66     /**
      67      * 得到格式化后的日期,格式为yyyy-MM-dd,如2009-10-15
      68      *
      69      * @param currDate 要格式化的日期
      70      * @return String 返回格式化后的日期,默认格式为为yyyy-MM-dd,如2009-10-15
      71      * @see #getFormatDate(java.util.Date, String)
      72      */
      73     public static String getFormatDate(java.util.Date currDate) {
      74         return getFormatDate(currDate, DATE_FORMAT);
      75     }
      76 
      77     /**
      78      * 得到格式化后的日期,格式为yyyy-MM-dd,如2009-10-15
      79      *
      80      * @param currDate 要格式化的日期
      81      * @return Date 返回格式化后的日期,默认格式为为yyyy-MM-dd,如2009-10-15
      82      * @see #getFormatDate(java.util.Date)
      83      */
      84     public static Date getFormatDateToDate(java.util.Date currDate) {
      85         return getFormatDate(getFormatDate(currDate));
      86     }
      87 
      88     /**
      89      * 得到格式化后的日期,格式为yyyy年MM月dd日,如2009年02月15日
      90      *
      91      * @param currDate 要格式化的日期
      92      * @return String 返回格式化后的日期,默认格式为yyyy年MM月dd日,如2009年02月15日
      93      * @see #getFormatDate(java.util.Date, String)
      94      */
      95     public static String getFormatDate_CN(java.util.Date currDate) {
      96         return getFormatDate(currDate, DATE_FORMAT_CN);
      97     }
      98 
      99     /**
     100      * 得到格式化后的日期,格式为yyyy年MM月dd日,如2009年02月15日
     101      *
     102      * @param currDate 要格式化的日期
     103      * @return Date 返回格式化后的日期,默认格式为yyyy年MM月dd日,如2009年02月15日
     104      * @see #getFormatDate_CN(String)
     105      */
     106     public static Date getFormatDateToDate_CN(java.util.Date currDate) {
     107         return getFormatDate_CN(getFormatDate_CN(currDate));
     108     }
     109 
     110     /**
     111      * 得到格式化后的日期,格式为yyyy-MM-dd,如2009-10-15
     112      *
     113      * @param currDate 要格式化的日期
     114      * @return Date 返回格式化后的日期,默认格式为yyyy-MM-dd,如2009-10-15
     115      * @see #getFormatDate(String, String)
     116      */
     117     public static Date getFormatDate(String currDate) {
     118         return getFormatDate(currDate, DATE_FORMAT);
     119     }
     120 
     121     /**
     122      * 得到格式化后的日期,格式为yyyy年MM月dd日,如2009年02月15日
     123      *
     124      * @param currDate 要格式化的日期
     125      * @return 返回格式化后的日期,默认格式为yyyy年MM月dd日,如2009年02月15日
     126      * @see #getFormatDate(String, String)
     127      */
     128     public static Date getFormatDate_CN(String currDate) {
     129         return getFormatDate(currDate, DATE_FORMAT_CN);
     130     }
     131 
     132     /**
     133      * 根据格式得到格式化后的日期
     134      *
     135      * @param currDate 要格式化的日期
     136      * @param format   日期格式,如yyyy-MM-dd
     137      * @return String 返回格式化后的日期,格式由参数<code>format</code>
     138      * 定义,如yyyy-MM-dd,如2009-10-15
     139      * @see java.text.SimpleDateFormat#format(java.util.Date)
     140      */
     141     public static String getFormatDate(java.util.Date currDate, String format) {
     142         SimpleDateFormat dtFormatdB = null;
     143         try {
     144             dtFormatdB = new SimpleDateFormat(format);
     145             return dtFormatdB.format(currDate);
     146         } catch (Exception e) {
     147             dtFormatdB = new SimpleDateFormat(DATE_FORMAT);
     148             try {
     149                 return dtFormatdB.format(currDate);
     150             } catch (Exception ex) {
     151             }
     152         }
     153         return null;
     154     }
     155 
     156     /**
     157      * 得到格式化后的时间,格式为yyyy-MM-dd HH:mm:ss,如2009-10-15 15:23:45
     158      *
     159      * @param currDate 要格式化的时间
     160      * @return String 返回格式化后的时间,默认格式为yyyy-MM-dd HH:mm:ss,如2009-10-15 15:23:45
     161      * @see #getFormatDateTime(java.util.Date, String)
     162      */
     163     public static String getFormatDateTime(java.util.Date currDate) {
     164         return getFormatDateTime(currDate, TIME_FORMAT);
     165     }
     166 
     167     /**
     168      * 得到格式化后的时间,格式为yyyy-MM-dd HH:mm:ss,如2009-10-15 15:23:45
     169      *
     170      * @param currDate 要格式环的时间
     171      * @return Date 返回格式化后的时间,默认格式为yyyy-MM-dd HH:mm:ss,如2009-10-15 15:23:45
     172      * @see #getFormatDateTime(String)
     173      */
     174     public static Date getFormatDateTimeToTime(java.util.Date currDate) {
     175         return getFormatDateTime(getFormatDateTime(currDate));
     176     }
     177 
     178     /**
     179      * 得到格式化后的时间,格式为yyyy-MM-dd HH:mm:ss,如2009-10-15 15:23:45
     180      *
     181      * @param currDate 要格式化的时间
     182      * @return Date 返回格式化后的时间,默认格式为yyyy-MM-dd HH:mm:ss,如2009-10-15 15:23:45
     183      * @see #getFormatDateTime(String, String)
     184      */
     185     public static Date getFormatDateTime(String currDate) {
     186         return getFormatDateTime(currDate, TIME_FORMAT);
     187     }
     188 
     189     /**
     190      * 得到格式化后的时间,格式为yyyy年MM月dd日 HH:mm:ss,如2009年02月15日 15:23:45
     191      *
     192      * @param currDate 要格式化的时间
     193      * @return String 返回格式化后的时间,默认格式为yyyy年MM月dd日 HH:mm:ss,如2009年02月15日 15:23:45
     194      * @see #getFormatDateTime(java.util.Date, String)
     195      */
     196     public static String getFormatDateTime_CN(java.util.Date currDate) {
     197         return getFormatDateTime(currDate, TIME_FORMAT_CN);
     198     }
     199 
     200     /**
     201      * 得到格式化后的时间,格式为yyyy年MM月dd日 HH:mm:ss,如2009年02月15日 15:23:45
     202      *
     203      * @param currDate 要格式化的时间
     204      * @return Date 返回格式化后的时间,默认格式为yyyy年MM月dd日 HH:mm:ss,如2009年02月15日 15:23:45
     205      * @see #getFormatDateTime_CN(String)
     206      */
     207     public static Date getFormatDateTimeToTime_CN(java.util.Date currDate) {
     208         return getFormatDateTime_CN(getFormatDateTime_CN(currDate));
     209     }
     210 
     211     /**
     212      * 得到格式化后的时间,格式为yyyy年MM月dd日 HH:mm:ss,如2009年02月15日 15:23:45
     213      *
     214      * @param currDate 要格式化的时间
     215      * @return Date 返回格式化后的时间,默认格式为yyyy年MM月dd日 HH:mm:ss,如2009年02月15日 15:23:45
     216      * @see #getFormatDateTime(String, String)
     217      */
     218     public static Date getFormatDateTime_CN(String currDate) {
     219         return getFormatDateTime(currDate, TIME_FORMAT_CN);
     220     }
     221 
     222     /**
     223      * 根据格式得到格式化后的时间
     224      *
     225      * @param currDate 要格式化的时间
     226      * @param format   时间格式,如yyyy-MM-dd HH:mm:ss
     227      * @return String 返回格式化后的时间,格式由参数<code>format</code>定义,如yyyy-MM-dd HH:mm:ss
     228      * @see java.text.SimpleDateFormat#format(java.util.Date)
     229      */
     230     public static String getFormatDateTime(java.util.Date currDate, String format) {
     231         SimpleDateFormat dtFormatdB = null;
     232         try {
     233             dtFormatdB = new SimpleDateFormat(format);
     234             return dtFormatdB.format(currDate);
     235         } catch (Exception e) {
     236             dtFormatdB = new SimpleDateFormat(TIME_FORMAT);
     237             try {
     238                 return dtFormatdB.format(currDate);
     239             } catch (Exception ex) {
     240             }
     241         }
     242         return null;
     243     }
     244 
     245     /**
     246      * 根据格式得到格式化后的日期
     247      *
     248      * @param currDate 要格式化的日期
     249      * @param format   日期格式,如yyyy-MM-dd
     250      * @return Date 返回格式化后的日期,格式由参数<code>format</code>
     251      * 定义,如yyyy-MM-dd,如2009-10-15
     252      * @see java.text.SimpleDateFormat#parse(java.lang.String)
     253      */
     254     public static Date getFormatDate(String currDate, String format) {
     255         SimpleDateFormat dtFormatdB = null;
     256         try {
     257             dtFormatdB = new SimpleDateFormat(format);
     258             return dtFormatdB.parse(currDate);
     259         } catch (Exception e) {
     260             dtFormatdB = new SimpleDateFormat(DATE_FORMAT);
     261             try {
     262                 return dtFormatdB.parse(currDate);
     263             } catch (Exception ex) {
     264             }
     265         }
     266         return null;
     267     }
     268 
     269     /**
     270      * 根据格式得到格式化后的时间
     271      *
     272      * @param currDate 要格式化的时间
     273      * @param format   时间格式,如yyyy-MM-dd HH:mm:ss
     274      * @return Date 返回格式化后的时间,格式由参数<code>format</code>定义,如yyyy-MM-dd HH:mm:ss
     275      * @see java.text.SimpleDateFormat#parse(java.lang.String)
     276      */
     277     public static Date getFormatDateTime(String currDate, String format) {
     278         SimpleDateFormat dtFormatdB = null;
     279         try {
     280             dtFormatdB = new SimpleDateFormat(format);
     281             return dtFormatdB.parse(currDate);
     282         } catch (Exception e) {
     283             dtFormatdB = new SimpleDateFormat(TIME_FORMAT);
     284             try {
     285                 return dtFormatdB.parse(currDate);
     286             } catch (Exception ex) {
     287             }
     288         }
     289         return null;
     290     }
     291 
     292     /**
     293      * @param time Seconds 传入参数 秒
     294      * @return 返回 格式 hh:mm:ss 秒
     295      */
     296     public static String getFormatHourAndMinuteTime(long time) {
     297         if (time < 60) {
     298             return String.valueOf(time);
     299         }
     300         if (time < 60 * 60) {
     301             int seconds = (int) (time % 60l);
     302             int minutes = (int) (time / (60l));
     303             return String.valueOf(minutes) + ":" + (seconds < 10 ? ("0" + String.valueOf(seconds)) : String.valueOf(seconds));
     304         }
     305         int seconds = (int) (time % 60l);
     306         int minutes = (int) ((time / 60l) % 60l);
     307         int hours = (int) (time / (60l * 60l));
     308         return hours + ":" + (minutes < 10 ? ("0" + String.valueOf(minutes)) : String.valueOf(minutes)) + ":"
     309                 + (seconds < 10 ? ("0" + String.valueOf(seconds)) : String.valueOf(seconds));
     310     }
     311 
     312     /**
     313      * 得到本日的上月时间 如果当日为2007-9-1,那么获得2007-8-1
     314      */
     315     public static String getDateBeforeMonth() {
     316         Calendar cal = Calendar.getInstance();
     317         cal.add(Calendar.MONTH, -1);
     318         return getFormatDate(cal.getTime(), DATE_FORMAT);
     319     }
     320 
     321     /**
     322      * 得到本日的前几个月时间 如果number=2当日为2007-9-1,那么获得2007-7-1
     323      */
     324     public static String getDateBeforeMonth(int number) {
     325         Calendar cal = Calendar.getInstance();
     326         cal.add(Calendar.MONTH, -number);
     327         return getFormatDate(cal.getTime(), DATE_FORMAT);
     328     }
     329 
     330     public static long getDaysOfDates(Date first, Date second) {
     331         Date d1 = getFormatDateTime(getFormatDate(first), DATE_FORMAT);
     332         Date d2 = getFormatDateTime(getFormatDate(second), DATE_FORMAT);
     333 
     334         long mils = d1.getTime() - d2.getTime();
     335 
     336         return mils / (TIME_DAY_MILLISECOND);
     337     }
     338 
     339     /**
     340      * 获得两个Date型日期之间相差的天数(第2个减第1个)
     341      * first, Date second
     342      *
     343      * @return int 相差的天数
     344      */
     345     public static int getDaysBetweenDates(Date first, Date second) {
     346         Date d1 = getFormatDateTime(getFormatDate(first), DATE_FORMAT);
     347         Date d2 = getFormatDateTime(getFormatDate(second), DATE_FORMAT);
     348 
     349         Long mils = (d2.getTime() - d1.getTime()) / (TIME_DAY_MILLISECOND);
     350 
     351         return mils.intValue();
     352     }
     353 
     354     /**
     355      * 获得两个Date型日期之间相差的小时数(第2个减第1个)
     356      * <p>
     357      * first, Date second
     358      *
     359      * @return int 相差的小时数
     360      */
     361     public static int getHoursBetweenDates(Date first, Date second) {
     362 
     363         Date d1 = getFormatDateTime(getFormatDate(first), DATE_FORMAT);
     364         Date d2 = getFormatDateTime(getFormatDate(second), DATE_FORMAT);
     365 
     366         Long mils = (d2.getTime() - d1.getTime()) / (TIME_HOUR_MILLISECOND);
     367 
     368         return mils.intValue();
     369 
     370     }
     371 
     372     public static int getSecondsBetweenDates(Date first, Date second) {
     373         Long mils = (second.getTime() - first.getTime()) / (TIME_SECONDS_MILLISECOND);
     374         return mils.intValue();
     375     }
     376 
     377     /**
     378      * 获得两个String型日期之间相差的天数(第2个减第1个)
     379      * <p>
     380      * first, String second
     381      *
     382      * @return int 相差的天数
     383      */
     384     public static int getDaysBetweenDates(String first, String second) {
     385         Date d1 = getFormatDateTime(first, DATE_FORMAT);
     386         Date d2 = getFormatDateTime(second, DATE_FORMAT);
     387 
     388         Long mils = (d2.getTime() - d1.getTime()) / (TIME_DAY_MILLISECOND);
     389 
     390         return mils.intValue();
     391     }
     392 
     393     /**
     394      * @return 获取两个Date之间的天数的列表
     395      */
     396     public static List<Date> getDaysListBetweenDates(Date first, Date second) {
     397         List<Date> dateList = new ArrayList<Date>();
     398         Date d1 = getFormatDateTime(getFormatDate(first), DATE_FORMAT);
     399         Date d2 = getFormatDateTime(getFormatDate(second), DATE_FORMAT);
     400         if (d1.compareTo(d2) > 0) {
     401             return dateList;
     402         }
     403         do {
     404             dateList.add(d1);
     405             d1 = getDateBeforeOrAfter(d1, 1);
     406         } while (d1.compareTo(d2) <= 0);
     407         return dateList;
     408     }
     409 
     410     public static String getDateBeforeDay() {
     411         Calendar cal = Calendar.getInstance();
     412         cal.add(Calendar.DAY_OF_YEAR, -1);
     413         return getFormatDate(cal.getTime(), DATE_FORMAT);
     414     }
     415 
     416     /**
     417      * 得到格式化后的当前系统日期,格式为yyyy-MM-dd,如2009-10-15
     418      *
     419      * @return String 返回格式化后的当前服务器系统日期,格式为yyyy-MM-dd,如2009-10-15
     420      * @see #getFormatDate(java.util.Date)
     421      */
     422     public static String getCurrDateStr() {
     423         return getFormatDate(getCurrDate());
     424     }
     425 
     426     /**
     427      * 得到格式化后的当前系统日期,格式为yyyy-MM,如2009-10
     428      *
     429      * @return String 返回格式化后的当前服务器系统日期,格式为yyyy-MM,如2009-10
     430      * @see #getFormatDate(java.util.Date)
     431      */
     432     public static String getCurrDateMonthStr() {
     433         return getFormatMonth(getCurrDate());
     434     }
     435 
     436 
     437     public static int getCurrDateHour() {
     438         Calendar cal = Calendar.getInstance();
     439 
     440         return cal.get(Calendar.HOUR_OF_DAY);
     441     }
     442 
     443     /**
     444      * 得到格式化后的当前系统时间,格式为yyyy-MM-dd HH:mm:ss,如2009-10-15 15:23:45
     445      *
     446      * @return String 返回格式化后的当前服务器系统时间,格式为yyyy-MM-dd HH:mm:ss,如2009-10-15
     447      * 15:23:45
     448      * @see #getFormatDateTime(java.util.Date)
     449      */
     450     public static String getCurrDateTimeStr() {
     451         return getFormatDateTime(getCurrDate());
     452     }
     453 
     454     /**
     455      * 得到格式化后的当前系统日期,格式为yyyy年MM月dd日,如2009年02月15日
     456      *
     457      * @return String 返回当前服务器系统日期,格式为yyyy年MM月dd日,如2009年02月15日
     458      * @see #getFormatDate(java.util.Date, String)
     459      */
     460     public static String getCurrDateStr_CN() {
     461         return getFormatDate(getCurrDate(), DATE_FORMAT_CN);
     462     }
     463 
     464     /**
     465      * 得到格式化后的当前系统时间,格式为yyyy年MM月dd日 HH:mm:ss,如2009年02月15日 15:23:45
     466      *
     467      * @return String 返回格式化后的当前服务器系统时间,格式为yyyy年MM月dd日 HH:mm:ss,如2009年02月15日
     468      * 15:23:45
     469      * @see #getFormatDateTime(java.util.Date, String)
     470      */
     471     public static String getCurrDateTimeStr_CN() {
     472         return getFormatDateTime(getCurrDate(), TIME_FORMAT_CN);
     473     }
     474 
     475     /**
     476      * 得到系统当前日期的前或者后几天
     477      *
     478      * @param iDate 如果要获得前几天日期,该参数为负数; 如果要获得后几天日期,该参数为正数
     479      * @return Date 返回系统当前日期的前或者后几天
     480      * @see java.util.Calendar#add(int, int)
     481      */
     482     public static Date getDateBeforeOrAfter(int iDate) {
     483         Calendar cal = Calendar.getInstance();
     484         cal.add(Calendar.DAY_OF_MONTH, iDate);
     485         return cal.getTime();
     486     }
     487 
     488     /**
     489      * 得到日期的前或者后几天
     490      *
     491      * @param iDate 如果要获得前几天日期,该参数为负数; 如果要获得后几天日期,该参数为正数
     492      * @return Date 返回参数<code>curDate</code>定义日期的前或者后几天
     493      * @see java.util.Calendar#add(int, int)
     494      */
     495     public static Date getDateBeforeOrAfter(Date curDate, int iDate) {
     496         Calendar cal = Calendar.getInstance();
     497         cal.setTime(curDate);
     498         cal.add(Calendar.DAY_OF_MONTH, iDate);
     499         return cal.getTime();
     500     }
     501 
     502     /**
     503      * 得到格式化后的月份,格式为yyyy-MM,如2009-02
     504      *
     505      * @param currDate 要格式化的日期
     506      * @return String 返回格式化后的月份,格式为yyyy-MM,如2009-02
     507      * @see #getFormatDate(java.util.Date, String)
     508      */
     509     public static String getFormatMonth(java.util.Date currDate) {
     510         return getFormatDate(currDate, MONTH_FORMAT);
     511     }
     512 
     513     /**
     514      * 得到格式化后的日,格式为yyyyMMdd,如20090210
     515      *
     516      * @param currDate 要格式化的日期
     517      * @return String 返回格式化后的日,格式为yyyyMMdd,如20090210
     518      * @see #getFormatDate(java.util.Date, String)
     519      */
     520     public static String getFormatDay(java.util.Date currDate) {
     521         return getFormatDate(currDate, DAY_FORMAT);
     522     }
     523 
     524     public static String getFormatTime(java.util.Date currDate) {
     525         return getFormatDate(currDate, TIME_FORMAT_NUM);
     526     }
     527 
     528     /**
     529      * 得到格式化后的当月第一天,格式为yyyy-MM-dd,如2009-10-01
     530      * <p>
     531      * 要格式化的日期
     532      *
     533      * @return String 返回格式化后的当月第一天,格式为yyyy-MM-dd,如2009-10-01
     534      * @see java.util.Calendar#getMinimum(int)
     535      * @see #getFormatDate(java.util.Date, String)
     536      */
     537     public static String getFirstDayOfMonth() {
     538         Calendar cal = Calendar.getInstance();
     539         int firstDay = cal.getMinimum(Calendar.DAY_OF_MONTH);
     540         cal.set(Calendar.DAY_OF_MONTH, firstDay);
     541         return getFormatDate(cal.getTime(), DATE_FORMAT);
     542     }
     543 
     544     /**
     545      * 得到格式化后的下月第一天,格式为yyyy-MM-dd,如2009-10-01
     546      * <p>
     547      * 要格式化的日期
     548      *
     549      * @return String 返回格式化后的下月第一天,格式为yyyy-MM-dd,如2009-10-01
     550      * @see java.util.Calendar#getMinimum(int)
     551      * @see #getFormatDate(java.util.Date, String)
     552      */
     553     public static String getFirstDayOfNextMonth() {
     554         Calendar cal = Calendar.getInstance();
     555         cal.add(Calendar.MONTH, +1);
     556         int firstDay = cal.getMinimum(Calendar.DAY_OF_MONTH);
     557         cal.set(Calendar.DAY_OF_MONTH, firstDay);
     558         return getFormatDate(cal.getTime(), DATE_FORMAT);
     559     }
     560 
     561     /**
     562      * 得到格式化后的当月第一天,格式为yyyy-MM-dd,如2009-10-01
     563      *
     564      * @param currDate 要格式化的日期
     565      * @return String 返回格式化后的当月第一天,格式为yyyy-MM-dd,如2009-10-01
     566      * @see java.util.Calendar#getMinimum(int)
     567      * @see #getFormatDate(java.util.Date, String)
     568      */
     569     public static String getFirstDayOfMonth(Date currDate) {
     570         Calendar cal = Calendar.getInstance();
     571         cal.setTime(currDate);
     572         int firstDay = cal.getMinimum(Calendar.DAY_OF_MONTH);
     573         cal.set(Calendar.DAY_OF_MONTH, firstDay);
     574         return getFormatDate(cal.getTime(), DATE_FORMAT);
     575     }
     576 
     577     /**
     578      * 得到格式化后的当月最后一天,格式为yyyy-MM-dd,如2009-10-28
     579      *
     580      * @param currDate 要格式化的日期
     581      * @return String 返回格式化后的当月最后一天,格式为yyyy-MM-dd,如2009-10-28
     582      * @see java.util.Calendar#getMinimum(int)
     583      * @see #getFormatDate(java.util.Date, String)
     584      */
     585     public static String getLastDayOfMonth(Date currDate) {
     586         Calendar cal = Calendar.getInstance();
     587         cal.setTime(currDate);
     588         int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
     589         cal.set(Calendar.DAY_OF_MONTH, lastDay);
     590         return getFormatDate(cal.getTime(), DATE_FORMAT);
     591     }
     592 
     593     /**
     594      * 得到格式化后的当月最后一天,格式为yyyy-MM-dd,如2009-10-28
     595      * <p>
     596      * 要格式化的日期
     597      *
     598      * @return String 返回格式化后的当月最后一天,格式为yyyy-MM-dd,如2009-10-28
     599      * @see java.util.Calendar#getMinimum(int)
     600      * @see #getFormatDate(java.util.Date, String)
     601      */
     602     public static String getLastDayOfMonth() {
     603         Calendar cal = Calendar.getInstance();
     604         int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
     605         cal.set(Calendar.DAY_OF_MONTH, lastDay);
     606         return getFormatDate(cal.getTime(), DATE_FORMAT);
     607     }
     608 
     609     /**
     610      * 得到日期的前或者后几小时
     611      *
     612      * @param iHour 如果要获得前几小时日期,该参数为负数; 如果要获得后几小时日期,该参数为正数
     613      * @return Date 返回参数<code>curDate</code>定义日期的前或者后几小时
     614      * @see java.util.Calendar#add(int, int)
     615      */
     616     public static Date getDateBeforeOrAfterHours(Date curDate, int iHour) {
     617         Calendar cal = Calendar.getInstance();
     618         cal.setTime(curDate);
     619         cal.add(Calendar.HOUR_OF_DAY, iHour);
     620         return cal.getTime();
     621     }
     622 
     623     /**
     624      * 判断日期是否在当前周内
     625      *
     626      * @param curDate
     627      * @param compareDate
     628      * @return
     629      */
     630     public static boolean isSameWeek(Date curDate, Date compareDate) {
     631         if (curDate == null || compareDate == null) {
     632             return false;
     633         }
     634 
     635         Calendar calSun = Calendar.getInstance();
     636         calSun.setTime(getFormatDateToDate(curDate));
     637         calSun.set(Calendar.DAY_OF_WEEK, 1);
     638 
     639         Calendar calNext = Calendar.getInstance();
     640         calNext.setTime(calSun.getTime());
     641         calNext.add(Calendar.DATE, 7);
     642 
     643         Calendar calComp = Calendar.getInstance();
     644         calComp.setTime(compareDate);
     645         if (calComp.after(calSun) && calComp.before(calNext)) {
     646             return true;
     647         } else {
     648             return false;
     649         }
     650     }
     651 
     652     public static boolean isSameDay(Date currentDate, Date compareDate) {
     653         if (currentDate == null || compareDate == null) {
     654             return false;
     655         }
     656         String current = getFormatDate(currentDate);
     657         String compare = getFormatDate(compareDate);
     658         if (current.equals(compare)) {
     659             return true;
     660         }
     661         return false;
     662     }
     663 
     664     /**
     665      * 时间查询时,结束时间的 23:59:59
     666      */
     667     public static String addDateEndfix(String datestring) {
     668         if ((datestring == null) || datestring.equals("")) {
     669             return null;
     670         }
     671         return datestring + " 23:59:59";
     672     }
     673 
     674     /**
     675      * 返回格式化的日期
     676      * <p>
     677      * 格式"yyyy-MM-dd 23:59:59";
     678      *
     679      * @return
     680      */
     681     public static Date getFormatDateEndfix(String dateStr) {
     682         dateStr = addDateEndfix(dateStr);
     683         return getFormatDateTime(dateStr);
     684     }
     685 
     686     /**
     687      * 返回格式化的日期
     688      *
     689      * @param datePre 格式"yyyy-MM-dd HH:mm:ss";
     690      * @return
     691      */
     692     public static Date formatEndTime(String datePre) {
     693         if (datePre == null)
     694             return null;
     695         String dateStr = addDateEndfix(datePre);
     696         return getFormatDateTime(dateStr);
     697     }
     698 
     699     // date1加上compday天数以后的日期与当前时间比较,如果大于当前时间返回true,否则false
     700     public static Boolean compareDay(Date date1, int compday) {
     701         if (date1 == null)
     702             return false;
     703         Date dateComp = getDateBeforeOrAfter(date1, compday);
     704         Date nowdate = new Date();
     705         if (dateComp.after(nowdate))
     706             return true;
     707         else
     708             return false;
     709     }
     710 
     711     /**
     712      * 进行时段格式转换,对于输入的48位的01串,将进行如下操作: <li>
     713      * 1.先将输入中每个0变成两个0,每个1变成2个1,形成一个96位的二进制串。</li> <li>
     714      * 2.将上述的96位的二进制串分成3组,每组32位。</li> <li>3.将每个32位的二进制串转换成一个8位的16进制串。</li> <li>
     715      * 4.将3个8位的16进制串合并成一个串,中间以","分割。</li>
     716      *
     717      * @param timespan 一个48位的二进制串,如:
     718      *                 "011111111011111111111111111111111111111111111110"
     719      * @return 一个16进制串,每位间以","分割。如:"3fffcfff,ffffffff,fffffffc"
     720      */
     721     public static String convertBinaryTime2Hex(String timespan) {
     722         if (timespan == null || timespan.equals("")) {
     723             return "";
     724         }
     725 
     726         String ret = "";
     727         String tmp = "";
     728         for (int i = 0; i < timespan.length(); i++) {
     729             tmp += timespan.charAt(i);
     730             tmp += timespan.charAt(i);
     731             // tmp += i;
     732             if ((i + 1) % 16 == 0) {
     733                 if (!ret.equals("")) {
     734                     ret += ",";
     735                 }
     736                 Long t = Long.parseLong(tmp, 2);
     737                 String hexStr = Long.toHexString(t);
     738                 if (hexStr.length() < 8) {
     739                     int length = hexStr.length();
     740                     for (int n = 0; n < 8 - length; n++) {
     741                         hexStr = "0" + hexStr;
     742                     }
     743                 }
     744 
     745                 ret += hexStr;
     746                 tmp = "";
     747             }
     748         }
     749 
     750         return ret;
     751     }
     752 
     753     /**
     754      * 进行时段格式转换,将输入的26位的2进制串转换成48位的二进制串。
     755      *
     756      * @param timespan 一个16进制串,每位间以","分割。如:"3fffcfff,ffffffff,fffffffc"
     757      * @return 一个48位的二进制串,如:"011111111011111111111111111111111111111111111110"
     758      */
     759     public static String convertHexTime2Binary(String timespan) {
     760         if (timespan == null || timespan.equals("")) {
     761             return "";
     762         }
     763 
     764         String tmp = "";
     765         String ret = "";
     766         String[] strArr = timespan.split(",");
     767         for (int i = 0; i < strArr.length; i++) {
     768             String binStr = Long.toBinaryString(Long.parseLong(strArr[i], 16));
     769             if (binStr.length() < 32) {
     770                 int length = binStr.length();
     771                 for (int n = 0; n < 32 - length; n++) {
     772                     binStr = "0" + binStr;
     773                 }
     774             }
     775             tmp += binStr;
     776         }
     777 
     778         for (int i = 0; i < 48; i++) {
     779             ret += tmp.charAt(i * 2);
     780         }
     781 
     782         return ret;
     783     }
     784 
     785     /**
     786      * 进行时段格式转换,将输入的32位的10进制串转换成48位的二进制串。
     787      *
     788      * @param timespan 一个16进制串,每位间以","分割。如:"1234567890,1234567890,1234567890c"
     789      * @return 一个48位的二进制串,如:"011111111011111111111111111111111111111111111110"
     790      */
     791     public static String convertDecTime2Binary(String timespan) {
     792         if (timespan == null || timespan.equals("")) {
     793             return "";
     794         }
     795 
     796         String tmp = "";
     797         String ret = "";
     798         String[] strArr = timespan.split(",");
     799         for (int i = 0; i < strArr.length; i++) {
     800             String binStr = Long.toBinaryString(Long.parseLong(strArr[i], 10));
     801             if (binStr.length() < 32) {
     802                 int length = binStr.length();
     803                 for (int n = 0; n < 32 - length; n++) {
     804                     binStr = "0" + binStr;
     805                 }
     806             }
     807             tmp += binStr;
     808         }
     809 
     810         for (int i = 0; i < 48; i++) {
     811             ret += tmp.charAt(i * 2);
     812         }
     813 
     814         return ret;
     815     }
     816 
     817     /**
     818      * 进行时段格式转换,对于输入的48位的01串,将进行如下操作: <li>
     819      * 1.先将输入中每个0变成两个0,每个1变成2个1,形成一个96位的二进制串。</li> <li>
     820      * 2.将上述的96位的二进制串分成3组,每组32位。</li> <li>3.将每个32位的二进制串转换成一个10位的10进制串。</li> <li>
     821      * 4.将3个8位的16进制串合并成一个串,中间以","分割。</li>
     822      *
     823      * @param timespan 一个48位的二进制串,如:
     824      *                 "011111111011111111111111111111111111111111111110"
     825      * @return 一个16进制串,每位间以","分割。如:"1234567890,1234567890,1234567890"
     826      */
     827     public static String convertBinaryTime2Dec(String timespan) {
     828         if (timespan == null || timespan.equals("")) {
     829             return "";
     830         }
     831 
     832         String ret = "";
     833         String tmp = "";
     834         for (int i = 0; i < timespan.length(); i++) {
     835             tmp += timespan.charAt(i);
     836             tmp += timespan.charAt(i);
     837             // tmp += i;
     838             if ((i + 1) % 16 == 0) {
     839                 if (!ret.equals("")) {
     840                     ret += ",";
     841                 }
     842                 Long t = Long.parseLong(tmp, 2);
     843                 String decStr = Long.toString(t);
     844                 if (decStr.length() < 10) {
     845                     int length = decStr.length();
     846                     for (int n = 0; n < 10 - length; n++) {
     847                         decStr = "0" + decStr;
     848                     }
     849                 }
     850 
     851                 ret += decStr;
     852                 tmp = "";
     853             }
     854         }
     855 
     856         return ret;
     857     }
     858 
     859     public static String secondToRender(int second) {
     860         if (second <= 0) {
     861             return "00:00";
     862         }
     863         int min = second / 60;
     864         int sec = second % 60;
     865         StringBuilder cs = new StringBuilder(5);
     866         if (min < 10) {
     867             cs.append("0");
     868         }
     869         cs.append(min);
     870         cs.append(":");
     871         if (sec < 10) {
     872             cs.append("0");
     873         }
     874         cs.append(sec);
     875         return cs.toString();
     876     }
     877 
     878     /**
     879      * 计算指定日期+addMonth月+15号 返回格式"2009-10-15"
     880      *
     881      * @param date
     882      * @param addMonth
     883      * @param monthDay
     884      * @return
     885      */
     886     public static String genericSpecdate(Date date, int addMonth, int monthDay) {
     887         Calendar cal = Calendar.getInstance();
     888         cal.setTime(date);
     889         cal.add(Calendar.MONTH, addMonth);
     890         cal.set(Calendar.DAY_OF_MONTH, monthDay);
     891         return getFormatDate(cal.getTime(), DATE_FORMAT);
     892     }
     893 
     894     public static String monthOpertation(Date date, int addMonth) {
     895         Calendar cal = Calendar.getInstance();
     896         cal.setTime(date);
     897         cal.add(Calendar.MONTH, addMonth);
     898         return getFormatDate(cal.getTime(), MONTH_FORMAT);
     899     }
     900 
     901     /**
     902      * 获得以今天为单位若干天以前或以后的日期的标准格式"Wed Feb 20 00:00:00 CST 2009",是0点0分0秒。
     903      *
     904      * @param idx
     905      * @return
     906      */
     907     public static Date getDateBeforeOrAfterV2(int idx) {
     908         return getDateBeforeOrAfter(getFormatDateToDate(getCurrDate()), idx);
     909     }
     910 
     911     /**
     912      * 获得给定时间若干秒以前或以后的日期的标准格式。
     913      *
     914      * @param curDate
     915      * @param seconds
     916      * @return curDate
     917      */
     918     public static Date getSpecifiedDateTimeBySeconds(Date curDate, int seconds) {
     919         long time = (curDate.getTime() / 1000) + seconds;
     920         curDate.setTime(time * 1000);
     921         return curDate;
     922     }
     923 
     924     /**
     925      * 获得给定日期当天23点59分59秒的标准格式。
     926      *
     927      * @param curDate
     928      * @return curDate
     929      */
     930     public static Date getSpecifiedDateTime_235959(Date curDate) {
     931         return getSpecifiedDateTimeBySeconds(getFormatDateToDate(curDate), 24 * 60 * 60 - 1);
     932     }
     933 
     934     public static String getSpecifiedDateTime_month(Date curDate) {
     935         return getFormatDateTime(curDate, "MM.dd");
     936     }
     937 
     938     /**
     939      * 获得年份
     940      *
     941      * @param date
     942      * @return
     943      */
     944     public static int getYear(Date date) {
     945         Calendar cal = Calendar.getInstance();
     946         cal.setTime(date);
     947         return cal.get(Calendar.YEAR);
     948     }
     949 
     950     /**
     951      * 获得月份
     952      *
     953      * @param date
     954      * @return
     955      */
     956     public static int getMonth(Date date) {
     957         Calendar cal = Calendar.getInstance();
     958         cal.setTime(date);
     959         return cal.get(Calendar.MONTH);
     960     }
     961 
     962     /**
     963      * 获得天
     964      *
     965      * @param date
     966      * @return
     967      */
     968     public static int getDay(Date date) {
     969         Calendar cal = Calendar.getInstance();
     970         cal.setTime(date);
     971         return cal.get(Calendar.DAY_OF_MONTH);
     972     }
     973 
     974     /**
     975      * 获得hour
     976      *
     977      * @param date
     978      * @return
     979      */
     980     public static int getHour(Date date) {
     981         Calendar cal = Calendar.getInstance();
     982         cal.setTime(date);
     983         return cal.get(Calendar.HOUR_OF_DAY);
     984     }
     985 
     986     /**
     987      * Feed 及 Comment 中间 取时间差
     988      */
     989     private final static long SECOND = 60l;
     990     private final static long MINUTE = 60l * 60l;
     991     private final static long HOUR = 24l * 60l * 60l;
     992 
     993     public static String minusTime(String pubTime) {
     994         Date pubDate = DateTimeUtil.getFormatDateTime(pubTime);
     995         return minusTime(pubDate);
     996     }
     997 
     998     public static String minusDays(Date pubDate) {
     999         pubDate = getFormatDateToDate(pubDate);
    1000         Date currentDate = getFormatDateToDate(new Date());
    1001         long pL = pubDate.getTime();
    1002         long cL = currentDate.getTime();
    1003 
    1004         long pass = (cL - pL);
    1005         pass = pass / 1000;
    1006         long result = pass / HOUR;
    1007 //        // 考虑凌晨的问题
    1008 //        if ( result ==0 && pL < getBeforeDawnOfYesterday().getTime()) {
    1009 //            result++;
    1010 //        }
    1011         if (result == 0) {
    1012             return "今天";
    1013         } else {
    1014             return result + " 天前";
    1015         }
    1016 
    1017     }
    1018 
    1019     /**
    1020      * 昨天凌晨的时间
    1021      *
    1022      * @return
    1023      */
    1024     public static Date getBeforeDawnOfYesterday() {
    1025         Calendar calendar = Calendar.getInstance();
    1026         calendar.add(Calendar.DAY_OF_YEAR, -1);
    1027         calendar.set(Calendar.HOUR_OF_DAY, 23);
    1028         calendar.set(Calendar.MINUTE, 59);
    1029         calendar.set(Calendar.SECOND, 59);
    1030         calendar.set(Calendar.MILLISECOND, 0);
    1031         System.out.println(getFormatDateTime(calendar.getTime()));
    1032         return calendar.getTime();
    1033     }
    1034 
    1035     // 记者改写时间.
    1036 //    private static int stepTime = 1000 * 60 * 1;
    1037 //
    1038 //    private static int oneMinute = 1000 * 60;
    1039 
    1040     public static Date setHour(Date pubTime, int hour) {
    1041 
    1042         Calendar calendar = Calendar.getInstance();
    1043         calendar.setTime(pubTime);
    1044         calendar.set(Calendar.HOUR_OF_DAY, hour);
    1045 
    1046         return calendar.getTime();
    1047     }
    1048 
    1049     public static String minusTime(Date pubDate) {
    1050         String result = null;
    1051         Date currentDate = new Date();
    1052         long pL = pubDate.getTime();
    1053         long cL = currentDate.getTime();
    1054         long pass = (cL - pL);
    1055         if (pass < 0) {
    1056             result = "未知";
    1057         } else {
    1058             pass = pass / 1000;
    1059             if (pass < SECOND) {
    1060                 result = "刚刚";
    1061             } else if (pass < MINUTE) {
    1062                 result = (pass / SECOND) + "分钟前";
    1063             } else if (pass < HOUR) {
    1064                 String pFuix = DateTimeUtil.isSameDay(currentDate, pubDate) ? "今天" : "昨天";
    1065                 String pubFormat = DateTimeUtil.getFormatDateTime(pubDate);
    1066                 try {
    1067                     pubFormat = pubFormat.substring((pubFormat.indexOf(" ") + 1), pubFormat.lastIndexOf(":"));
    1068                     result = pFuix + pubFormat;
    1069                 } catch (Exception e) {
    1070                     result = pass / MINUTE + "小时前";
    1071                 }
    1072             } else {
    1073                 result = (pass / HOUR) + "天前";
    1074             }
    1075         }
    1076         return result;
    1077     }
    1078 
    1079     /**
    1080      * 获得N天前/后 的开始
    1081      *
    1082      * @return
    1083      */
    1084     public static long getDayBeginTimestamp(int days) {
    1085         Calendar cal = Calendar.getInstance();
    1086         cal.add(Calendar.DAY_OF_MONTH, days);
    1087         cal.set(Calendar.HOUR_OF_DAY, 0);
    1088         cal.set(Calendar.SECOND, 0);
    1089         cal.set(Calendar.MINUTE, 0);
    1090         cal.set(Calendar.MILLISECOND, 001);
    1091         return cal.getTimeInMillis();
    1092     }
    1093 
    1094     /**
    1095      * 获得N天前/后 的结束
    1096      *
    1097      * @return
    1098      */
    1099     public static long getDayEndTimestamp(int days) {
    1100         Calendar cal = Calendar.getInstance();
    1101         cal.add(Calendar.DAY_OF_MONTH, days);
    1102         cal.set(Calendar.HOUR_OF_DAY, 23);
    1103         cal.set(Calendar.SECOND, 59);
    1104         cal.set(Calendar.MINUTE, 59);
    1105         cal.set(Calendar.MILLISECOND, 59);
    1106         return cal.getTimeInMillis();
    1107     }
    1108 
    1109     /**
    1110      * 获得一天的开始
    1111      *
    1112      * @return
    1113      */
    1114     public static long getDayBeginTimestamp() {
    1115         Calendar cal = Calendar.getInstance();
    1116         cal.set(Calendar.HOUR_OF_DAY, 0);
    1117         cal.set(Calendar.SECOND, 0);
    1118         cal.set(Calendar.MINUTE, 0);
    1119         cal.set(Calendar.MILLISECOND, 001);
    1120         return cal.getTimeInMillis();
    1121     }
    1122 
    1123     /**
    1124      * 获取一天的结束
    1125      *
    1126      * @return
    1127      */
    1128     public static long getDayEndTimestamp() {
    1129         Calendar cal = Calendar.getInstance();
    1130         cal.set(Calendar.HOUR_OF_DAY, 23);
    1131         cal.set(Calendar.SECOND, 59);
    1132         cal.set(Calendar.MINUTE, 59);
    1133         cal.set(Calendar.MILLISECOND, 59);
    1134         return cal.getTimeInMillis();
    1135     }
    1136 
    1137     /**
    1138      * 获得n年前的今天
    1139      *
    1140      * @return
    1141      */
    1142     public static long getBirthdayTimestamp(int years) {
    1143         Calendar cal = Calendar.getInstance();
    1144         cal.add(Calendar.YEAR, -years);
    1145         cal.set(Calendar.SECOND, 0);
    1146         cal.set(Calendar.MINUTE, 0);
    1147         cal.set(Calendar.MILLISECOND, 001);
    1148         return cal.getTimeInMillis();
    1149     }
    1150     
    1151   //获得当前时间是星期几  
    1152       public static String getWeekOfDate() {
    1153           String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
    1154           Calendar cal = Calendar.getInstance();
    1155           // cal.setTime(dt);
    1156           int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
    1157           if (w < 0)
    1158               w = 0;
    1159           return weekDays[w];
    1160       }
    1161       
    1162       
    1163       //比较两个时间的大小
    1164        public static int compareTwoDate(String DATE1, String DATE2) {
    1165          DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    1166          try {
    1167              Date dt1 = df.parse(DATE1);//时间1
    1168              Date dt2 = df.parse(DATE2);//时间2
    1169              if (dt1.getTime() > dt2.getTime()) {
    1170                  System.out.println(dt1+"时间1在时间2"+dt2+"之后");
    1171                  return 1;
    1172              } else if (dt1.getTime() < dt2.getTime()) {
    1173                  System.out.println(dt1+"时间1在时间2"+dt2+"之前");
    1174                  return -1;
    1175              } else {
    1176                  return 0;
    1177              }
    1178          } catch (Exception exception) {
    1179              exception.printStackTrace();
    1180          }
    1181          return 0;
    1182      }
    1183        
    1184        
    1185       public static long getDatePoor(Date endDate, Date nowDate) {
    1186            
    1187           long nd = 1000 * 24 * 60 * 60;
    1188           long nh = 1000 * 60 * 60;
    1189           long nm = 1000 * 60;
    1190           // long ns = 1000;
    1191           // 获得两个时间的毫秒时间差异
    1192           long diff = endDate.getTime() - nowDate.getTime();
    1193           // 计算差多少天
    1194           long day = diff / nd;
    1195           // 计算差多少小时
    1196           long hour = diff % nd / nh;
    1197           // 计算差多少分钟
    1198           long min = diff % nd % nh / nm;
    1199           // 计算差多少秒//输出结果
    1200           // long sec = diff % nd % nh % nm / ns;
    1201 //          return day + "天" + hour + "小时" + min + "分钟";
    1202           return min;
    1203       }
    1204       
    1205       public static long dateDiff(String startTime, String endTime,  
    1206             String format, String str) {  
    1207         // 按照传入的格式生成一个simpledateformate对象    
    1208         SimpleDateFormat sd = new SimpleDateFormat(format);  
    1209         long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数    
    1210         long nh = 1000 * 60 * 60;// 一小时的毫秒数    
    1211         long nm = 1000 * 60;// 一分钟的毫秒数    
    1212         long ns = 1000;// 一秒钟的毫秒数    
    1213         long diff;  
    1214         long day = 0;  
    1215         long hour = 0;  
    1216         long min = 0;  
    1217         long minTime = 0;
    1218         long sec = 0;  
    1219         // 获得两个时间的毫秒时间差异    
    1220         try {  
    1221             diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();  
    1222             day = diff / nd;// 计算差多少天    
    1223             hour = diff % nd / nh + day * 24;// 计算差多少小时    
    1224             min = diff % nd % nh / nm + day * 24 * 60;// 计算差多少分钟    
    1225             sec = diff % nd % nh % nm / ns;// 计算差多少秒    
    1226             minTime=day * 24 * 60+(hour - day * 24)*60+(min - day * 24 * 60);
    1227             // 输出结果    
    1228 //            System.out.println("时间相差:" + day + "天" + (hour - day * 24) + "小时"  
    1229 //                    + (min - day * 24 * 60) + "分钟" + sec + "秒。");  
    1230 //            System.out.println("hour=" + hour + ",min=" + min);  
    1231             if (str.equalsIgnoreCase("h")) {  
    1232                 return hour;  
    1233             } else if(str.equalsIgnoreCase("m")) {  
    1234                 return minTime;
    1235             }  else {  
    1236               return min;  
    1237           }  
    1238         } catch (ParseException e) {  
    1239             e.printStackTrace();  
    1240         }  
    1241         if (str.equalsIgnoreCase("h")) {  
    1242             return hour;  
    1243         } else {  
    1244             return min;  
    1245         }  
    1246     }  
    1247        
    1248     public static void main(String[] args) {
    1249         //获得yyyyMMdd格式的时间
    1250         System.out.println(getFormatDay(new java.util.Date()));
    1251         
    1252         
    1253         
    1254         System.out.println(getFormatDateTime(new java.util.Date()));
    1255         
    1256         //获得yyyy-MM-dd"格式的目前系统时间
    1257         System.out.println(getFormatDate(new java.util.Date()));
    1258         
    1259         //获得两个日期之间的天数差 
    1260         System.out.println(getDaysBetweenDates("2017-04-22",getFormatDate(new java.util.Date())));
    1261         
    1262         
    1263 //        String dateTime=getFormatDate(new java.util.Date());
    1264 //        
    1265 //        int mun=getDaysBetweenDates(dateTime,"2017-03-29");
    1266 //        
    1267 //        if(mun>90){
    1268 //            System.out.println("没有过过期"+mun); 
    1269 //        }else {
    1270 //            System.out.println("过期"+mun);
    1271 //        }
    1272         
    1273         //第二个减去第一个
    1274 //        System.out.println(getDaysBetweenDates(getFormatDate(new java.util.Date()),"2019-02-22"));
    1275         
    1276         System.out.println(getFormatDay(getDateBeforeOrAfter(-60)));
    1277         
    1278 //        long i=getDatePoor(getFormatDateTime("2017-07-14 08:50:25"), new java.util.Date());//后面-前面
    1279 //        System.out.println(getFormatDateTime("2017-07-14 08:50:25")+"====="+i);
    1280         
    1281         long h=dateDiff("2017-07-13 08:50:25",getFormatDateTime( new java.util.Date()), TIME_FORMAT, "m");
    1282         System.out.println(h);   
    1283 }
    1284 }
  • 相关阅读:
    (原创)代码分析-DataGrid实现增删(带提示)改和分页
    (原创)webform页面间传值的特殊方法
    (原创)如何把数据放到web不能访问的文件夹中并给用户下载?
    MediaWiki如何重置用户密码
    伟大的程序员是怎样炼成的?
    Java多线程(一)、多线程的基本概念和使用
    Android杂谈Activity、Window、View的关系
    Android LayoutInflater学习
    10条不可不知的手机礼仪 看看你犯过哪几项?
    Java支持的字符集[Code]
  • 原文地址:https://www.cnblogs.com/wyf-love-dch/p/7483065.html
Copyright © 2020-2023  润新知