靜以修心

          JAVA 簡單的日期封裝類

          本來以為有了date4j就萬事無休了,結果在工作的時候發覺有不少腳步僅僅需要兩三個簡單的class執行一下就可以完成任務了。也就是說即使是date4j,相對于這兩三個甚至是一個class來說還是過于臃腫了。于是乎自己寫了個簡單的日期封裝類。
          主要功能是
          1.獲取當前時間
          2.獲取當前年,月,日,時,分,秒
          3.獲取指定日期的年,月,日,時,分,秒
          4.獲取兩個日期的時間差(包括年月日時分秒)
          5.將字符竄類型轉成java.util.date類型
          6.指定日期添加時間

          package com.kohri.date;

          import java.text.DateFormat;
          import java.text.ParseException;
          import java.text.SimpleDateFormat;
          import java.util.Calendar;
          import java.util.Date;
          import java.util.GregorianCalendar;

          /**
           * @descriped a simple class for date
           * 
          @author Kohri
           * @date 2012/4/22
           * 
          @version 1.0
           
          */
          public class SimpleDate {

              private static String defaultFormat = "yyyy-MM-dd HH:mm:ss";
              private static SimpleDateFormat sf = null ;
              private static Calendar cal = Calendar.getInstance();
              private static Date date = null;

              /**
               * get current date time (default 'yyyy-MM-dd HH:mm:ss')
               * 
          @return string
               
          */
              public static String getDateNow() {
                  sf = new SimpleDateFormat(defaultFormat);
                  String currentTime = "";
                  currentTime = sf.format(new Date());
                  return currentTime;
              }
              
              /**
               * get year (default now) 
               * 
          @return int 
               
          */
              public static int getYear(){
                  int currentYear = cal.get(Calendar.YEAR);
                  return currentYear;
              }
              
              /**
               * get mon (default now) 
               * 
          @return int 
               
          */
              public static int getMonth(){
                  int currentMonth = cal.get(Calendar.MONTH) + 1;
                  return currentMonth;
              }
              
              /**
               * get day of month (default now)
               * 
          @return int 
               
          */
              public static int getDay(){
                  int currentDayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
                  return currentDayOfMonth;
              }
              
              /**
               * get hours (default now)
               * 
          @return int 
               
          */
              public static int getHours(){
                  int currentHours = cal.get(Calendar.HOUR_OF_DAY);
                  return currentHours;
              }
              
              /**
               * get  minutes (default now)
               * 
          @return int 
               
          */
              public static int getMinutes(){
                  int currentMinute = cal.get(Calendar.MINUTE);
                  return currentMinute;
              }

              /**
               * get seconds (default now)
               * 
          @return int 
               
          */
              public static int getSeconds(){
                  int currentSecond = cal.get(Calendar.SECOND);
                  return currentSecond;
              }
              
              /**
               * string change to date
               * 
          @param strDate
               * 
          @param dateFormat
               * 
          @return date
               
          */
              public static Date toDate(String strDate, String dateFormat){
                  if(strDate == null || strDate.length() == 0){
                      return null;
                  }
                  Date date = null;
                  DateFormat df = new SimpleDateFormat(dateFormat);
                  try {
                      date = df.parse(strDate);
                  } catch (ParseException e) {
                      e.printStackTrace();
                  }
                  return date;
              }
              
              /**
               * Returns this Calendar's time value in milliseconds
               * 
          @param p_date
               * 
          @return long
               
          */
              public static long getMillisOfDate(Date date) {
                     cal.setTime(date);
                     return cal.getTimeInMillis();
              }
              
              
              
              /**
               * compare two date 
               * return the greater date 
               * if equals return null
               * 
          @param strStartDate
               * 
          @param strEndDate
               * 
          @return date 
               
          */
              public static Date getGreaterDate(String strStartDate, String strEndDate){
                  Date date = null;
                  Date startDate = toDate(strStartDate, "yyyy-MM-dd");
                  Date endDate = toDate(strEndDate, "yyyy-MM-dd");
                  long startTime = getMillisOfDate(startDate);
                  long endTime = getMillisOfDate(endDate);
                  if((startTime - endTime) > 0){
                      return startDate;
                  }else if((endTime - startTime) > 0){
                      return endDate;
                  }
                  return date;
              }
              
              /**
               * get days between 2 different date
               * 
          @param strStartDate less date (yyyy-MM-dd)
               * 
          @param strEndDate greater date (yyyy-MM-dd)
               * 
          @return long
               
          */
              public static long getDaysOftwoDiffDate(String strStartDate, String strEndDate){
                     Date startDate = toDate(strStartDate, "yyyy-MM-dd");
                     Date endDate = toDate(strEndDate, "yyyy-MM-dd");
                     long startTime = getMillisOfDate(startDate);
                     long endTime = getMillisOfDate(endDate);
                     long betweenDays = (long) ((endTime - startTime) / ( 1000 * 60 * 60 * 24 ));
                     return betweenDays;
              }
              
              /**
               * get weeks between 2 different date
               * 
          @param strStartDate less date (yyyy-MM-dd)
               * 
          @param strEndDate greater date (yyyy-MM-dd)
               * 
          @return long
               
          */
              public static long getWeeksOfTwoDiffDate(String strStartDate, String strEndDate){
                  return getDaysOftwoDiffDate(strStartDate, strEndDate) / 7;
              }
              
              /**
               * get months between 2 different date
               * 
          @param strStartDate less date (yyyy-MM-dd)
               * 
          @param strEndDate greater date (yyyy-MM-dd)
               * 
          @return long
               
          */
              public static long getMonthsOfTwoDiffDate(String strStartDate, String strEndDate){
                  return getDaysOftwoDiffDate(strStartDate, strEndDate) / 30;
              }
              
              /**
               * get years between 2 different date
               * 
          @param strStartDate less date (yyyy-MM-dd)
               * 
          @param strEndDate greater date (yyyy-MM-dd)
               * 
          @return long
               
          */
              public static long getYearsOfTwoDiffDate(String strStartDate, String strEndDate){
                  return getDaysOftwoDiffDate(strStartDate, strEndDate) / 365;
              }
              
              /**
               * add date
               * 
          @param date
               * 
          @param count 
               * 
          @param field Calendar.YEAR(MONTH..)
               * 
          @param format DateFormat(yyyy-MM-dd)
               * 
          @return string
               
          */
              public static String addDate(Date date,int count,int field,String format){
                     cal.setTime(date);
                     int year = getYear();
                     int month = getMonth() - 1;
                     int day = getDay();
                     int hours = getHours();
                     int minutes = getMinutes();
                     int seconds = getSeconds();
                     Calendar calendar = new GregorianCalendar(year, month, day, hours, minutes, seconds);
                     calendar.add(field,count);
                     DateFormat df = new SimpleDateFormat(format);
                     String tmpDate = df.format(calendar.getTime());
                     if(date == null){
          cal.setTime(new Date());
          }else{
          cal.setTime(date);
          }
                     return tmpDate;
              }
              
              /**
               * calendar settime
               * 
          @param date
               
          */
              private static void setCalTime(Date date){
                  if(date != null){
                      cal.setTime(date);
                  }
              }
              
              //setter getter

              public static String getDefaultFormat() {
                  return defaultFormat;
              }

              public static void setDefaultFormat(String defaultFormat) {
                  SimpleDate.defaultFormat = defaultFormat;
              }

              public static Date getDate() {
                  return date;
              }

              public static void setDate(Date date) {
                  SimpleDate.date = date;
                  setCalTime(date);
              }
              

          }

          posted on 2012-04-25 22:36 kohri 閱讀(1936) 評論(1)  編輯  收藏 所屬分類: JAVA

          評論

          # re: JAVA 簡單的日期封裝類 2012-04-28 09:12 jackzlz

          可以考慮再加上獲得當月的第一天和當月的最后一天  回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          <2012年4月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          統計

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 高州市| 沈阳市| 边坝县| 高台县| 庆云县| 莎车县| 涿鹿县| 祁东县| 平果县| 大兴区| 肃宁县| 奉新县| 德庆县| 仁化县| 巴青县| 西昌市| 大关县| 茂名市| 商河县| 新建县| 岢岚县| 嘉义县| 曲水县| 遂川县| 石林| 华亭县| 绍兴县| 西城区| 通许县| 浮山县| 炉霍县| 青冈县| 筠连县| 太保市| 太仓市| 石首市| 永顺县| 石棉县| 建水县| 瓮安县| 册亨县|