天天看点

这样才是写工具类TimeUtils编码时的考量小结

这样才是写工具类TimeUtils编码时的考量小结
文章讲的很浅,道理大家都懂。写出来,是为了更好的贯彻它!

与君共勉
           

什么是工具类?

这样才是写工具类TimeUtils编码时的考量小结

这种 低级的问题还问的出来。

最近我发现 好多东西都是一看就懂。然而,自己不知道什么时候就能写出 shi 一样的代码。

这是因为编码时的构思,没有明确下来,构思不清晰,就相当于地基不稳。

接下来,我们以 TimeUtil为例讲解我的思考。

TimeUtils编码时的考量

偶然的一次CodeReview,发现TimeUtils竟然不知道什么时候,变乱了。

我知道 我该重构了。可是在重构之前,我仔细反思了自己对这个类的维护。

说到底还是思考不充分导致。

没有经过思考的代码,是没有灵气的代码。

大道至简

我需要一个核心思想。于是,我开始琢磨。。

思考描述

及时的记录自己当时的想法,请写进CodeDesc中。

* 在实际项目中,总可能会出现各种各样的时间格式,花样百出
 * 后端给N种格式,前端再转换再显示。
 * 讲道理,是要统一后端给的格式:时间戳。
 * 这个后端给的时间,是要结合时区的。唯有时间戳不可破。
 * 为了应对这种情况,前端必须适配。
 * 这是一个工具类,要具备通用性。
 * 这个类,应该提供各种类型转换成long的方法
 * 提供,long转换成各种格式的方法。(当然,时间格式,除了通用的都是模块自己提供)
 * 做法:
 * 1. 子模块,可以自己定义TimeConst。
 * 2. 子模块先将后端的返回字段,转换成long的时间戳
 * 2. 根据 子模块自定义的格式,转换成需要的格式。
 * <p>
 * 这个类包括:
 * 1、getTime从String、Date、Time获取时间戳
 * 2、从long时间戳,转化为各种格式
 * 3、String转Date
 * 4、String转Calendar
 * 5、或者你会需要扩展些功能。我建议将公共的部分塞到public里头去,必须做差。
           

这份描述表达了我编写这个代码时的思考。

这段文字写出来,这个类就算是实现了一半了;具体的编码实现就是具体的思考。

思考永远是最重要的

你的考量会给后面的同事带来很多便捷,节约大量的时间。

工具类的初始化
protected TimeUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }
           
getTime从String、Date、Time获取时间戳
/**
     * Formatted time string to the milliseconds.
     *
     * @param time       The formatted time string.
     * @param timeFormat The format.
     * @return the milliseconds
     */
    public static long getTime(final String time, @NonNull final String timeFormat) {
        return getTime(time, new SimpleDateFormat(timeFormat));
    }

    /**
     * Formatted time string to the milliseconds.
     *
     * @param time   The formatted time string.
     * @param format The format.
     * @return the milliseconds
     */
    public static long getTime(final String time, @NonNull final DateFormat format) {
        try {
            return format.parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 从Date转成时间戳
     *
     * @param date
     * @return
     */
    public static long getTime(@NonNull final Date date) {
        return date.getTime();
    }

    /**
     * 从Date转成时间戳
     *
     * @param date
     * @return
     */
    public static long getTime(@NonNull final Calendar date) {
        return getTime(date.getTime());
    }
    
    /**
     * 从Date转成时间戳
     *
     * @param date
     * @return
     */
    public static long getTime(@NonNull final Calendar date) {
        return getTime(date.getTime());
    }
           
从long时间戳,转化为各种格式
/**
     * Milliseconds to the formatted time string.
     *
     * @param millis     The milliseconds.
     * @param timeFormat The format.
     * @return the formatted time string
     */
    public static String millis2String(final long millis, @NonNull final String timeFormat) {
        return millis2String(millis, new SimpleDateFormat(timeFormat));
    }

    /**
     * UTC time to format
     *
     * @param utcTime
     * @param format  yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String formatTo(String utcTime, String format) {
        if (utcTime == null) {
            return "";
        }
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
        Date result;
        try {
            result = df.parse(utcTime);
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
            return sdf.format(result);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return utcTime;
    }

    /**
     * Milliseconds to the formatted time string.
     *
     * @param millis The milliseconds.
     * @param format The format.
     * @return the formatted time string
     */
    public static String millis2String(final long millis, @NonNull final DateFormat format) {
        return format.format(new Date(millis));
    }
           
String转Date
/**
     * Formatted time string to the milliseconds.
     *
     * @param time   The formatted time string.
     * @param timeFormat The format.
     * @return the milliseconds
     */
    public static long string2Millis(final String time, @NonNull final String timeFormat) {
        return string2Millis(time, new SimpleDateFormat(timeFormat));
    }

    /**
     * Formatted time string to the milliseconds.
     *
     * @param time   The formatted time string.
     * @param format The format.
     * @return the milliseconds
     */
    public static long string2Millis(final String time, @NonNull final DateFormat format) {
        try {
            return format.parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }
           
String转Calendar
/**
     * Formatted time string to the date.
     *
     * @param time       The formatted time string.
     * @param timeFormat The format.
     * @return the date
     */
    public static Date string2Date(final String time, @NonNull final String timeFormat) {
        return string2Date(time, new SimpleDateFormat(timeFormat));
    }

    /**
     * Formatted time string to the date.
     *
     * @param time   The formatted time string.
     * @param format The format.
     * @return the date
     */
    public static Date string2Date(final String time, @NonNull final DateFormat format) {
        try {
            return format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * Formatted time string to the date.
     *
     * @param time       The formatted time string.
     * @param timeFormat The format.
     * @return the date
     */
    public static Calendar string2Calendar(final String time, @NonNull final String timeFormat) {
        return string2Calendar(time, new SimpleDateFormat(timeFormat));
    }

    /**
     * Formatted time string to the date.
     *
     * @param time   The formatted time string.
     * @param format The format.
     * @return the date
     */
    public static Calendar string2Calendar(final String time, @NonNull final DateFormat format) {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(format.parse(time));
            return calendar;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
           
时间做差
/**
     * 获取合适型两个时间差
     *
     * @param millis0   毫秒时间戳1
     * @param millis1   毫秒时间戳2
     * @param precision 精度
     *                  <p>precision = 0,返回null</p>
     *                  <p>precision = 1,返回天</p>
     *                  <p>precision = 2,返回天和小时</p>
     *                  <p>precision = 3,返回天、小时和分钟</p>
     *                  <p>precision = 4,返回天、小时、分钟和秒</p>
     *                  <p>precision &gt;= 5,返回天、小时、分钟、秒和毫秒</p>
     * @return 合适型两个时间差
     */
    public static int getFitTimeSpanV2(long millis0, long millis1, int precision) {
        return millis2FitTimeSpanV2(Math.abs(millis0 - millis1), precision);
    }

    @SuppressLint("DefaultLocale")
    public static int millis2FitTimeSpanV2(long millis, int precision) {
        if (millis <= 0) {
            return 0;
        }
        int[] unitLen = {86400000, 3600000, 60000, 1000, 1};
        long mode = 0;
        precision = Math.min(precision, 5);
        mode = millis / unitLen[precision];
        return (int) mode;
    }
           

小结

比起迅速开始编码工作,思考构思更重要。

思考不充分,迟早会给你带来返工。

程序员这份工作,总算很大的瓶颈。我时长想,这家伙跟我差不多,却为什么工资比我高辣么多。

是思维决定了你的层次。凡事多思考。方有沉淀。

这就是好多人常说的。10年经验,是10个1年经验,还是1个10年经验。

有成长方有沉淀。

没有经过思考的代码,是没有灵气的代码。

大道至简

,Linux的开创者,当初也只是因为一个想法。