将10位和13位时间戳转换为时间

yy-MM-dd hh:mm:ss是12小时制格式。

yy-MM-dd HH:mm:ss是24小时制格式。

 * 将10 or 13 位时间戳转为时间字符串  
 * convert the number 1407449951 1407499055617 to date/time format timestamp  
 */  
public static String timestamp2Date(String str_num) {  
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    if (str_num.length() == 13) {  
        String date = sdf.format(new Date(toLong(str_num)));  
        LogUtil.d(Constant.TAG + "将13位时间戳:" + str_num + "转化为字符串:", date);  
        return date;  
    } else {  
        String date = sdf.format(new Date(toInt(str_num) * 1000L));  
        LogUtil.d(Constant.TAG + "将10位时间戳:" + str_num + "转化为字符串:", date);  
        return date;  
    }  
}  
   /** 
     * String转long 
     * 
     * @param obj 
     * @return 转换异常返回 0 
     */  
    public static long toLong(String obj) {  
        try {  
            return Long.parseLong(obj);  
        } catch (Exception e) {  
        }  
        return 0;  
    } 
  /** 
    * 对象转整 
    * 
    * @param obj 
    * @return 转换异常返回 0 
    */  
   public static int toInt(Object obj) {  
       if (obj == null)  
           return 0;  
       return toInt(obj.toString(), 0);  
   } 

 

转载于:https://my.oschina.net/u/2391658/blog/894699

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值