java把距离1970年 1月1日 的秒数 转换成对应的日期

本文介绍两种将距离1970年1月1日的秒数转换为日期的方法:使用Date类的setTime方法和GregorianCalendar类的setTimeInMillis方法。通过实例演示如何将长整型时间戳转换为特定格式的日期字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值