用于处理Unix Time时间的工具类

用于处理Unix Time时间的工具类
import java.util.Calendar;
import java.util.Date;

/**
 * 该类是DateUtil的改进类, 专门用于处理Unix Time时间, 即格式为"yyyy-MM-dd HH:mm:ss". 
 */
public final class UnixTime {

    static ThreadLocal<Calendar> CalendarLocal = new ThreadLocal<Calendar>() {

        @Override
        protected Calendar initialValue() {
            return Calendar.getInstance();
        }

    };

    /**
     * 标记各个字段的起始位置; 
     */
    static interface Index {
        /* yyyy-MM-dd HH:mm:ss */
        int[] YEAR = { 0, 4 };
        int[] MONTH = { 5, 7 };
        int[] DAY = { 8, 10 };
        int[] HOUR = { 11, 13 };
        int[] MINUTE = { 14, 16 };
        int[] SECOND = { 17, 19 };
    }

    /**
     * 解析日期字串, 返回各个时间段的数值. 依次是年, 月, 日, 时, 分, 秒.
     * 
     * @param val
     *            日期字串, 固定格式为"yyyy-MM-dd HH:mm:ss"
     * @return 解析后的各个时间段的数值数组.
     */
    public static int[] extract(String val) {
        int[] vals = new int[6];
        int len = val.length();
        if (len >= Index.YEAR[1]) {
            vals[0] = Integer.parseInt(val.substring(Index.YEAR[0], Index.YEAR[1]), 10);
        }
        if (len >= Index.MONTH[1]) {
            vals[1] = Integer.parseInt(val.substring(Index.MONTH[0], Index.MONTH[1]), 10) - 1;
        }
        if (len >= Index.DAY[1]) {
            vals[2] = Integer.parseInt(val.substring(Index.DAY[0], Index.DAY[1]), 10);
        }
        if (len >= Index.HOUR[1]) {
            vals[3] = Integer.parseInt(val.substring(Index.HOUR[0], Index.HOUR[1]), 10);
        }
        if (len >= Index.MINUTE[1]) {
            vals[4] = Integer.parseInt(val.substring(Index.MINUTE[0], Index.MINUTE[1]), 10);
        }
        if (len >= Index.SECOND[1]) {
            vals[5] = Integer.parseInt(val.substring(Index.SECOND[0], Index.SECOND[1]), 10);
        }
        return vals;
    }

    /**
     * 解析日期对象, 返回各个时间段的数值. 依次是年, 月, 日, 时, 分, 秒.
     * 
     * @param val
     *            日期字串, 固定格式为"yyyy-MM-dd HH:mm:ss"
     * @return 解析后的各个时间段的数值数组.
     */
    public static int[] extract(Date val) {
        int[] vals = new int[6];
        Calendar cal = CalendarLocal.get();
        cal.setTime(val);
        vals[0] = cal.get(Calendar.YEAR);
        vals[1] = cal.get(Calendar.MONTH);
        vals[2] = cal.get(Calendar.DAY_OF_MONTH);
        vals[3] = cal.get(Calendar.HOUR_OF_DAY);
        vals[4] = cal.get(Calendar.MINUTE);
        vals[5] = cal.get(Calendar.SECOND);
        return vals;
    }

    /**
     * 解析日期字串, 返回日期对象.
     * 
     * @param val
     *            日期字串, 固定格式为"yyyy-MM-dd HH:mm:ss"
     * @return
     */
    public static Date parse(String val) {
        int[] vals = extract(val);
        Calendar cal = CalendarLocal.get();
        cal.set(vals[0], vals[1], vals[2], vals[3], vals[4], vals[5]);
        return cal.getTime();
    }

    /**
     * 格式日期对象.
     * 
     * @param date
     * @return
     *            日期字串, 固定格式为"yyyy-MM-dd HH:mm:ss"
     */
    public static String format(Date date) {
        int[] vals = extract(date);
        return String.format("%04d-%02d-%02d %02d:%02d:%02d", vals[0], vals[1] + 1, vals[2], vals[3], vals[4], vals[5]);
    }

    /**
     * 格式日期对象.
     * 
     * @param date
     * @return
     *            日期字串, 固定格式为"yyyy-MM-dd"
     */
    public static String formatDate(Date date) {
        int[] vals = extract(date);
        return String.format("%04d-%02d-%02d", vals[0], vals[1] + 1, vals[2]);
    }

}

猜你喜欢

转载自qiaoshi.iteye.com/blog/2183094
今日推荐