Formatting a Date Using the Joda Time Library

In the java class, I get the date as the string say "renewDate" from the datepicker-input form in mm / dd / yyyy.

When I try to update code using joda time library

DateTime expireDate = new DateTime(renewDate);
// i get error at above line
updateOrganization.setRenewdate(expireDate.toDate());
organizationDAO.update(updateOrganization);

but if I format the date in the form ie, from mm / dd / yyyy to yyyy-mm-dd and send it to the java class, this will work fine.

How can I format a date from mm / dd / yy to yyyy-mm-dd in a Java class. Input is Stringformat.

+5
source share
2 answers

The list of valid formats for the constructor used is described in detail in javadoc ISODateTimeFormat , which does not include "mm / dd / yyyy":

datetime = | -
 time = 'T' []
 date-opt-time = date-element ['T' [time-element] [offset]]
 date-element = std-date-element | ord-date-element | -
 std-date-element = yyyy ['-' MM ['-' dd]

 ord-date-element = yyyy ['-' DDD]
 week-date-element = xxxx '-W' ww ['-' e]
 time-element = HH [-] | []
 minute-element = ':' mm [second-element] | []
 second-element = ':' ss [fraction]
 fraction = ('.' | ',') +
 offset = 'Z' | (('+' | '-') HH [':' mm [':' ss [('.' | ',') SSS]]])

DateTimeFormatter ( ):

DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd/yyyy");
DateTime expireDate = fmt.parseDateTime(renewDate);
+15

, parse, , String ISO

, ,

DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd/yyyy");
DateTime expireDate = DateTime.parse( renewDate, fmt );
+2

All Articles