Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date formatting using Joda time library

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

When i try to update in the 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 i.e, from mm/dd/yyyy to yyyy-mm-dd and send it to the java class its working fine.

How can i format the date from mm/dd/yy to yyyy-mm-dd at Java class. Input is Stringformat.

like image 354
Sangram Anand Avatar asked May 31 '12 11:05

Sangram Anand


1 Answers

The list of valid formats for the constructor you are using are detailed in the javadoc of ISODateTimeFormat, which does not include "mm/dd/yyyy":

datetime = time | date-opt-time
time = 'T' time-element [offset]
date-opt-time = date-element ['T' [time-element] [offset]]
date-element = std-date-element | ord-date-element | week-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] | [fraction]
minute-element = ':' mm [second-element] | [fraction]
second-element = ':' ss [fraction]
fraction = ('.' | ',') digit+
offset = 'Z' | (('+' | '-') HH [':' mm [':' ss [('.' | ',') SSS]]])

You can parse a different format with a DateTimeFormatter (note MM in upper case for month):

DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd/yyyy");
DateTime expireDate = fmt.parseDateTime(renewDate);
like image 74
assylias Avatar answered Sep 26 '22 00:09

assylias