Java 13位时间戳截取时分秒方法
要截取Java 13位时间戳的时分秒,可以使用java.time包中的Instant和LocalTime类来完成。以下是一个示例代码:
import java.time.Instant;
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
long timestamp = 1599537061000L; // Java 13位时间戳
Instant instant = Instant.ofEpochMilli(timestamp);
LocalTime time = LocalTime.ofInstant(instant, java.time.ZoneId.systemDefault());
int hour = time.getHour();
int minute = time.getMinute();
int second = time.getSecond();
System.out.println('时: ' + hour);
System.out.println('分: ' + minute);
System.out.println('秒: ' + second);
}
}
在上面的示例代码中,我们首先使用Instant.ofEpochMilli()方法将13位时间戳转换为Instant对象。然后,使用LocalTime.ofInstant()方法将Instant对象转换为LocalTime对象,并指定时区为系统默认时区。最后,使用getHour()、getMinute()和getSecond()方法从LocalTime对象中获取时、分、秒。
原文地址: http://www.cveoy.top/t/topic/qaot 著作权归作者所有。请勿转载和采集!