/**
*时间戳转换成日期格式字符串
*@paramseconds精确到秒的字符串
*@paramformatStr
*@return
*/
publicstaticStringtimeStamp2Date(Stringseconds,Stringformat){
if(seconds==null||seconds.isEmpty()||seconds.equals("null")){
return"";
}
if(format==null||format.isEmpty())format="yyyy-MM-ddHH:mm:ss";
SimpleDateFormatsdf=newSimpleDateFormat(format);
returnsdf.format(newDate(Long.valueOf(seconds+"000")));
}
/**
*日期格式字符串转换成时间戳
*@paramdate字符串日期
*@paramformat如:yyyy-MM-ddHH:mm:ss
*@return
*/
publicstaticStringdate2TimeStamp(Stringdate_str,Stringformat){
try{
SimpleDateFormatsdf=newSimpleDateFormat(format);
returnString.valueOf(sdf.parse(date_str).getTime()/1000);
}catch(Exceptione){
e.printStackTrace();
}
return"";
}
/**
*取得当前时间戳(精确到秒)
*@return
*/
publicstaticStringtimeStamp(){
longtime=System.currentTimeMillis();
Stringt=String.valueOf(time/1000);
returnt;
}
//输出结果:
//timeStamp=1417792627
//date=-12-0523:17:07
//1417792627
publicstaticvoidmain(String[]args){
StringtimeStamp=timeStamp();
System.out.println("timeStamp="+timeStamp);
Stringdate=timeStamp2Date(timeStamp,"yyyy-MM-ddHH:mm:ss");
System.out.println("date="+date);
StringtimeStamp2=date2TimeStamp(date,"yyyy-MM-ddHH:mm:ss");
System.out.println(timeStamp2);
}
或者publicstaticvoidmain(String[]args){
StringstrDate="1328007600000";
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd");
Stringsd=sdf.format(newDate(Long.parseLong(strDate)));
System.out.println(sd);
}
如果觉得《java将时间戳转换成字符串_java 时间戳与日期字符串相互转换详解》对你有帮助,请点赞、收藏,并留下你的观点哦!