靜以修心

          JAVA 簡(jiǎn)單的日期封裝類

          本來(lái)以為有了date4j就萬(wàn)事無(wú)休了,結(jié)果在工作的時(shí)候發(fā)覺(jué)有不少腳步僅僅需要兩三個(gè)簡(jiǎn)單的class執(zhí)行一下就可以完成任務(wù)了。也就是說(shuō)即使是date4j,相對(duì)于這兩三個(gè)甚至是一個(gè)class來(lái)說(shuō)還是過(guò)于臃腫了。于是乎自己寫(xiě)了個(gè)簡(jiǎn)單的日期封裝類。
          主要功能是
          1.獲取當(dāng)前時(shí)間
          2.獲取當(dāng)前年,月,日,時(shí),分,秒
          3.獲取指定日期的年,月,日,時(shí),分,秒
          4.獲取兩個(gè)日期的時(shí)間差(包括年月日時(shí)分秒)
          5.將字符竄類型轉(zhuǎn)成java.util.date類型
          6.指定日期添加時(shí)間

          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 閱讀(1937) 評(píng)論(1)  編輯  收藏 所屬分類: JAVA

          評(píng)論

          # re: JAVA 簡(jiǎn)單的日期封裝類 2012-04-28 09:12 jackzlz

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


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


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

          導(dǎo)航

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

          統(tǒng)計(jì)

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 闻喜县| 都昌县| 邢台县| 遵化市| 娄烦县| 金平| 曲沃县| 新竹县| 乌拉特中旗| 垣曲县| 额尔古纳市| 福安市| 张家界市| 石泉县| 永川市| 延安市| 富平县| 尼勒克县| 广灵县| 桐梓县| 那坡县| 邻水| 元氏县| 井研县| 平昌县| 榆社县| 临泽县| 云霄县| 白城市| 肥乡县| 金阳县| 左云县| 平武县| 格尔木市| 林口县| 龙州县| 资阳市| 兴海县| 宜春市| 连云港市| 蓬溪县|