• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to convert descriptive dates into Java Date format

 
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bother missed the jpa bit, so no ideas
 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to give John Jai credit too - I merely improved his solution slightly.
 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 235
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 235
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 235
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 235
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome, but it's not Robert but Rob.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic