Java愛好者

          一個堅定的Java愛好者,歡迎和我討論
          隨筆 - 7, 文章 - 8, 評論 - 6, 引用 - 0
          數據加載中……

          我也貢獻一個去年做項目自己寫的一個日期類吧,供大家參考

          /*
          * Created on 2005-7-29
          */
          package cn.com.worksoft.util.datetime;

          import java.sql.Timestamp;
          import java.text.SimpleDateFormat;
          import java.util.Calendar;
          import java.util.Date;

          import cn.com.worksoft.util.basetype.CastType;

          /**
          * @author XING 日期時間工具類。
          */
          public final class TDateTime implements java.io.Serializable, Comparable {
          private final static int[] full_day_month = new int[] { 1, 3, 5, 7, 8, 10,
          12 };

          /**
          * The year of the gregorianCutover, with 0 representing 1 BC, -1
          * representing 2 BC, etc.
          */
          private static transient int gregorianCutoverYear = 1582;

          private final static int[] normal_day_month = new int[] { 4, 6, 9, 11};

          public final static int QUARTER_OF_FIRST = 1;

          public final static int QUARTER_OF_FOURTH = 4;

          public final static int QUARTER_OF_SECOND = 2;

          public final static int QUARTER_OF_THIRD = 3;

          /**
          *
          */
          private static final long serialVersionUID = 1574586998788149344L;

          /**
          * 當前日期時間。
          *
          * @return
          */
          public static TDateTime CurrentDateTime() {
          return new TDateTime();
          }

          /**
          * 取得某年月最大日。
          *
          * @param y
          * @param m
          * @return
          */
          public static int getMaxDay(int y, int m) {
          if (isLeapNormalMonth(y, m))
          return 29;
          else if (isNormalMonth(y, m))
          return 28;
          else if (isSmallMonth(m))
          return 30;
          else
          return 31;
          }

          /**
          * 是否為大月
          *
          * @param m
          * @return
          */
          public static boolean isBigMonth(int m) {
          for (int i = 0; i < full_day_month.length; i++) {
          if (m == full_day_month[i])
          return true;
          }
          return false;
          }

          /**
          * 是否為閏平月
          *
          * @param year
          * @param month
          * @return
          */
          public static boolean isLeapNormalMonth(int y, int m) {
          return isleapyear(y) && m == 2;
          }

          /**
          * 是否閏年。
          *
          * @param y
          * @return
          */
          public static boolean isleapyear(int y) {
          return y >= gregorianCutoverYear ? ((y % 4 == 0) && ((y % 100 != 0) || (y % 400 == 0)))
          : // Gregorian
          (y % 4 == 0); // Julian
          }

          /**
          * 是否為普通平月
          *
          * @param year
          * @param month
          * @return
          */
          public static boolean isNormalMonth(int y, int m) {
          return !isleapyear(y) && m == 2;
          }

          /**
          * 是否為小月
          *
          * @param m
          * @return
          */
          public static boolean isSmallMonth(int m) {
          for (int i = 0; i < normal_day_month.length; i++) {
          if (m == normal_day_month[i])
          return true;
          }
          return false;
          }

          public static void main(String[] args) throws Exception {
          // System.out.println(CastType.dtparse("2005-3-3 00:00:00"));
          TDateTime tdt1 = new TDateTime("2005-3-32 10:00:00");
          TDateTime tdt2 = new TDateTime("2005-4-1 11:00:00");
          // TDateTime tdt1 = new TDateTime(2000, 2, 3, 4, 5, 6, 7);
          System.out.println(tdt1);
          System.out.println(tdt2);
          System.out.println(tdt1.getDayDistance(tdt2));
          System.out.println(tdt1);
          System.out.println(tdt2);
          }

          private Calendar cal = null;

          private Date dt = null;

          private Timestamp ts = null;

          public TDateTime() {
          cal = Calendar.getInstance();
          dt = cal.getTime();
          ts = new Timestamp(cal.getTimeInMillis());
          }

          public TDateTime(Calendar cal) {
          init(cal);
          }

          public TDateTime(Date dt) {
          init(dt);
          }

          public TDateTime(int year) {
          cal = Calendar.getInstance();
          cal.set(Calendar.YEAR, year);
          init(cal);
          }

          public TDateTime(int year, int month) {
          cal = Calendar.getInstance();
          cal.set(Calendar.YEAR, year);
          cal.set(Calendar.MONTH, month - 1);
          init(cal);
          }

          public TDateTime(int year, int month, int day) {
          cal = Calendar.getInstance();
          cal.set(Calendar.YEAR, year);
          cal.set(Calendar.MONTH, month - 1);
          cal.set(Calendar.DATE, day);
          init(cal);
          }

          public TDateTime(int year, int month, int day, int hour) {
          cal = Calendar.getInstance();
          cal.set(Calendar.YEAR, year);
          cal.set(Calendar.MONTH, month - 1);
          cal.set(Calendar.DATE, day);
          cal.set(Calendar.HOUR_OF_DAY, hour);
          init(cal);
          }

          public TDateTime(int year, int month, int day, int hour, int minute) {
          cal = Calendar.getInstance();
          cal.set(Calendar.YEAR, year);
          cal.set(Calendar.MONTH, month - 1);
          cal.set(Calendar.DATE, day);
          cal.set(Calendar.HOUR_OF_DAY, hour);
          cal.set(Calendar.MINUTE, minute);
          init(cal);
          }

          public TDateTime(int year, int month, int day, int hour, int minute,
          int second) {
          cal = Calendar.getInstance();
          cal.set(Calendar.YEAR, year);
          cal.set(Calendar.MONTH, month - 1);
          cal.set(Calendar.DATE, day);
          cal.set(Calendar.HOUR_OF_DAY, hour);
          cal.set(Calendar.MINUTE, minute);
          cal.set(Calendar.SECOND, second);
          init(cal);
          }

          public TDateTime(int year, int month, int day, int hour, int minute,
          int second, int millisecond) {
          cal = Calendar.getInstance();
          cal.set(Calendar.YEAR, year);
          cal.set(Calendar.MONTH, month - 1);
          cal.set(Calendar.DATE, day);
          cal.set(Calendar.HOUR_OF_DAY, hour);
          cal.set(Calendar.MINUTE, minute);
          cal.set(Calendar.SECOND, second);
          cal.set(Calendar.MILLISECOND, millisecond);
          init(cal);
          }

          public TDateTime(String value) throws Exception {
          this(CastType.dtparse(value));
          }

          public TDateTime(TDateTime tdt) {
          cal = (Calendar) tdt.cal.clone();
          init(cal);
          }

          public TDateTime(Timestamp ts) {
          init(ts);
          }

          /**
          * 增加日期。
          *
          * @param add
          */
          public void addDay(int add) {
          cal.add(Calendar.DATE, add);
          init(cal);
          }

          /**
          * 增加小時。
          *
          * @param add
          */
          public void addHour(int add) {
          cal.add(Calendar.HOUR_OF_DAY, add);
          init(cal);
          }

          /**
          * 增加毫秒。
          *
          * @param add
          */
          public void addMillisecond(int add) {
          cal.add(Calendar.MILLISECOND, add);
          init(cal);
          }

          /**
          * 增加分鐘。
          *
          * @param add
          */
          public void addMinute(int add) {
          cal.add(Calendar.MINUTE, add);
          init(cal);
          }

          /**
          * 增加月。
          *
          * @param add
          */
          public void addMonth(int add) {
          cal.add(Calendar.MONTH, add);
          init(cal);
          }

          /**
          * 增加秒。
          *
          * @param add
          */
          public void addSecond(int add) {
          cal.add(Calendar.SECOND, add);
          init(cal);
          }

          /**
          * 增加年份。
          *
          * @param add
          */
          public void addYear(int add) {
          cal.add(Calendar.YEAR, add);
          init(cal);
          }

          /*
          * (non-Javadoc)
          *
          * @see java.lang.Comparable#compareTo(java.lang.Object)
          */
          public int compareTo(Object o) {
          if (o == null) {
          return 1;
          }
          if (o instanceof TDateTime) {
          return compareTo((TDateTime) o);
          }
          throw new UnknowRuntimeException("can't compare TDateTime with "
          + o.getClass());
          }

          public int compareTo(TDateTime tdt) {
          if (tdt == null) {
          return 1;
          }
          return dt.compareTo(tdt.dt);
          }

          /**
          * 取得Calendar數據。
          *
          * @return
          */
          public Calendar getCalendar() {
          return cal;
          }

          /**
          * 取得Date數據。
          *
          * @return
          */
          public Date getDate() {
          return dt;
          }

          /**
          * 取得日期。
          *
          * @return Returns the day.
          */
          public int getDay() {
          return cal.get(Calendar.DATE);
          }

          /**
          * 取得兩個日期相隔天數。 <br>
          * 如果比參數日大,返回正數,否則如果小,返回負數,否則返回零。
          *
          * @param tdt
          * @return
          */
          public int getDayDistance(TDateTime tdt) {
          tdt = new TDateTime(tdt);
          TDateTime clone = new TDateTime(this);
          clone.setHour(0);
          clone.setMinute(0);
          clone.setSecond(0);
          clone.setMillisecond(0);
          tdt.setHour(0);
          tdt.setMinute(0);
          tdt.setSecond(0);
          tdt.setMillisecond(0);

          int day = 0;
          if (clone.compareTo(tdt) > 0) {
          while (clone.compareTo(tdt) > 0) {
          day++;
          clone.addDay(-1);
          }
          } else {
          while (clone.compareTo(tdt) < 0) {
          day--;
          clone.addDay(1);
          }
          }
          return day;
          }

          /**
          * 取得小時。
          *
          * @return Returns the hour.
          */
          public int getHour() {
          return cal.get(Calendar.HOUR_OF_DAY);
          }

          /**
          * 取得本月最大天數。
          *
          * @return
          */
          public int getMaxDay() {
          return getMaxDay(getYear(), getMonth());
          }

          /**
          * 取得毫秒。
          *
          * @return Returns the millisecond.
          */
          public int getMillisecond() {
          return cal.get(Calendar.MILLISECOND);
          }

          /**
          * 取得分鐘。
          *
          * @return Returns the minute.
          */
          public int getMinute() {
          return cal.get(Calendar.MINUTE);
          }

          /**
          * 取得月份。
          *
          * @return Returns the month.
          */
          public int getMonth() {
          return cal.get(Calendar.MONTH) + 1;
          }

          /**
          * 取得季節。
          *
          * @return
          */
          public int getQuarter() {
          int month = getMonth();
          switch (month) {
          case 1:
          case 2:
          case 3:
          return TDateTime.QUARTER_OF_FIRST;
          case 4:
          case 5:
          case 6:
          return TDateTime.QUARTER_OF_SECOND;
          case 7:
          case 8:
          case 9:
          return TDateTime.QUARTER_OF_THIRD;
          case 10:
          case 11:
          case 12:
          return TDateTime.QUARTER_OF_FOURTH;
          }
          throw new UnknowRuntimeException("month is error");
          }

          /**
          * 取得秒。
          *
          * @return Returns the second.
          */
          public int getSecond() {
          return cal.get(Calendar.SECOND);
          }

          /**
          * 獲得Timestamp類型數據。
          *
          * @return
          */
          public Timestamp getTimestamp() {
          return ts;
          }

          /**
          * 取得年。
          *
          * @return Returns the year.
          */
          public int getYear() {
          return cal.get(Calendar.YEAR);
          }

          /**
          * @param c
          */
          private void init(Calendar c) {
          cal = c;
          dt = cal.getTime();
          ts = new Timestamp(cal.getTimeInMillis());
          }

          /**
          * @param d
          */
          private void init(Date d) {
          dt = d;
          ts = new Timestamp(d.getTime());
          cal = Calendar.getInstance();
          cal.setTimeInMillis(ts.getTime());
          }

          /**
          * @param t
          */
          private void init(Timestamp t) {
          ts = t;
          cal = Calendar.getInstance();
          cal.setTimeInMillis(ts.getTime());
          dt = cal.getTime();
          }

          /**
          * 設置日期。
          *
          * @param day
          *????????????The day to set.
          */
          public void setDay(int day) {
          cal.set(Calendar.DATE, day);
          init(cal);
          }

          /**
          * 設置小時。
          *
          * @param hour
          *????????????The hour to set.
          */
          public void setHour(int hour) {
          cal.set(Calendar.HOUR_OF_DAY, hour);
          init(cal);
          }

          /**
          * 設置毫秒。
          *
          * @param millisecond
          *????????????The millisecond to set.
          */
          public void setMillisecond(int millisecond) {
          cal.set(Calendar.MILLISECOND, millisecond);
          init(cal);
          }

          /**
          * 設置分鐘。
          *
          * @param minute
          *????????????The minute to set.
          */
          public void setMinute(int minute) {
          cal.set(Calendar.MINUTE, minute);
          init(cal);
          }

          /**
          * 設置月份。
          *
          * @param month
          *????????????The month to set.
          */
          public void setMonth(int month) {
          cal.set(Calendar.MONTH, month - 1);
          init(cal);
          }

          /**
          * 設置秒。
          *
          * @param second
          *????????????The second to set.
          */
          public void setSecond(int second) {
          cal.set(Calendar.SECOND, second);
          init(cal);
          }

          /**
          * 設置年。
          *
          * @param year
          *????????????The year to set.
          */
          public void setYear(int year) {
          cal.set(Calendar.YEAR, year);
          init(cal);
          }

          /**
          * 返回日期串。
          *
          * @return
          */
          public String toDateString() {
          return toString("yyyy-MM-dd");
          }

          /**
          * 返回日期時間串
          *
          * @return
          */
          public String toDateTimeString() {
          return toString("yyyy-MM-dd HH:mm:ss.SSS");
          }

          /*
          * (non-Javadoc)
          *
          * @see java.lang.Object#toString()
          */
          public String toString() {
          StringBuffer sb = new StringBuffer();
          sb.append("TDateTime");
          sb.append("[");
          sb.append(getTimestamp());
          sb.append("]");
          sb.append("[");
          sb.append("Year:");
          sb.append(getYear());
          sb.append(",");
          sb.append("Month:");
          sb.append(getMonth());
          sb.append(",");
          sb.append("Day:");
          sb.append(getDay());
          sb.append(",");
          sb.append("Hour:");
          sb.append(getHour());
          sb.append(",");
          sb.append("Minute:");
          sb.append(getMinute());
          sb.append(",");
          sb.append("Second:");
          sb.append(getSecond());
          sb.append(",");
          sb.append("MilliSecond:");
          sb.append(getMillisecond());
          sb.append("]");
          return sb.toString();
          }

          /**
          * <pre>
          *??????????????????????????????時間格式語法:
          *????????????????????????
          *??????????????????????????????使用一個 time pattern 字符串指定時間格式。 在這種方式下,所有的 ASCII 字母被保留為模式字母,定義如下:
          *????????????????????????
          *??????????????????????????????符號???? 含義????????????????????表示????????????????示例
          *??????????????????????????????------?? -------???????????????? ------------????????-------
          *??????????????????????????????G????????年代標志符??????????????(Text)??????????????AD
          *??????????????????????????????y????????年??????????????????????(Number)????????????1996
          *??????????????????????????????M????????月??????????????????????(Text & Number)???? July & 07
          *??????????????????????????????d????????日??????????????????????(Number)????????????10
          *??????????????????????????????h????????時 在上午或下午 (1?12)??(Number)????????????12
          *??????????????????????????????H????????時 在一天中 (0?23)??????(Number)????????????0
          *??????????????????????????????m????????分??????????????????????(Number)????????????30
          *??????????????????????????????s????????秒??????????????????????(Number)????????????55
          *??????????????????????????????S????????毫秒????????????????????(Number)????????????978
          *??????????????????????????????E????????星期????????????????????(Text)??????????????Tuesday
          *??????????????????????????????D????????一年中的第幾天??????????(Number)????????????189
          *??????????????????????????????F????????一月中第幾個星期幾??????(Number)????????????2??(2nd Wed in July)
          *??????????????????????????????w????????一年中第幾個星期????????(Number)????????????27
          *??????????????????????????????W????????一月中第幾個星期????????(Number)????????????2
          *??????????????????????????????a????????上午 / 下午 標記符??????(Text)??????????????PM
          *??????????????????????????????k????????時 在一天中 (1?24)??????(Number)????????????24
          *??????????????????????????????K????????時 在上午或下午 (0?11)??(Number)????????????0
          *??????????????????????????????z????????時區????????????????????(Text)??????Pacific Standard Time
          *??????????????????????????????'????????文本轉義符??????????????(Delimiter)
          *??????????????????????????????''?????? 單引號??????????????????(Literal)?????????? '
          * </pre>
          *
          * @param parttern
          * @return
          */
          /**
          * @param parttern
          * @return
          */
          public String toString(String parttern) {
          SimpleDateFormat sdf = new SimpleDateFormat(parttern);
          return sdf.format(dt);
          }

          /**
          * 返回短時間類型。
          *
          * @return
          */
          public String toShortTimeString() {
          return toString("HH:mm");
          }

          /**
          * 返回時間串。
          *
          * @return
          */
          public String toTimeString() {
          return toString("HH:mm:ss.SSS");
          }
          }

          posted on 2006-06-30 14:21 JStar 閱讀(1379) 評論(5)  編輯  收藏

          評論

          # re: 我也貢獻一個去年做項目自己寫的一個日期類吧,供大家參考  回復  更多評論   

          原來是文思的兄弟,呵呵,算半個同事,希望以后能交流交流,我的QQ:12933725
          2006-06-30 14:52 | Jason Huggins

          # re: 我也貢獻一個去年做項目自己寫的一個日期類吧,供大家參考  回復  更多評論   

          技術無國界,歡迎提出寶貴意見,以后多交流
          2006-06-30 14:56 | JStar

          # re: 我也貢獻一個去年做項目自己寫的一個日期類吧,供大家參考  回復  更多評論   

          呵呵,收下了
          2007-10-30 10:58 | 式樣

          # 我的成長  回復  更多評論   

          關于成長我不知道怎樣說

          但是我可以作為學生來講應該先明白自己的目標有好大;;;;;;;;;;;;;;;;;;;;;;;;

          在心理上要好好的把握,先樂觀的對待它.在去追求它的一切''''''也許對于其他的同學來講就不一定了'


          我建議去我們學校的網站去看看


          有建議者可以發表到徐家中學教育網站
          2009-01-13 17:33 | 安恬恬

          # 哦哦  回復  更多評論   

          我的QQ是
          511323890



          10;3的學生

          李安
          2009-01-13 17:36 |

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


          網站導航:
           
          主站蜘蛛池模板: 孟连| 襄汾县| 康乐县| 宁安市| 库伦旗| 泌阳县| 云霄县| 彭州市| 金昌市| 绵竹市| 西城区| 平利县| 荥经县| 封开县| 东方市| 禄劝| 南川市| 南平市| 威宁| 林周县| 藁城市| 金溪县| 道孚县| 沁水县| 兖州市| 西丰县| 富蕴县| 潜江市| 电白县| 宁乡县| 北碚区| 石景山区| 肥西县| 湟中县| 邵武市| 定兴县| 东兰县| 新丰县| 正安县| 平湖市| 武山县|