首页 > 编程语言 > 详细

java 时间,时间戳互转

时间:2017-02-07 19:05:47      阅读:182      评论:0      收藏:0      [点我收藏+]
   /**
     * 时间转换成时间戳
     * @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;
}
 

 

java 时间,时间戳互转

原文:http://www.cnblogs.com/cc-robot/p/6375089.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!