1. 程式人生 > >Java 時間轉換成unix時間戳

Java 時間轉換成unix時間戳

java進行時間轉換成unix timestamp

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author kongqz [email protected]
 * @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); 
		
		
		
	}

}