Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Date String to Some Java Object

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.

like image 781
Ziyan Junaideen Avatar asked Jan 13 '12 17:01

Ziyan Junaideen


People also ask

How do you parse a date to an object?

The parse() method takes a date string (such as "2011-10-10T14:48:00" ) and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. This function is useful for setting date values based on string values, for example in conjunction with the setTime() method and the Date object.

Can we convert string to date in java?

By default, Java dates are in the ISO-8601 format, so if we have any string which represents a date and time in this format, then we can use the parse() API of these classes directly.


2 Answers

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).

like image 155
Jonik Avatar answered Sep 18 '22 23:09

Jonik


SimpleDateFormat will parse dates into Java Date objects:

http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html

SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); // first example SimpleDateFormat format2 = new SimpleDateFormat("MMMMM dd,yyyy"); // second example  Date d1 = format1.parse( dateStr1 ); Date d2 = format2.parse( dateStr2 ); 
like image 34
chubbsondubs Avatar answered Sep 20 '22 23:09

chubbsondubs