Java-用秒旁边的AM / PM解析日期(无空格) [英] Java - parse date with AM/PM next to seconds (no space)

查看:113
本文介绍了Java-用秒旁边的AM / PM解析日期(无空格)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析以下日期: 2001年12月12日晚上11:59:59

I am trying to parse the following kind of date: Dec 12 2001 11:59:59PM.

如果AM / PM不是秒,我将使用以下模式: MMM dd yyyy HH:mm:ss a

If the AM/PM wasn't next to the seconds, I would use the following pattern: MMM dd yyyy HH:mm:ss a.

但是,使用模式 MMM dd yyyy HH:mm:ssa 忽略AM / PM(因此始终解释为AM)。

However the AM/PM is ignored with the pattern MMM dd yyyy HH:mm:ssa (and is therefore always interpreted as AM).

我正在尝试使用 SimpleDateFormat 。我试图指定一个无用的语言环境。

I am trying to use a SimpleDateFormat. I tried to specify a locale to no avail.

是否可以使用 SimpleDateFormat 来做到这一点?替代方法/外部处理? SimpleDateFormat 使我特别感兴趣,因为它在 @JsonFormat 批注的pattern属性中使用。

Is this possible using a SimpleDateFormat or do I need to use an alternative method/external processing? The SimpleDateFormat particularly interests me due to its use in the pattern attribute of the @JsonFormat annotation.

谢谢。

推荐答案

使用AM / PM需要12个小时 hh 而不是24小时 HH

with AM/PM you want 12 hours hh instead of 24 hours HH.

hh:mm:ss''a

k / K / h / H 影响 a 也是如此,现在所有内容都可以使用 ssa

As k/K/h/H influence a too, now everything might work with ssa.

如果 ssa 仍然是有问题的(看似是一个错误),请尝试使用空文字串(单引号)分隔字母。

If ssa is still problematic (seemingly a bug), try separating the letters by an empty literal string (single quotes).

以下作品:

The following works:

hh:mm:ssa

这篇关于Java-用秒旁边的AM / PM解析日期(无空格)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆