java 中把 已知的距离1970年 1月1日 的秒数 转化 为相应的日期 的实现方式
第一种方式:
采用 Date 类的 setTime( Long time ) 方法
public class Timetest {
public static void main(String[] args) {
//秒
long second = 1509412775L;
Date d = new Date();
d.setTime(second *1000);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(d));
}
}
结果如下:
2017-10-31
第二种方式:
使用 GregorianCalendar 类的 setTimeInMillis( Long time ) 方法
public class Timetest {
public static void main(String[] args) {
//秒
long second = 1509412775L;
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis(second * 1000);
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd ");
System.out.print( format.format(gc.getTime()));
}
}
结果如下:
2017-10-31