<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

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

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

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

     22    public final static int HOUR = 2;
     23    
     24    /**
     25     * 時間字段常量,表示“天”
     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     * 默認的字符串格式時間分隔符
     41      */

     42    private String timeSeparator = ":";
     43    
     44    /**
     45     * 時間數據容器
     46      */

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

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

     55
     56    /**
     57     * 使用時、分構造一個時間
     58      * @param hour      小時
     59      * @param minute    分鐘
     60      */

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

     64
     65    /**
     66     * 使用時、分、秒構造一個時間
     67      * @param hour      小時
     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     * 使用一個字符串構造時間<br/>
     77     * Time time = new Time("14:22:23");
     78     * @param time      字符串格式的時間,默認采用“:”作為分隔符
     79      */

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

     83
     84    /**
     85     * 使用天、時、分、秒構造時間,進行全字符的構造
     86      * @param day       天
     87      * @param hour      時
     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     * 使用一個字符串構造時間,指定分隔符<br/>
    100     * Time time = new Time("14-22-23", "-");
    101     * @param time      字符串格式的時間
    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     * 設置時間字段的值
    126     * @param field     時間字段常量
    127     * @param value     時間字段的值
    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        // 進行進位計算
    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     * 獲得時間字段的值
    145      * @param field     時間字段常量
    146      * @return          該時間字段的值
    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     * 將時間進行“加”運算,即加上一個時間
    157      * @param time      需要加的時間
    158      * @return          運算后的時間
    159      */

    160    public Time addTime(Time time) {
    161        Time result = new Time();
    162        int up = 0;     // 進位標志
    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     * 將時間進行“減”運算,即減去一個時間
    173      * @param time      需要減的時間
    174      * @return          運算后的時間
    175      */

    176    public Time subtractTime(Time time) {
    177        Time result = new Time();
    178        int down = 0;       // 退位標志
    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     * 獲得時間字段的分隔符
    196      * @return
    197     */

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

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

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

    209
    210    /**
    211     * 正則表達式引用處理方法,源自 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 閱讀(566) 評論(1)  編輯  收藏 所屬分類: 網摘

    FeedBack:
    # re: 時間計算工具類
    2008-12-30 13:15 | 北京時間
    很有價值  回復  更多評論
      

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

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案

    PL/SQL存儲過程與函數

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 久久精品免费一区二区喷潮 | 特级aaaaaaaaa毛片免费视频| 四虎永久免费影院| 青青操在线免费观看| 亚洲冬月枫中文字幕在线看| 亚洲&#228;v永久无码精品天堂久久 | 亚洲AV无码专区在线亚| 亚洲AV无码乱码精品国产| **一级毛片免费完整视| 色哟哟国产精品免费观看| 亚洲综合免费视频| 亚洲精品tv久久久久久久久久| 最近在线2018视频免费观看| 成人国产网站v片免费观看| 亚洲国产成人精品无码区在线秒播| 亚洲国产aⅴ综合网| 成年免费大片黄在线观看岛国| 亚洲免费视频一区二区三区| 中文字幕 亚洲 有码 在线| 国产V亚洲V天堂无码| 免费看国产精品麻豆| 免费精品国产自产拍在线观看图片| h片在线播放免费高清| 亚洲高清有码中文字| 亚洲国产第一页www| 亚洲无码日韩精品第一页| 成年女人视频网站免费m| 久久er国产精品免费观看2| 美女被免费网站在线视频免费 | 一级毛片完整版免费播放一区| 亚洲乱码一二三四区乱码| 91亚洲导航深夜福利| 亚洲午夜无码久久久久| 亚洲成a人片在线观看日本麻豆| 中文字幕人成无码免费视频| 99热在线精品免费播放6| 在线观看黄片免费入口不卡| 美女露隐私全部免费直播| 亚洲精品无码少妇30P| 亚洲人色大成年网站在线观看| 亚洲成人午夜在线|