java 时间,时间戳互转


   /**
     * 时间转换成时间戳
     * @param time
     * @return
     */
    public static long dateToTimestamp(String time){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = simpleDateFormat.parse(time);
            long ts = date.getTime() / 1000;
            return ts;
        } catch (ParseException e) {
            return 0;
        }
    }
注:一定要写try, catch,不然会报unhandled exception: java.text.ParseException的错误

/**
* 时间戳转时间(11位时间戳)
* @param time
* @return
*/
public static String timestampToDate(long time) {
String dateTime;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMAT_SEC_FULL);
long timeLong = Long.valueOf(time);
dateTime = simpleDateFormat.format(new Date(timeLong * 1000L));
return dateTime;
}
 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM