class="java">package test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class Test {
public static void main(String[] args) throws Exception {
t1();
t2();
}
/**
* 格式化 date 对象,返回字符串
*/
public static void t1(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
String res = sdf.format(new Date());
System.out.println(res);
//输出结果:2013-12-10 16:32
}
/**
* 解析字符串日期,返回 date 对象
* @throws ParseException
*/
public static void t2() throws ParseException{
Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2013-12-10");
System.out.println(date);
//输出结果:Tue Dec 10 00:00:00 CST 2013
//getTime() 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来,此 Date 对象表示的毫秒数
System.out.println(date.getTime()/1000);
//输出结果:1386604800
}
}
?