靜以修心

          JAVA 簡單的日期封裝類

          本來以為有了date4j就萬事無休了,結(jié)果在工作的時候發(fā)覺有不少腳步僅僅需要兩三個簡單的class執(zhí)行一下就可以完成任務(wù)了。也就是說即使是date4j,相對于這兩三個甚至是一個class來說還是過于臃腫了。于是乎自己寫了個簡單的日期封裝類。
          主要功能是
          1.獲取當(dāng)前時間
          2.獲取當(dāng)前年,月,日,時,分,秒
          3.獲取指定日期的年,月,日,時,分,秒
          4.獲取兩個日期的時間差(包括年月日時分秒)
          5.將字符竄類型轉(zhuǎn)成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

          可以考慮再加上獲得當(dāng)月的第一天和當(dāng)月的最后一天  回復(fù)  更多評論   


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


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

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

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 竹溪县| 望都县| 泾源县| 淳化县| 罗山县| 宜君县| 兴国县| 公安县| 柳林县| 呼玛县| 宝山区| 贵溪市| 连城县| 涡阳县| 湖北省| 靖西县| 鹤壁市| 龙泉市| 杭锦后旗| 商都县| 乌拉特中旗| 六盘水市| 江北区| 福清市| 安国市| 美姑县| 集贤县| 永川市| 景洪市| 阿巴嘎旗| 大方县| 运城市| 郑州市| 陇西县| 灯塔市| 宜丰县| 竹山县| 九江市| 会理县| 宜宾县| 永州市|