tags:

views:

585

answers:

2

Hello, How to specify the format string to convert the date alone from string. In my case, only the date part is relevant

Constructing it as DateTime fails:

String dateString = "2009-04-17";
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
DateTime dateTime = formatter.parseDateTime(dateString);

with error java.lang.IllegalArgumentException: Invalid format: "2011-04-17" is too short

Probably because I should use LocalDate instead. But, I do not see any formatter for LocalDate . What is the best way to convert String dateString = "2009-04-17"; into LocalDate (or something else if that is not the right representation)

thanks...

+5  A: 

You're probably looking for LocalDate(Object). It's a bit confusing since it takes a generic Object, but the docs indicate that it will use a ConverterManager that knows how to handle a String if you give it one.

Hank Gay
It certainly is a potentially confusing Javadoc entry.
hbunny
thanks a lot for the pointer... I'm bit confused how to use ConverterManager. How do I pass the string to it? Do I need need to add a partial converter to it? but it says by default it accepts string for partial convert. If you could give an example, it would be great help.. thanks
bsreekanth
Sorry my answer wasn't clear enough, but it looks like you figured it out. I did indeed mean `final LocalDate myDate = new LocalDate("2010-04-28");`
Hank Gay
A: 

Apparently, it was easy.. the LocalDate takes string through the constructor..
so.. LocalDate dt = LocalDate("2009-04-17")

bsreekanth