Java时间戳转换为日期

这篇博客演示了如何将当前时间和秒级时间戳转换为日期,并进行格式化。通过使用Java的Date和SimpleDateFormat类,将时间戳转换为指定格式的字符串。接着,将字符串日期解析回Date对象,并进一步转换为LocalDateTime,展示了日期时间处理的基本操作。
摘要由CSDN通过智能技术生成

当前时间时间戳转换为日期

//当前时间毫秒的时间戳转换为日期
Date millisecondDate= new Date(System.currentTimeMillis());
//格式化时间
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String millisecondStrings = formatter.format(millisecondDate);
System.out.println(millisecondStrings);

//当前时间秒的时间戳转换为日期

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//字符串类型的秒级时间戳
String currentTime = "1657519062";
Long date = Long.valueOf(currentTime);
//String类型
String date_time = formatter.format(new Date(date*1000L));
System.out.println(date_time);
//Date类型
Date times=formatter.parse(date_time);
System.out.println(times);
//Date转为LocalDateTime类型
Instant instant = times.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();

System.out.println(localDateTime );

  • 10
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值