wood burning stoves 2.0*
The moose likes Java in General and the fly likes How to convert descriptive dates into Java Date format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to convert descriptive dates into Java Date format" Watch "How to convert descriptive dates into Java Date format" New topic
Author

How to convert descriptive dates into Java Date format

Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi Java specialists,

I need a solution to convert the following date formats to Java Date format to be persisted into MySQL using JPA:

1st January 2008,
2nd February 2009,
3rd March 2010,
4th April 2011.

It does not appear that this can be done using SimpleDateFormat class from the examples available.

I have no problem coming up with persisting data in general into MySQL using JPA.

It is not difficult to write a conversion class to do this but wondering whether there is an easier way to do this.

Thanks a lot,

Jack
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
All used formats can be grouped and a method can be written to check each date against all formats. But check if the nd, th, rd, can be generally named something like 'xx' so that the parser ignores it.

Ragupathirajan Venkatesan
Ranch Hand

Joined: Aug 18, 2010
Posts: 62
Hi

I hope the above approach will cause the problem in some conditions. Specially when the date is less than 10. for example 08-08-2011(There is no guaranty that this is in dd-mm-yyyy or mm-dd-yyyy format). Hence write a method to parse the date along with send the format you want to convert. Please ignore if i am wrong
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

John Jai wrote:All used formats can be grouped and a method can be written to check each date against all formats. But check if the nd, th, rd, can be generally named something like 'xx' so that the parser ignores it.


You don't need to catch any exceptions if you use the other parse method:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

bother missed the jpa bit, so no ideas
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi Rob,

You have come to the rescue once again with the following implementation:



Output
parsedDate: 30/04/2009

Thank you very much,

Jack

In response to Wendy Gibbons’s suggestion, I do not write any SQL and run against MySQL. JPA does it on my behalf. Thanks all the same.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

You have to give John Jai credit too - I merely improved his solution slightly.
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Absolutely. Thank you to all those who have contributed to this post including John Jai.

I am still not clear on why exceptions occur with John Jai's solution and how your enhancement overcame it. Can anyone help me out on this?

Thanks again,

Jack
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
I have used the DateFormat:parse() method that throws the ParseException and Rob has used SimpleDateFormat:parse(String,ParsePosition) that does not throw the error as he mentioned. Check the given API docs.
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi John,

Thank you very much for you last clarification on the differences between the 2 classes. I will lookup Java API Doc to understand the reason for their difference behaviours.

Jack
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

DateFormat.parse(String) actually calls DateFormat.parse(String, ParsePosition); it throws a ParseException using the ParsePosition's error index if the result is null.
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Got it Rob,

This behaviour is consistent with other Java classes in general. I don't understand why the designer would have one class thowing exception while the other returns null even though they both achieve the same objective, but that is besides the point.

Crystal clear now. Thanks again,

Jack
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi All,

The suggestion by Jon Thai & Rob Spoor does not work when the date of the month is more than 1 digit. For instance where the string date is "22nd December 2011". Below is an enhancement intended to overcome this limitation but failed to work:


The value of publishedDate is null as opposed to 22-12-2911.

Any idea on where the problem is coming from? I am not clear on how line 2 - 7 work.

Thanks,

Jack
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

If I take that code and that example input String, I get successfully parsed dates for the third and fourth patterns.

As for the working of that code:
- Line 2 creates a ParsePosition object that will be shared for all parsing. A ParsePosition contains two important values: the current index and the index of where the last error occurred. This one's initialized with a current index of 0 (start of String), and the error index defaults to -1 (no error).
- Line 3 iterates over all of the formats.
- Lines 4 and 5 reset the ParsePosition, because it may have been modified by a call of line 7 of a previous iteration.
- Line 6 is comment
- Line 7 creates a SimpleDateFormat object with the current format, and tries to parse the input String to a Date. If it fails the ParsePosition's error index is updated and the parse method returns null. If it succeeds the ParsePosition's index is updated instead and the parse method returns the result.
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi Robert,

You are absolutely right again. I have over iterated (for loop) the date string format matching without breaking out from it. Below is the correct working code snippet:



I am crystal cleared again on how this simple piece of code works. Apologies for missed spelling John Jai's name.

Thanks again,

Jack
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

You're welcome, but it's not Robert but Rob.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to convert descriptive dates into Java Date format