2020-08-21

时间:2022-07-23
本文章向大家介绍2020-08-21,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

java封装时间戳与时间的相互转换

public class TimeUtil {
    /**
     * 时间戳转换为时间
     * @param time
     * @return
     */
    public static String toDate(Long time){
        Date date = new Date();
        date.setTime(time);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    /**
     * 时间转换为时间戳
     * @param time
     * @return
     * @throws ParseException
     */
    public static Long toTimeStamp(String time) throws ParseException {
        //时间格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = format.parse(time);
        return date.getTime();
    }
}