java 在java中将日期转换为时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17482177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Date to time stamp in java
提问by Hymanyesind
I am trying to convert the date to timestamp
我正在尝试将日期转换为时间戳
Thu Jul 04 13:32:51 IST 2013
to timestamp value
Thu Jul 04 13:32:51 IST 2013
时间戳值
java.sql.Timestamp timeStampDate = new
Timestamp(user.getCreatedTime().getTime());
System.out.println("Today is " +timeStampDate);
but it gives the result as 2013-07-04 13:32:51.0
I need only the long value like 1372924971000.
但它给出了结果,因为2013-07-04 13:32:51.0
我只需要像 1372924971000 这样的长值。
回答by Kai
Call getTime()
on your Timestamp
object:
调用getTime()
您的Timestamp
对象:
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Timestamp object.
返回自 1970 年 1 月 1 日格林威治标准时间 00:00:00 以来的毫秒数,由此 Timestamp 对象表示。
In fact you don't have to create a Timestamp
first. user.getCreatedTime().getTime()
already gives you the long value which you want.
事实上,您不必创建Timestamp
第一个。user.getCreatedTime().getTime()
已经为您提供了您想要的长期价值。
回答by William Morrison
Call this functionon your timestamp object:
在时间戳对象上调用此函数:
timeStampDate.getTime()
As the doc states:
正如文档所述:
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Timestamp object.
返回自 1970 年 1 月 1 日格林威治标准时间 00:00:00 以来的毫秒数,由此 Timestamp 对象表示。
Fun fact: The origin date used to measure elapsed time is referred to as the epoch. Here for instance, January 1, 1970 is the epoch.
有趣的事实:用于测量经过时间的原始日期称为纪元。例如,这里的纪元是 1970 年 1 月 1 日。
You could actually just call:
你实际上可以调用:
user.getCreatedTime().getTime()
回答by Prateek
I guess you are looking for epoch time simple use
我猜你正在寻找纪元时间的简单使用
long epoch = System.currentTimeMillis()/1000;