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

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

问题描述

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


  1. 2012-01-10 23:13:26

  2. 2012年1月13日

我发现包Joda,有趣的包,但不知道是否最简单



我能够将第一个示例解析为DateTime对象(Joda)reg-ex和String操作。 (例如:用' - '替换空格并将其传递给构造函数。

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

但是问题是第二种格式,我如何使用这样一个输入来提取一个对象,最好是一个Joda对象,我肯定可以编写一个函数来将格式更改为Joda支持的格式,但是想知道是否会一些其他的方法(甚至一些本地Java库)来做。



如果有比Joda更好的东西,请让我知道。 p>

谢谢。

解决方案

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



解析#1:

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

编辑:实际 LocalDateTime 是没有时区的日期时间的更合适的类型:

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

而#2:

  DateTimeFormatter f = DateTimeFormat.forPattern(MMMM dd,yyyy); 
LocalDate localDate = f.parseLocalDate(2012年1月13日);

是的,Joda-Time绝对是要走的路,就Java date&关注时间处理。 :)



大多数人都同意,Joda是一个非常用户友好的图书馆。例如,我以前从来没有和Joda一起进行过这样的解析,但是花了我几分钟的时间从API中解出来并写下来。



更新(2015)



如果您使用的是 Java 8 大多数情况下,您应该使用 java.time 而不是Joda-Time。它包含几乎所有的好东西或他们的等同物 - 来自Joda。对于已经熟悉Joda API的用户,Stephen Colebourne的 Joda - 时间到java.time迁移指南很方便。



以下是上述示例的java.time版本。



解析#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的对应单元,用于我的原始答案),但是这是有道理的,因为解析的字符串中没有时区信息。



解析#2:



DateTimeFormatter f = DateTimeFormatter.ofPattern(MMMM dd,yyyy);

  
LocalDate localDate = LocalDate.from(f.parse(2012年1月13日));

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

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. January 13, 2012

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

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(' ', '-'))

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.

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

Thank you.

解决方案

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.

To parse #1:

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

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

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

And for #2:

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

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

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.

Update (2015)

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.

Here are java.time versions of above examples.

To parse #1:

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

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.

To parse #2:

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

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

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

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