posts - 33,  comments - 17,  trackbacks - 0
            1import java.text.DecimalFormat;
            2import java.util.Arrays;
            3
            4/**
            5 * 時(shí)間計(jì)算工具類
            6 */

            7public class Time {
            8
            9    /**
           10     * 時(shí)間字段常量,表示“秒”
           11      */

           12    public final static int SECOND = 0;
           13    
           14    /**
           15     * 時(shí)間字段常量,表示“分”
           16      */

           17    public final static int MINUTE = 1;
           18    
           19    /**
           20     * 時(shí)間字段常量,表示“時(shí)”
           21      */

           22    public final static int HOUR = 2;
           23    
           24    /**
           25     * 時(shí)間字段常量,表示“天”
           26      */

           27    public final static int DAY = 3;
           28
           29    /**
           30     * 各常量允許的最大值
           31      */

           32    private final int[] maxFields = 595923, Integer.MAX_VALUE - 1 };
           33    
           34    /**
           35     * 各常量允許的最小值
           36      */

           37    private final int[] minFields = 000, Integer.MIN_VALUE };
           38    
           39    /**
           40     * 默認(rèn)的字符串格式時(shí)間分隔符
           41      */

           42    private String timeSeparator = ":";
           43    
           44    /**
           45     * 時(shí)間數(shù)據(jù)容器
           46      */

           47    private int[] fields = new int[4];    
           48    
           49    /**
           50     * 無參構(gòu)造,將各字段置為 0
           51     */

           52    public Time() {
           53        this(0000);
           54    }

           55
           56    /**
           57     * 使用時(shí)、分構(gòu)造一個(gè)時(shí)間
           58      * @param hour      小時(shí)
           59      * @param minute    分鐘
           60      */

           61    public Time(int hour, int minute) {
           62        this(0, hour, minute, 0);
           63    }

           64
           65    /**
           66     * 使用時(shí)、分、秒構(gòu)造一個(gè)時(shí)間
           67      * @param hour      小時(shí)
           68      * @param minute    分鐘
           69      * @param second    秒
           70      */

           71    public Time(int hour, int minute, int second) {
           72        this(0, hour, minute, second);
           73    }

           74    
           75    /**
           76     * 使用一個(gè)字符串構(gòu)造時(shí)間<br/>
           77     * Time time = new Time("14:22:23");
           78     * @param time      字符串格式的時(shí)間,默認(rèn)采用“:”作為分隔符
           79      */

           80    public Time(String time) {        
           81        this(time, null);
           82    }

           83
           84    /**
           85     * 使用天、時(shí)、分、秒構(gòu)造時(shí)間,進(jìn)行全字符的構(gòu)造
           86      * @param day       天
           87      * @param hour      時(shí)
           88      * @param minute    分
           89      * @param second    秒
           90      */

           91    public Time(int day, int hour, int minute, int second) {
           92        set(DAY, day);
           93        set(HOUR, hour);
           94        set(MINUTE, minute);
           95        set(SECOND, second);
           96    }
            
           97    
           98    /**
           99     * 使用一個(gè)字符串構(gòu)造時(shí)間,指定分隔符<br/>
          100     * Time time = new Time("14-22-23", "-");
          101     * @param time      字符串格式的時(shí)間
          102      */

          103    public Time(String time, String timeSeparator) {
          104        if(timeSeparator != null{
          105            setTimeSeparator(timeSeparator);
          106        }

          107        String pattern = patternQuote(this.timeSeparator);
          108        String matcher = new StringBuffer()
          109                                .append("\\d+?").append(pattern)
          110                                .append("\\d+?").append(pattern)
          111                                .append("\\d+?")
          112                                .toString();
          113        if(!time.matches(matcher)) {
          114            throw new IllegalArgumentException(time + ", time format error, HH"
          115                    + this.timeSeparator + "mm" + this.timeSeparator + "ss");
          116        }
                  
          117        String[] times = time.split(pattern);
          118        set(DAY, 0);
          119        set(HOUR, Integer.parseInt(times[0]));
          120        set(MINUTE, Integer.parseInt(times[1]));
          121        set(SECOND, Integer.parseInt(times[2]));
          122    }

          123    
          124    /**
          125     * 設(shè)置時(shí)間字段的值
          126     * @param field     時(shí)間字段常量
          127     * @param value     時(shí)間字段的值
          128     */

          129    public void set(int field, int value) {        
          130        if(value < minFields[field]) {
          131            throw new IllegalArgumentException(value +
          132                    ", time value must be positive.");
          133        }

          134        fields[field] = value % (maxFields[field] + 1);
          135        // 進(jìn)行進(jìn)位計(jì)算
          136         int carry = value / (maxFields[field] + 1);
          137        if(carry > 0{
          138            int upFieldValue = get(field + 1);
          139            set(field + 1, upFieldValue + carry);
          140        }

          141    }

          142
          143    /**
          144     * 獲得時(shí)間字段的值
          145      * @param field     時(shí)間字段常量
          146      * @return          該時(shí)間字段的值
          147      */

          148    public int get(int field) {
          149        if(field < 0 || field > fields.length - 1{
          150            throw new IllegalArgumentException(field + ", field value is error.");
          151        }

          152        return fields[field];
          153    }

          154
          155    /**
          156     * 將時(shí)間進(jìn)行“加”運(yùn)算,即加上一個(gè)時(shí)間
          157      * @param time      需要加的時(shí)間
          158      * @return          運(yùn)算后的時(shí)間
          159      */

          160    public Time addTime(Time time) {
          161        Time result = new Time();
          162        int up = 0;     // 進(jìn)位標(biāo)志
          163         for (int i = 0; i < fields.length; i++{
          164            int sum = fields[i] + time.fields[i] + up;
          165            up = sum / (maxFields[i] + 1);
          166            result.fields[i] = sum % (maxFields[i] + 1);
          167        }

          168        return result;
          169    }

          170
          171    /**
          172     * 將時(shí)間進(jìn)行“減”運(yùn)算,即減去一個(gè)時(shí)間
          173      * @param time      需要減的時(shí)間
          174      * @return          運(yùn)算后的時(shí)間
          175      */

          176    public Time subtractTime(Time time) {
          177        Time result = new Time();
          178        int down = 0;       // 退位標(biāo)志
          179         for (int i = 0, k = fields.length - 1; i < k; i++{
          180            int difference = fields[i] + down;
          181            if (difference >= time.fields[i]) {
          182                difference -= time.fields[i];
          183                down = 0;
          184            }
           else {
          185                difference += maxFields[i] + 1 - time.fields[i];
          186                down = -1;
          187            }

          188            result.fields[i] = difference;
          189        }

          190        result.fields[DAY] = fields[DAY] - time.fields[DAY] + down;
          191        return result;
          192    }

          193    
          194    /**
          195     * 獲得時(shí)間字段的分隔符
          196      * @return
          197     */

          198    public String getTimeSeparator() {
          199        return timeSeparator;
          200    }

          201
          202    /**
          203     * 設(shè)置時(shí)間字段的分隔符(用于字符串格式的時(shí)間)
          204      * @param timeSeparator     分隔符字符串
          205      */

          206    public void setTimeSeparator(String timeSeparator) {
          207        this.timeSeparator = timeSeparator;
          208    }

          209
          210    /**
          211     * 正則表達(dá)式引用處理方法,源自 JDK @link java.util.regex.Pattern#quote(String)
          212     */

          213    private String patternQuote(String s) {
          214        int slashEIndex = s.indexOf("\\E");
          215        if (slashEIndex == -1)
          216            return "\\Q" + s + "\\E";
          217
          218        StringBuilder sb = new StringBuilder(s.length() * 2);
          219        sb.append("\\Q");
          220        slashEIndex = 0;
          221        int current = 0;
          222        while ((slashEIndex = s.indexOf("\\E", current)) != -1{
          223            sb.append(s.substring(current, slashEIndex));
          224            current = slashEIndex + 2;
          225            sb.append("\\E\\\\E\\Q");
          226        }

          227        sb.append(s.substring(current, s.length()));
          228        sb.append("\\E");
          229        return sb.toString();
          230    }

          231
          232    public String toString() {
          233        DecimalFormat df = new DecimalFormat("00");
          234        return new StringBuffer().append(fields[DAY]).append("")
          235                    .append(df.format(fields[HOUR])).append(timeSeparator)
          236                    .append(df.format(fields[MINUTE])).append(timeSeparator)
          237                    .append(df.format(fields[SECOND]))
          238                    .toString();
          239    }

          240
          241    public int hashCode() {
          242        final int PRIME = 31;
          243        int result = 1;
          244        result = PRIME * result + Arrays.hashCode(fields);
          245        return result;
          246    }

          247
          248    public boolean equals(Object obj) {
          249        if (this == obj)
          250            return true;
          251        if (obj == null)
          252            return false;
          253        if (getClass() != obj.getClass())
          254            return false;
          255        final Time other = (Time) obj;
          256        if (!Arrays.equals(fields, other.fields)) {
          257            return false;
          258        }

          259        return true;
          260    }

          261}

          262
          posted on 2008-07-24 08:28 scea2009 閱讀(571) 評論(1)  編輯  收藏 所屬分類: 網(wǎng)摘

          FeedBack:
          # re: 時(shí)間計(jì)算工具類
          2008-12-30 13:15 | 北京時(shí)間
          很有價(jià)值  回復(fù)  更多評論
            

          <2008年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          PL/SQL存儲過程與函數(shù)

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 天祝| 祁门县| 嘉鱼县| 大悟县| 南涧| 枣阳市| 无锡市| 增城市| 金昌市| 三门县| 柘城县| 兴安县| 耒阳市| 邓州市| 东莞市| 南城县| 商都县| 安西县| 常德市| 泽州县| 叶城县| 凭祥市| 无棣县| 景德镇市| 高清| 北流市| 兰坪| 广宁县| 河北省| 揭东县| 连南| 孙吴县| 平江县| 台南县| 涟水县| 马鞍山市| 信宜市| 都匀市| 安多县| 宝鸡市| 水富县|