源程序如下:
public class TimeTool {
//2011/08/17 12:01:12.984851 1313554656851 1313554657851 1313583456851
public static void main(String[] args) {
TimeTool time=new TimeTool();
String timeStamp=null;
String date=null;
timeStamp = time.Date2Stamp("2011/08/17 12:01:13.984851");
System.out.println("原时间为="+timeStamp);
System.out.println("转换的时间戳为="+timeStamp);
date=time.Stamp2Date(timeStamp);
System.out.println("转换后的时间="+date);
}
public static String Date2Stamp(String date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SS");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("CEST")); //设置时区
Date d = null;
try {
d = simpleDateFormat.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long ts = d.getTime();
return String.valueOf(ts);
}
/**
* 将时间戳转换为日期
* @param stamp 时间戳
* @return 时间,返回格式为 yyyy-MM-dd-HH-mm-ss
*/
public static String Stamp2Date(String stamp){
String res;
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SS");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("CEST")); //设置时区
long lt=new Long(Long.parseLong(stamp));
Date date=new Date(lt);
res=simpleDateFormat.format(date);
return res;
}
}
输出结果:
原时间为=1313583457851
转换的时间戳为=1313583457851
转换后的时间=2011/08/17 12:17:37.851