zoukankan      html  css  js  c++  java
  • concurrent包中的TimeUnit类

      TimeUnit类是concurrent包中的一个类,主要的方法是实现时间的转换,如天数转换为时间,或从时间转换成天数。

      在该类中定义了一个枚举类型:代码为:

      

    NANOSECONDS {
            public long toNanos(long d)   { return d; }
            public long toMicros(long d)  { return d/(C1/C0); }
            public long toMillis(long d)  { return d/(C2/C0); }
            public long toSeconds(long d) { return d/(C3/C0); }
            public long toMinutes(long d) { return d/(C4/C0); }
            public long toHours(long d)   { return d/(C5/C0); }
            public long toDays(long d)    { return d/(C6/C0); }
            public long convert(long d, TimeUnit u) { return u.toNanos(d); }
            int excessNanos(long d, long m) { return (int)(d - (m*C2)); }
        },
        MICROSECONDS {
            public long toNanos(long d)   { return x(d, C1/C0, MAX/(C1/C0)); }
            public long toMicros(long d)  { return d; }
            public long toMillis(long d)  { return d/(C2/C1); }
            public long toSeconds(long d) { return d/(C3/C1); }
            public long toMinutes(long d) { return d/(C4/C1); }
            public long toHours(long d)   { return d/(C5/C1); }
            public long toDays(long d)    { return d/(C6/C1); }
            public long convert(long d, TimeUnit u) { return u.toMicros(d); }
            int excessNanos(long d, long m) { return (int)((d*C1) - (m*C2)); }
        },
        MILLISECONDS {
            public long toNanos(long d)   { return x(d, C2/C0, MAX/(C2/C0)); }
            public long toMicros(long d)  { return x(d, C2/C1, MAX/(C2/C1)); }
            public long toMillis(long d)  { return d; }
            public long toSeconds(long d) { return d/(C3/C2); }
            public long toMinutes(long d) { return d/(C4/C2); }
            public long toHours(long d)   { return d/(C5/C2); }
            public long toDays(long d)    { return d/(C6/C2); }
            public long convert(long d, TimeUnit u) { return u.toMillis(d); }
            int excessNanos(long d, long m) { return 0; }
        },
        SECONDS {
            public long toNanos(long d)   { return x(d, C3/C0, MAX/(C3/C0)); }
            public long toMicros(long d)  { return x(d, C3/C1, MAX/(C3/C1)); }
            public long toMillis(long d)  { return x(d, C3/C2, MAX/(C3/C2)); }
            public long toSeconds(long d) { return d; }
            public long toMinutes(long d) { return d/(C4/C3); }
            public long toHours(long d)   { return d/(C5/C3); }
            public long toDays(long d)    { return d/(C6/C3); }
            public long convert(long d, TimeUnit u) { return u.toSeconds(d); }
            int excessNanos(long d, long m) { return 0; }
        },
        MINUTES {
            public long toNanos(long d)   { return x(d, C4/C0, MAX/(C4/C0)); }
            public long toMicros(long d)  { return x(d, C4/C1, MAX/(C4/C1)); }
            public long toMillis(long d)  { return x(d, C4/C2, MAX/(C4/C2)); }
            public long toSeconds(long d) { return x(d, C4/C3, MAX/(C4/C3)); }
            public long toMinutes(long d) { return d; }
            public long toHours(long d)   { return d/(C5/C4); }
            public long toDays(long d)    { return d/(C6/C4); }
            public long convert(long d, TimeUnit u) { return u.toMinutes(d); }
            int excessNanos(long d, long m) { return 0; }
        },
        HOURS {
            public long toNanos(long d)   { return x(d, C5/C0, MAX/(C5/C0)); }
            public long toMicros(long d)  { return x(d, C5/C1, MAX/(C5/C1)); }
            public long toMillis(long d)  { return x(d, C5/C2, MAX/(C5/C2)); }
            public long toSeconds(long d) { return x(d, C5/C3, MAX/(C5/C3)); }
            public long toMinutes(long d) { return x(d, C5/C4, MAX/(C5/C4)); }
            public long toHours(long d)   { return d; }
            public long toDays(long d)    { return d/(C6/C5); }
            public long convert(long d, TimeUnit u) { return u.toHours(d); }
            int excessNanos(long d, long m) { return 0; }
        },
        DAYS {
            public long toNanos(long d)   { return x(d, C6/C0, MAX/(C6/C0)); }
            public long toMicros(long d)  { return x(d, C6/C1, MAX/(C6/C1)); }
            public long toMillis(long d)  { return x(d, C6/C2, MAX/(C6/C2)); }
            public long toSeconds(long d) { return x(d, C6/C3, MAX/(C6/C3)); }
            public long toMinutes(long d) { return x(d, C6/C4, MAX/(C6/C4)); }
            public long toHours(long d)   { return x(d, C6/C5, MAX/(C6/C5)); }
            public long toDays(long d)    { return d; }
            public long convert(long d, TimeUnit u) { return u.toDays(d); }
            int excessNanos(long d, long m) { return 0; }
        };

      可以通过定义的方法实现转化:

    如1小时转换为分钟数60分钟:

    TimeUnit.HOURS.toMinutes(1)  ->60

      toMinutes方法将返回return x(d, C5/C4, MAX/(C5/C4));

      在该类中定义了

    static long x(long d, long m, long over) {
            if (d >  over) return Long.MAX_VALUE;
            if (d < -over) return Long.MIN_VALUE;
            return d * m;
        }

    实现时间的转换。

  • 相关阅读:
    代码改变世界,随手写了点代码解决了一个小学生级别的作业题,编程要从娃娃抓起
    智和信通实现信创国产化适配 助力信创生态智能运维体系建设
    智和信通赋能国产信创 建设IT智能监控运维体系
    北京智和信通智慧档案馆网络监控运维解决方案
    图书馆网络运维监控安全态势感知方案-智和信通
    广播电视网络运维安全态势感知解决方案
    北京智和信通荣膺2020智能运维星耀榜最具影响力企业
    直播新规出台!如何用技术管住乱打赏的「熊孩子」?
    是什么让你在赛博空间更好看?
    Niu Talk 数据科学系列论坛:明晚,我们聊聊大数据与开源
  • 原文地址:https://www.cnblogs.com/luoweiKnowledge/p/4230424.html
Copyright © 2011-2022 走看看