我可以Date()通过将long值放入Date()构造函数中将unix时间戳转换为对象.例如:我可以把它作为new Date(1318762128031).
但在那之后,我怎样才能从Date()对象中获取unix时间戳?
jac*_*bit 81
getTime()检索自1970年1月1日以来传递给构造函数的毫秒数.从那里获取Unix时间(相同,但在几秒钟内)应该不会太难.
Ped*_*ito 39
要获得unix时间戳,timestamp我们需要划分Date()
Date currentDate = new Date();
currentDate.getTime() / 1000;
// 1397132691
Run Code Online (Sandbox Code Playgroud)
或者干脆:
long unixTime = System.currentTimeMillis() / 1000L;
Run Code Online (Sandbox Code Playgroud)
bha*_*ker 22
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
public class Timeconversion {
private DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm", Locale.ENGLISH); //Specify your locale
public long timeConversion(String time) {
long unixTime = 0;
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+5:30")); //Specify your timezone
try {
unixTime = dateFormat.parse(time).getTime();
unixTime = unixTime / 1000;
} catch (ParseException e) {
e.printStackTrace();
}
return unixTime;
}
}
Run Code Online (Sandbox Code Playgroud)