自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);
答案 0 :(得分:1)
请参阅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");
答案 1 :(得分:0)
简单地将Java millis除以1000