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());