gpt4 book ai didi

java - 将时间戳转换为自 Unix 纪元以来的秒数

转载 作者:行者123 更新时间:2023-11-30 11:27:30 29 4
gpt4 key购买 nike

如何将时间戳转换为自 Unix 纪元以来的秒数?我只是将时间戳转换为 UTC 时间戳。现在我想将这个时间戳转换成秒。

Calendar currenttime = Calendar.getInstance();
System.out.println("current: "+currenttime.getTime());

//TIMESTAMP UTC/GMT
TimeZone z = currenttime.getTimeZone();
int offset = z.getRawOffset();
if(z.inDaylightTime(new Date())){
offset = offset + z.getDSTSavings();
}
int offsetHrs = offset / 1000 / 60 / 60;
int offsetMins = offset / 1000 / 60 % 60;
currenttime.add(Calendar.HOUR_OF_DAY, (-offsetHrs));
currenttime.add(Calendar.MINUTE, (-offsetMins));
System.out.println("GMT CURRENT TIME: "+currenttime);

最佳答案

参见 http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Date.html#getTime()

它表示返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象所代表的毫秒数。

根据你的代码

currenttime.getTime().getTime() / 1000

Calendar currenttime = Calendar.getInstance();
System.out.println("current: "+currenttime.getTime());

//TIMESTAMP UTC/GMT
TimeZone z = currenttime.getTimeZone();
int offset = z.getRawOffset();
if(z.inDaylightTime(new Date())){
offset = offset + z.getDSTSavings();
}
int offsetHrs = offset / 1000 / 60 / 60;
int offsetMins = offset / 1000 / 60 % 60;
currenttime.add(Calendar.HOUR_OF_DAY, (-offsetHrs));
currenttime.add(Calendar.MINUTE, (-offsetMins));
System.out.println("GMT CURRENT TIME: "+currenttime);

加上

System.out.println("This is : " + currenttime.getTime().getTime() / 1000 + " seconds");

关于java - 将时间戳转换为自 Unix 纪元以来的秒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19311060/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com