UNIX或LINUX时间戳转时间

本文提供了两种不同的方法来实现从时间戳到日期的转换:一种是使用Oracle SQL语句进行转换,另一种是通过Java代码实现同样的功能。Oracle方法中利用了TO_DATE函数结合TZ_OFFSET函数来考虑时区偏移;而Java方法则是将时间戳乘以1000转换为Java中的毫秒单位,并使用SimpleDateFormat格式化输出。

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

ORACLE:

select TO_DATE('19700101', 'yyyymmdd') + 1481148929 / 86400 +
TO_NUMBER(SUBSTR(TZ_OFFSET(sessiontimezone), 1, 3)) / 24
from dual

JAVA:

Long timestamp = Long.parseLong("1481148929")*1000;

String date = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java util.Date(timestamp));

转载于:https://www.cnblogs.com/orac/p/6145006.html

Java中,可以使用`java.time`包中的类来将Linux时间戳换为可读的时间格式。Linux时间戳通常指的是从1970年1月1日(Unix纪元)开始的秒数(毫秒数)。下面是一个简单的换例子: ```java import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class LinuxTimestampConverter { public static void main(String[] args) { // 示例:Linux时间戳,单位是秒 long linuxTimestampInSeconds = 1619651600L; // 将秒换为毫秒 long linuxTimestampInMilliseconds = linuxTimestampInSeconds * 1000L; // 将时间戳换为Instant对象 Instant instant = Instant.ofEpochMilli(linuxTimestampInMilliseconds); // 获取系统默认的时区 ZoneId zoneId = ZoneId.systemDefault(); // 将Instant对象换为ZonedDateTime对象 ZonedDateTime zonedDateTime = instant.atZone(zoneId); // 格式化时间输出 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = zonedDateTime.format(formatter); System.out.println("换后的时间为: " + formattedDateTime); } } ``` 上述代码中,我们首先将Linux时间戳从秒换为毫秒,因为`Instant.ofEpochMilli`方法接受的是毫秒级别的时间戳。然后,我们创建了一个`Instant`对象,它代表了这一时刻的全球标准时间(UTC)。接着,我们使用默认时区创建了一个`ZonedDateTime`对象,这样可以得到具体时区的时间。最后,我们通过`DateTimeFormatter`对时间进行了格式化,以便输出易读的格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值