Java/ 从 long 到 SimpleDateFormat 再到时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10590880/
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
Java/ From long to SimpleDateFormat and to Timestamp
提问by Adam Sh
I wrote a function that got a Timestamp (year, month, day, hour, min and sec) as a parameter.
我写了一个函数,它以时间戳(年、月、日、小时、分钟和秒)作为参数。
I just now find that instead of getting a Timestamp type, I got a long type - after the next stpes:
我刚刚发现不是得到 Timestamp 类型,而是得到了 long 类型 - 在下一个 stpes 之后:
The constructor is:
构造函数是:
protected SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
and then:
接着:
long qu = dateFormat.parse("24.12.2011 18:54:23").getTime();
I try to find how to convert the qu, back to SimpleDateFormat and then to Timestamp, or directly: from long to Timestamp (But where long was created after these two steps
).
我试图找到如何将 qu,转换回 SimpleDateFormat 然后转换为 Timestamp,或直接:从 long 转换为 Timestamp (But where long was created after these two steps
)。
eariler, I ask how to convert long to Timestamp.The answer was given is correct, but Im afraid that this is not the case when I convert it from SimpleDateFormat to long (the function doesn't work after the change).
早些时候,我问如何将 long 转换为 Timestamp。给出的答案是正确的,但是当我将其从 SimpleDateFormat 转换为 long 时,恐怕情况并非如此(该函数在更改后不起作用)。
Thanks.
谢谢。
回答by GriffeyDog
This works for me:
这对我有用:
public class Test {
public static void main(String... args) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
try {
long qu = dateFormat.parse("24/12/2011 18:54:23").getTime();
Timestamp ts = new Timestamp(qu);
System.out.println(ts);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Outputs: 2011-12-24 18:54:23.0
输出:2011-12-24 18:54:23.0