java中如何将Timestamp转换为毫秒数
我写了一个把当前时间转换为毫秒数的例子,你参考一下,我这运行没问题:
package test;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author Administrator
*当前时间转换为毫秒数
*/
public class DeclareTimer {
public static void main(String[] args) throws ParseException {
//获取当前时间
Timestamp t = new Timestamp(new Date().getTime());
System.out.println("当前时间:"+t);
//定义时间格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
String str = dateFormat.format(t);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmm");
//此处转换为毫秒数
long millionSeconds = sdf.parse(str).getTime();// 毫秒
System.out.println("毫秒数:"+millionSeconds);
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Cat {
public static void main(String[] args) throws ParseException {
String str = "201104141302";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmm");
long millionSeconds = sdf.parse(str).getTime();//毫秒
System.out.println(millionSeconds);
}
}
-------------------
1302757320000
2014-02-10