时间戳与标准时间的相互转化

1.时间戳转化为标准时间
Unix时间戳(Unix timestamp),或称Unix时间(Unix time)、POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。但是时间戳转化为标准时间的时候,要用毫秒进行转换,所以给定的时间戳要乘以1000,转化为毫秒

private String fomatTime(String formatStr){
        int time = Integer.parseInt(formatStr);
        Pattern p = Pattern.compile("[\\d]+");
        Matcher m;
        boolean b = false;
        String sd;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        if(formatStr != null){
             m = p.matcher(formatStr);
             b = m.matches();//判断是否为0-9之间的数字
             if(b){
                 sd = sdf.format(new Date(time*1000L));//变为毫秒
             }else{
                 sd = formatStr;
             }
            return sd;
        }else{
            return formatStr;
        }
    }

2.标准时间转换为时间戳(毫秒)

SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );

     String time="2015-10-25 0:0:0";

     Date date = null;
    try {
        date = format.parse(time);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

 System.out.print("Format To times:"+date.getTime());
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值