今天爱分享给大家带来unix时间戳的转换 Java实现【附代码】,希望能够帮助到大家。
Java时间转换成unix时间戳的方法
Java进行时间转换成unix timestamp的具体代码,供大家参考,具体内容如下
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author kongqz kongqingzhu@gmail.com
* @version 创建时间:2013-2-19 上午10:21:47
*/
public class TestUnixTime {
public static void main(String[] args) throws ParseException{
DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//1361325960
long epoch = df.parse("2013-02-20 10:06:00").getTime();
System.out.println("should be 1361325960 :"+epoch);
Date d=new Date();
String t=df.format(d);
epoch=df.parse(t).getTime()/1000;
System.out.println("t is :"+t+",unix stamp is "+epoch);
}
}