将日期字符串解析为某个 Java 对象 [英] Parse Date String to Some Java Object

查看:33
本文介绍了将日期字符串解析为某个 Java 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个读取文件和处理数据的项目中工作.例如,我在那里处理日期:

I am working in a project that reads files and processes data. There I got to work with dates for example:

  1. 2012-01-10 23:13:26
  2. 2012 年 1 月 13 日

我找到了 Joda 包,这个包很有趣,但不知道它是否是最简单的.

I found the package Joda, kinda interesting package but don't know if it is the easiest around.

我能够将第一个示例解析为 DateTime 对象 (Joda) 正则表达式和字符串操作.(例如:将空格替换为-"并将其传递给构造函数.

I was able to parse the first example to a DateTime object (Joda) reg-ex and String manipulation. (Ex: by replacing the space by '-' and passing it to constructor.

new DateTime("2012-01-10 23:13:26".replace(' ', '-'))

我想它有效,但问题在于第二种格式.我如何使用这样的输入来提取一个对象,最好是一个 Joda 对象.我当然可以编写一个函数来将格式更改为 Joda 支持的格式,但想知道是否还有其他方法(甚至是一些本机 Java 库)来做到这一点.

I guess it worked, but the problem is with the second format. How can I use such an input to extract a an object, preferably a Joda object. I sure can write a function to change the format to what Joda supports, but was wondering if there would be some other way (even some native Java library) to do it.

如果有比 Joda 更好的东西,也请告诉我.

If there are any thing better than Joda out there, please let me know it as well.

谢谢.

推荐答案

使用 Joda-Time,看一下 DateTimeFormat;它允许解析您提到的两种日期字符串(以及几乎任何其他任意格式).如果您的需求更复杂,请尝试 DateTimeFormatterBuilder.

Using Joda-Time, take a look at DateTimeFormat; it allows parsing both kind of date strings that you mention (and almost any other arbitrary formats). If your needs are even more complex, try DateTimeFormatterBuilder.

解析#1:

DateTimeFormatter f = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime dateTime = f.parseDateTime("2012-01-10 23:13:26");

实际上 LocalDateTime 是一个更适合没有时区的日期时间类型:

actually LocalDateTime is a more appropriate type for a datetime without a time zone:

LocalDateTime dateTime = f.parseLocalDateTime("2012-01-10 23:13:26");

对于#2:

DateTimeFormatter f = DateTimeFormat.forPattern("MMMM dd, yyyy");
LocalDate localDate = f.parseLocalDate("January 13, 2012");

是的,就 Java date & 而言,Joda-Time 绝对是要走的路.时间处理问题.:)

And yes, Joda-Time is definitely the way to go, as far as Java date & time handling is concerned. :)

大多数人都会同意,Joda 是一个非常用户友好的库.比如之前我从来没有用 Joda 做过这种解析,但是从 API 上我只花了几分钟就搞明白了,然后写出来了.

As mostly everyone will agree, Joda is an exceptionally user-friendly library. For example, I had never done this kind of parsing with Joda before, but it took me just a few minutes to figure it out from the API and write it.

如果您使用的是 Java 8,在大多数情况下,您只需使用 java.time 而不是 Joda-Time.它包含了几乎所有来自 Joda 的好东西——或者它们的等价物.对于那些已经熟悉 Joda API 的人,Stephen Colebourne 的 Joda- 时间到 java.time 迁移指南 派上用场.

If you're on Java 8, in most cases you should simply use java.time instead of Joda-Time. It contains pretty much all the good stuff—or their equivalents—from Joda. For those already familiar with Joda APIs, Stephen Colebourne's Joda-Time to java.time migration guide comes in handy.

这里是上述示例的 java.time 版本.

Here are java.time versions of above examples.

解析#1:

DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.from(f.parse("2012-01-10 23:13:26"));

您无法将其解析为 ZonedDateTime 或 OffsetDateTime(它们是 Joda 的 DateTime 的对应物,在我的原始答案中使用),但这有点道理,因为解析的字符串中没有时区信息.

You cannot parse this into ZonedDateTime or OffsetDateTime (which are counterparts of Joda's DateTime, used in my original answer), but that kinda makes sense because there's no time zone information in the parsed string.

解析#2:

DateTimeFormatter f = DateTimeFormatter.ofPattern("MMMM dd, yyyy");
LocalDate localDate = LocalDate.from(f.parse("January 13, 2012"));

这里的 LocalDate 是最适合解析的类型(就像 Joda-Time 一样).

Here LocalDate is the most appropriate type to parse into (just like with Joda-Time).

这篇关于将日期字符串解析为某个 Java 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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