Java Date Parsing" a.m."和“p.m.” [英] Java Date Parsing "a.m." and "p.m."

查看:126
本文介绍了Java Date Parsing" a.m."和“p.m.”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用SimpleDateFormat,如何解析字符串:2013-05-23T09:18:07 pm.380 + 0000

Using SimpleDateFormat, how can you parse the String: "2013-05-23T09:18:07 p.m..380+0000"

我所有的SimpleDateFormat字符串都在pm部分绊倒。

All my SimpleDateFormat Strings are tripping up on the "p.m." part.

提前致谢。

编辑:我们无法控制进入的格式。

We have no control over the format coming in.

我试过:

"yyyy-MM-dd'T'HH:mm:ss a.a..SSSZ"

"yyyy-MM-dd'T'HH:mm:ss aaaa.SSSZ"

"yyyy-MM-dd'T'HH:mm:ss a.'m'..SSSZ"

"yyyy-MM-dd'T'HH:mm:ss a.'m.'.SSSZ"

"yyyy-MM-dd'T'HH:mm:ss a.'m..'SSSZ"

"yyyy-MM-dd'T'HH:mm:ss aa'm'..SSSZ"

"yyyy-MM-dd'T'HH:mm:ss aa'm.'.SSSZ"

"yyyy-MM-dd'T'HH:mm:ss aaa'..'SSSZ"

"yyyy-MM-dd'T'HH:mm:ss aaa.'.'SSSZ"

"yyyy-MM-dd'T'HH:mm:ss aaa'.'.SSSZ"


推荐答案

目前尚不清楚380 + 0000部分应该是,但您可以通过为 SimpleDateFormat DateFormatSymbols 来修复AM / PM部分C $ C>。这是一个例子:

It's not clear what the "380+0000" part is meant to be, but you can fix the AM/PM part, by setting the DateFormatSymbols for the SimpleDateFormat. Here's an example:

import java.util.*;
import java.text.*;

public class Test {
    public static void main(String[] args) throws Exception {
        String text = "2013-05-23T09:18:07 p.m..380+0000";
        String pattern = "yyyy-MM-dd'T'hh:mm:ss aa'.380+0000'";

        SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.US);
        format.setTimeZone(TimeZone.getTimeZone("UTC"));

        DateFormatSymbols symbols = format.getDateFormatSymbols();
        symbols = (DateFormatSymbols) symbols.clone();
        symbols.setAmPmStrings(new String[] { "a.m.", "p.m."});
        format.setDateFormatSymbols(symbols);

        Date date = format.parse(text);
        System.out.println(date);
    }
}  

我不知道你是否 在变异之前克隆 DateFormatSymbols - 不清楚,说实话...... 文档指出两种方式:

I don't know whether you have to clone the DateFormatSymbols before mutating it - it's not clear, to be honest... the documentation points two ways:


DateFormatSymbols对象是可复制的。获取DateFormatSymbols对象时,可以随意修改日期时格式化数据。例如,您可以将本地化的日期时间格式模式字符替换为您容易记住的字符。或者您可以将代表性城市更改为您最喜欢的城市。

DateFormatSymbols objects are cloneable. When you obtain a DateFormatSymbols object, feel free to modify the date-time formatting data. For instance, you can replace the localized date-time format pattern characters with the ones that you feel easy to remember. Or you can change the representative cities to your favorite ones.

鉴于它提到克隆,这表明您应该 clone - 但随后的段落建议不要:(

Given that it's mentioning cloning, that suggests you should clone - but then the subsequent paragraph suggests not :(

这篇关于Java Date Parsing" a.m."和“p.m.”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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