Java SimpleDateFormat am/pm

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16669973/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:36:43  来源:igfitidea点击:

Java SimpleDateFormat am/pm

javaparsingdate-formatsimpledateformat

提问by Alex Haider

I get an Date with format: MM-dd-yy hhmma zzz. The problem is that i only get an A for AM or P for PM. How can I parse a string with that format to a date object?

我得到一个日期格式:MM-dd-yy hhmma zzz。问题是我只得到 A 代表 AM 或 P 代表 PM。如何将具有该格式的字符串解析为日期对象?

  • Alex
  • 亚历克斯

回答by Muskan

回答by Nim

Simple, do a string replace of A->AM/P->PM before parsing. SimpleDateFormat does not support A/P for AM/PM.

很简单,在解析之前做一个字符串替换 A->AM/P->PM。SimpleDateFormat 不支持 AM/PM 的 A/P。

回答by Rene Enriquez

This pattern is working for me:

这种模式对我有用:

dd-MM-yyyy hh:mm:ss a

dd-MM-yyyy hh:mm:ss a

It prints the information in this format: 12-02-2016 03:50:07 PM

它以这种格式打印信息:12-02-2016 03:50:07 PM

回答by Krithika Jagannathan

dd-MM-yyyy (MM-stands for month). Please do not use MM in the time section of the DATE TIME hh:MM:ss.

DD- MM-YYYY(MM-代表月)。请不要在 DATE TIME hh: MM:ss的时间部分使用 MM 。

For example. It will display the date '15-02-2017 12:12:05' as '15-02-2017 12:02:05' instead of 12:12:05. Month replaced in the place of minutes.

例如。它会显示日期'15 - 02-2017 12时12分05'秒作为'15 -02-2017 12:02:05' ,而不是12点12分05秒。用月代替分钟。

  • hh - stands for 12 hours time format
  • HH - stands for 24 hours time format
  • hh - 代表 12 小时时间格式
  • HH - 代表 24 小时制时间格式

it is best to use 12 hours time format with a 'hh:mm a' to give clear picture on whether it is 'AM/PM'

最好使用带有 ' hh:mm a' 的12 小时时间格式,以便清楚地了解它是否是'AM/PM'