时间戳格式转换

java时间与时间戳相互转换

时间转换成时间戳
1
2
3
4
5
6
7
8
public static String dateToStamp(String s) throws ParaseException{
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(s);
long ts = date.getTime();
res = String.valueOf(ts);
return res;
}
时间戳转换成时间
1
2
3
4
5
6
7
8
public static String dateToStamp(String s){
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long lt = new Long(s);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
return res;
}