iggyweb
iggyweb

Reputation: 2483

Joda Date Format Issue

I am trying to use the following code, but I am getting an error Invalid format: "12/11/2013":

String dFrom = ps.utils.gv(request, "dFrom");
String dTo = ps.utils.gv(request, "dTo");

DateTime dateFrom = new DateTime(dFrom);
DateTime dateTo = new DateTime(dTo);

int weeks = Weeks.weeksBetween(dateFrom, dateTo).getWeeks();

Could somebody please provide an example of how to format the date variable dFrom which is typically a UK formatted date such as 12/11/2013 to an ISO Date such as 2013-11-12 which I believe Joda supports.

Any help would be much appreciated :-)

Upvotes: 0

Views: 171

Answers (1)

Ilya
Ilya

Reputation: 29703

If you want convert format 12/11/2013 to 2013-11-12, you can use

DateTimeFormatter dtf = DateTimeFormat.forPatter("dd/MM/yyyy"); // or MM/dd/yyyy ?
String isoDate = ISODateTimeFormat.date().print(dtf.parseDateTime("12/11/2013"));  

For ISO format 2013-11-12 you can use standart date formatter:
ISODateTimeFormat::date()

DateTime date = ISODateTimeFormat.date().parseDateTime("2013-11-12"); 
String dateAsString = ISODateTimeFormat.date().print(date);

For format 12/11/2013 you should create your own formatter

DateTimeFormatter dtf = DateTimeFormat.forPatter("dd/MM/yyyy"); // or MM/dd/yyyy ?
DateTime date = dtf.parseDateTime("12/11/2013"); 
String dateAsString = dtf.print(date); 

Upvotes: 2

Related Questions