您的位置:首页 > 其它

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

2015-10-26 15:11 183 查看
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());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: