Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Features new in Java 8 and the fly likes String to joda LocalDate fromat of Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Features new in Java 8
Bookmark "String to joda LocalDate fromat of "dd-MMM-yy"" Watch "String to joda LocalDate fromat of "dd-MMM-yy"" New topic
Author

String to joda LocalDate fromat of "dd-MMM-yy"

sahar eb
Ranch Hand

Joined: May 06, 2009
Posts: 38
Hi,
I am using joda 2.2. I have this String as input "16-Mar-05" and I want to convert it to a joda localDate variable in the format of "16-Mar-05". How can I do that?
please note that I don't want it printed, I need it in a variable.
here is my method and options I have tried, but they return me this: 2005-03-16 instead of this: "16-Mar-05".


Thank you so much!
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2165
    
  47
I have this String as input "16-Mar-05" and I want to convert it to a joda localDate variable in the format of "16-Mar-05". How can I do that?

LocalDate doesn't store a given format it just represents a date without a time zone. How the date is stored internally is not relevant you just need to know it represents the value you used during creation. If you want to output the LocalDate object as a string in a particular format you create a suitable DateTimeFormatter and use one of the print() methods or call the LocalDate object's toString() method passing in the pattern to use. For instance:
Outputs:

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

sahar eb wrote:... convert it to a joda localDate variable in the format of "16-Mar-05". How can I do that?
please note that I don't want it printed, I need it in a variable.

That's not possible; the fact that you ask that question indicates that you have a misunderstanding of what a LocalDate object holds exactly.

A LocalDate object hold just a date value, just like an int variable only holds a number. It doesn't know anything about the format of the date. So you cannot have "a LocalDate object in the format 16-Mar-05".

The only thing that knows about formats is the DateTimeFormatter object, which you use to format (which means: convert to a string) the LocalDate object.

Ofcourse you can format the LocalDate object and store the result in a String, but you can't store a date in a LocalDate object in a certain format - simply because the LocalDate object doesn't know anything about formats.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String to joda LocalDate fromat of "dd-MMM-yy"
 
Similar Threads
How to parse a user-entered Date into a Date or Calendar object
epoch is wrong
Calendar and Scheduling
awkward date string
Error in Date format transform