本文以yyyyMMddHHmmss格式的字符串作为转换示例,如果有其他格式,请自行转换。

1.背景说明

今天维护一个功能,需要时间转换与计算。
原来的程序中时间以字符串格式存储,格式为:yyyyMMddHHmmss
数据库中的时间存储格式如下:

fssj  VARCHAR2(14) not null

实例:‘20180208120005’

2.功能需求

为了方便时间转换与计算,需要实现如下方法:

  • yyyymmddhhMMss类型的字符串 转换成 时间戳(毫秒)
  • 计算两个 yyyymmddhhMMss类型的字符串时间差(毫秒)
  • 时间戳(毫秒) 转换成 yyyymmddhhMMss类型的字符串
  • 计算 yyyymmddhhMMss类型的字符串millis毫秒 以后的 字符串

3.实现原理

主要通过SimpleDateFormat类进行转换:

//yyyymmddhhMMss-->timeMillis
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Long time = sdf.parse(time14).getTime();

//timeMillis-->yyyymmddhhMMss
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String time14 = sdf.format(new Date(millis));

4.全部代码

全部代码如下:

/**
 * 时间转换工具类(类名自己改)
 * @author hanchao
 *
 */
public class QxtMessageUtils{

    private static Logger LOGGER = Logger.getLogger(QxtMessageUtils.class);

    /**
     * 将yyyymmddhhMMss转换成时间戳(毫秒)
     * @return
     * @throws ParseException 
     */
    public static Long time14ToTimeMillis(String time14) throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = sdf.parse(time14);
        LOGGER.debug(time14 + "-->" + date.getTime());
        return date.getTime();
    }

    /**
     * 计算两个yyyymmddhhMMss的时间差(毫秒)
     * @param time14Start
     * @param time14End
     * @return
     * @throws ParseException 
     */
    public static Long timeMillisBetween(String time14Start,String time14End) throws ParseException{
        return time14ToTimeMillis(time14Start) - time14ToTimeMillis(time14End);
    }

    /**
     * 将时间戳(毫秒)转换成yyyymmddhhMMss
     * @param millis
     * @return
     */
    public static String timeMillisToTime14(Long millis){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String time14 = sdf.format(new Date(millis));
        LOGGER.debug(millis + "-->" +time14);
        return time14;
    }

    /**
     * yyyymmddhhMMss的时间增加millis毫秒
     * @param time14Start
     * @param millis
     * @return
     * @throws ParseException 
     */
    public static String time14AddTimeMillis(String time14Start,Long millis) throws ParseException{
        Long startMillis = time14ToTimeMillis(time14Start);
        Long endMills = startMillis + millis;
        String endTime14 = timeMillisToTime14(endMills);
        LOGGER.info(time14Start + " + " + millis + " --> " + endTime14);
        return endTime14;
    }
    //测试
    public static void main(String[] args) throws Exception{
        LOGGER.info(QxtMessageUtils.time14ToTimeMillis("20180208120005"));
        LOGGER.info(QxtMessageUtils.timeMillisToTime14(1519718616000L));
        LOGGER.info(QxtMessageUtils.timeMillisBetween("20180208120005", "20180208120100"));
        LOGGER.info(QxtMessageUtils.time14AddTimeMillis("20180208120005", (long) (60 * 1000)));
    }

}

测试结果:

2018-02-27 16:47:59 INFO  QxtMessageUtils:204 - 1518062405000
2018-02-27 16:47:59 INFO  QxtMessageUtils:205 - 20180227160336
2018-02-27 16:47:59 INFO  QxtMessageUtils:206 - -55000
2018-02-27 16:47:59 INFO  QxtMessageUtils:207 - 20180208120105