SimpleDateFormat忽略字符 [英] SimpleDateFormat Ignore Characters

查看:66
本文介绍了SimpleDateFormat忽略字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SimpleDateFormat ,并且从两个不同的来源获得结果.一种来源使用"yyyy-MM-dd HH:mm:ss" 格式,另一种来源使用"yyyy-MM-ddTHH:mm:ssZ" .我对从第二种格式中获取时区("Z"值)不感兴趣,有没有办法在不使用其他格式字符串的情况下获得这些时间?会忽略中间字符以及's'之后的字符的东西.

I am using a SimpleDateFormat and I am getting results from two different sources. One source uses the format "yyyy-MM-dd HH:mm:ss", while the other uses "yyyy-MM-ddTHH:mm:ssZ". I am not interested in obtaining the time zone ('Z' value) from the second format, is there a way I can obtain these times without using different format strings? Something that will ignore the middle character as well as the characters after 'ss'.

推荐答案

最干净,最清晰的解决方案是,您是否可以将字符串与两个来源分开,并对每个来源使用适当的格式化程序.

The cleanest and clearest solution is if you can separate the strings from the two sources and use an appropriate formatter for each.

您可能会考虑的另一种方法是尝试一下"以确定您拥有的格式并根据该格式选择格式化程序.例如 if(result.contains("T")&&result.endsWith("Z"))).

Another approach that you might consider is "taking a taste" to determine which format you’ve got and pick the formatter based on that. For example if (result.contains("T") && results.endsWith("Z")).

由于您询问要避免使用不同格式的字符串,所以这也是可能的:

Since you asked about avoiding different format strings, that is possible too:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd[ ]['T']HH:mm:ss[X]");

    System.out.println(LocalDateTime.parse("2015-11-06 21:23:18", formatter));
    System.out.println(LocalDateTime.parse("2018-08-25T08:18:49Z", formatter));

此代码段的输出为:

2015-11-06T21:23:18
2018-08-25T08:18:49

我建议您避免使用 SimpleDateFormat 类.它早已过时,而且非常麻烦.相反,我建议您使用java.time(现代的Java日期和时间API).感觉好多了.

I recommend you avoid the SimpleDateFormat class. It is long outdated and notoriously troublesome. Instead I recommend you use java.time, the modern Java date and time API. It’s so much nicer to work with.

方括号表示格式的可选部分.该格式还将接受一个字符串,该字符串的中间有一个空格和 T ,而其中没有任何一个.对于大多数目的,我建议我们可以接受.您不能使用 SimpleDateFormat 玩类似的技巧,它不接受方括号或可选部分的任何其他语法.

The square brackets denote optional parts of the format. The format will accept also a string that has both a space and a T in the middle, and one that hasn’t got any of them. For most purposes I suggest that we can live with that. You cannot play a similar trick with SimpleDateFormat, it does not accept square brackets or any other syntax for optional parts.

我不愿意忽略第二个字符串中的偏移量,而只是因为您说过想要这么做而感到不满意.我显然希望使用以下稍长的解决方案:

I am not happy about ignoring the offset in the second string and doing that only because you said you wanted to. I’d clearly prefer the following just slightly longer solution:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd[ ]['T']HH:mm:ss[X]")
            .withZone(ZoneOffset.UTC);

    System.out.println(OffsetDateTime.parse("2015-11-06 21:23:18", formatter));
    System.out.println(OffsetDateTime.parse("2018-08-25T08:18:49Z", formatter));

输出是相同的,只是现在有偏移量:

Output is the same, only now with offset:

2015-11-06T21:23:18Z
2018-08-25T08:18:49Z

链接: Oracle教程:日期时间解释如何使用 java.time .

这篇关于SimpleDateFormat忽略字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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