?? commondate.java
字號:
package com.huiton.cerp.pub.util.functions;import java.util.*;/** * Title: 通用日期 * Description: 處理通常使用的日期對象 * Copyright: Copyright Reserved By BRITC * Company: BRITC * @author 張愛軍 * @version 1.0 */public class CommonDate extends GregorianCalendar{ private String m_sep = "/"; //默認日期分隔符 private String m_colon = ":"; //默認的小時分鐘分隔符 /**無參構造器* */ public CommonDate() { super(); } /**構造器* */ public CommonDate(long t) { super(); super.setTimeInMillis(t); } /**構造器* */ public CommonDate(Date today) { super(); super.setTime(today); } /**構造器* * @param s * '_'可為任意字符,可準確處理形如'2002_02_02' or '20020202' 兩種形式的參數 * 對其中的非法字符,不指出異常,但日期可能不是所求 * */ public CommonDate(String s) { super(); // get the length, if it is more than 8, then get separator, // and split it use separator s = (s==null ? "" : s.trim()); int len = s.length(); try { if (s.length()== 8) { this.setYear(Integer.parseInt(s.substring(0,4))); this.setMonth(Integer.parseInt(s.substring(4,6))); this.setDate(Integer.parseInt(s.substring(6,8))); }else if (s.length()==10) { this.setYear(Integer.parseInt(s.substring(0,4))); this.setMonth(Integer.parseInt(s.substring(5,7))); this.setDate(Integer.parseInt(s.substring(8,10))); }else { } } catch (Exception e) { // } } /**構造器* * @param strYear year的字符串 * @param strMonth month的字符串 * @param strDate date的字符串 * */ public CommonDate(String strYear,String strMonth,String strDate) { super(); try { this.setYear(Integer.parseInt(strYear)); this.setMonth(Integer.parseInt(strMonth)); this.setDate(Integer.parseInt(strDate)); } catch(Exception e) { } } /**構造器* */ public CommonDate(int year,int month,int date) { this(year,month,date,0,0,0); } /**構造器* */ public CommonDate(int year,int month,int date,int hour,int min) { this(year,month,date,hour,min,0); } /**構造器* */ public CommonDate(int year,int month,int date,int hour,int min,int sec) { super(year,month-1,date,hour,min,sec); } /** 返回傳入日期和此日期之間的分鐘數 * @param cd 傳入日期 * @return int cd - this */ public int getDiffMinutes(CommonDate cd ) { int thisTime = (int)(this.getTimeInMillis()/60000); int cdTime = (int)(cd.getTimeInMillis()/60000); return cdTime-thisTime ; } /**返回幾小時后/前的日期 * @param hour,(hour>0)增加/(hour<0)減少的小時數*/ public CommonDate addHours(int hour) { CommonDate other = (CommonDate) this.clone(); if (hour==0) return other; other.add(HOUR,hour); return other ; } /**返回幾分鐘后/前的日期 * @param min,(min>0)增加/(min<0)減少的分鐘數*/ public CommonDate addMinutes(int min) { CommonDate other = (CommonDate) this.clone(); if (min==0) return other; other.add(MINUTE,min); return other ; } /**返回幾秒種后/前的日期 * @param sec,(sec>0)增加/(sec<0)減少的秒數*/ public CommonDate addSeconds(int sec) { CommonDate other = (CommonDate) this.clone(); if (sec==0) return other; other.add(SECOND,sec); return other ; } /**對象復制*/ public Object clone() { { CommonDate other = (CommonDate) super.clone(); other.m_sep = this.m_sep; return other; } } public int getYear() { return this.get(YEAR); } public int getMonth() { return this.get(MONTH)+1; } public int getDate() { return this.get(DATE); } public int getMinute() { return this.get(MINUTE); } public int getHour() { return this.get(HOUR_OF_DAY); } public void setYear(int year) { this.set(YEAR,year); } public void setMonth(int month) { this.set(MONTH,month-1); } public void setDate(int date) { this.set(DAY_OF_MONTH,date); } /**返回變遷后的日期 * @param i 變遷的天數,大于0則后移,否則前移 * */ public CommonDate shiftDate(int i) { CommonDate cDate = (CommonDate) this.clone(); if (i==0) return cDate; cDate.add(DATE, i); return cDate; } public CommonDate prevDate() { return shiftDate(-1); } public CommonDate nextDate() { return shiftDate(1); } /**返回周變遷后的日期 * @param i 變遷的周數,大于0則后移,否則前移 * */ public CommonDate shiftWeek(int i) { return shiftDate(7*i); } public CommonDate nextWeek() { return shiftWeek(1); } public CommonDate prevWeek() { return shiftWeek(-1); } /**返回月變遷后的日期 * @param i 變遷的月數,大于0則后移,否則前移 * */ public CommonDate shiftMonth(int i) { CommonDate cDate = (CommonDate) this.clone(); if (i==0) return cDate; cDate.add(MONTH, i); return cDate; } public CommonDate prevMonth() { return shiftMonth(-1); } public CommonDate nextMonth() { return shiftMonth(1); } /**返回年變遷后的日期 * @param i 變遷的年數,大于0則后移,否則前移 * */ public CommonDate shiftYear(int i) { CommonDate cDate = (CommonDate) this.clone(); if (i==0) return cDate; cDate.add(YEAR, i); return cDate; } public CommonDate prevYear() { return shiftYear(-1); } public CommonDate nextYear() { return shiftYear(1); } /**返回星期幾,sunday is 0,monday is 1... * */ public int getDayOfWeek() { return this.get(DAY_OF_WEEK) - SUNDAY; } /**返回該月天數 * */ public int getDaysOfTheMonth() { return this.getActualMaximum(DAY_OF_MONTH); } /**移到月初 * */ public CommonDate firstDayOfTheMonth() { return new CommonDate(getYear(),getMonth(),1); } /**移到該周周末,sunday 0 * */ public CommonDate firstDayOfTheWeek() { return shiftDate(-getDayOfWeek()); } /**返回字符串年月日,如:2002/01/20 * */ public String getYMD() { int intYear = this.getYear(); int intMonth = this.getMonth(); int intDate = this.getDate(); return intYear + m_sep + (intMonth<10 ? "0"+intMonth : ""+intMonth) + m_sep + (intDate<10 ? "0"+intDate : ""+intDate); } public String getMainKey() { int intYear = this.getYear(); int intMonth = this.getMonth(); int intDate = this.getDate(); return intYear + (intMonth<10 ? "0"+intMonth : ""+intMonth) + (intDate<10 ? "0"+intDate : ""+intDate); } /**返回字符串時分,如13:02 * */ public String getHM() { int intHour = getHour(); int intMinute = getMinute(); return (intHour<10 ? "0"+intHour : ""+intHour) + ":" + (intMinute<10 ? "0"+intMinute : ""+intMinute); } /**設置日期分隔符 * @param sep,新的分隔符 * */ public void setSep(String sep) { sep = (sep==null ? "" : sep.trim()); if (sep.length()>0) m_sep = sep.substring(0,1); } /**返回日期分隔符 * */ public String getSep() { return m_sep; } /**設置時分分隔符 * @param sep,新的分隔符 * */ public void setColon(String colon) { colon = (colon==null ? "" : colon.trim()); if (colon.length()>0) m_colon = colon.substring(0,1); } /**返回時分分隔符 * */ public String getColon() { return m_colon; } /**將對象轉變為字符串 * */ public String toString() { return getYMD(); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -