当前时间时间戳转换为日期
//当前时间毫秒的时间戳转换为日期
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 );
这篇博客演示了如何将当前时间和秒级时间戳转换为日期,并进行格式化。通过使用Java的Date和SimpleDateFormat类,将时间戳转换为指定格式的字符串。接着,将字符串日期解析回Date对象,并进一步转换为LocalDateTime,展示了日期时间处理的基本操作。

777

被折叠的 条评论
为什么被折叠?



