如何在java中使用AM / PM获取时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18320420/
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
How to get Timestamp with AM/PM in java
提问by muthu kumar
I have a date as String , which needs to be converted in to Time Stamp with AM/PM . I tried the below way, I'm getting the proper date format but didn't get in AM/PM.
我有一个日期作为 String ,需要将其转换为带有 AM/PM 的时间戳。我尝试了以下方式,我得到了正确的日期格式,但没有进入 AM/PM。
Can any one please help ?
任何人都可以帮忙吗?
code Snippet:
代码片段:
String dateString = "10/10/2010 11:23:29 AM";
SimpleDateFormat sfdate = new SimpleDateFormat("MM/dd/yyy HH:mm:ss a");
Date date = new Date();
date = sfdate.parse(dateString);
System.out.println(new Timestamp(date.getTime()));
Which gives me the output as below :
这给了我如下输出:
2010-10-10 11:23:29.0
But I needs it like this
但我需要这样
2010-10-10 11:23:29.00000000 AM
Kindly help me please.
请帮助我。
回答by jgm
Try:
尝试:
System.out.println(sfdate.format(date));
As your last line rather than the one that you have at current.
作为你的最后一行,而不是你目前拥有的那一行。
回答by rocketboy
Why create a timestamp ? When you can just :
为什么要创建时间戳?当你可以:
SimpleDateFormat sfdate = new SimpleDateFormat("MM/dd/yyy HH:mm:ss a");
Date date = new Date();
date = sfdate.parse(dateString);
System.out.println(sfdate.format(date) );
Output:
输出:
10/10/10 11:23:29 AM
回答by femtoRgon
Timestamp.toString()
prints to a specific format: yyyy-mm-dd hh:mm:ss.fffffffff
. The Timestamp object itself should be correct, if that's all you are looking for.
Timestamp.toString()
打印为特定格式:yyyy-mm-dd hh:mm:ss.fffffffff
. Timestamp 对象本身应该是正确的,如果这就是您要查找的全部内容的话。
If you then want to define another format in order to print it as you like, that would require you to format Date
object, using an appropriate pattern for the output format you are looking for.
如果您想定义另一种格式以便按您的喜好打印它,则需要您格式化Date
对象,使用适合您正在查找的输出格式的适当模式。
回答by Joe
What you're seeing is the result of Timestamp.toString()
. The actual valuein the Timestamp object instance is valid.
你看到的是结果Timestamp.toString()
。Timestamp 对象实例中的实际值是有效的。
If you're getting an error in a subsequent SQL operation, please post that error along with the code you're using.
如果您在后续 SQL 操作中遇到错误,请将该错误与您使用的代码一起发布。