Mico
Mico

Reputation: 497

Joda time invalid format

there's an error when I try to execute this code with Joda time:

// yyyy-mm-dd, also tried 17-Feb-2013
String input = "2013-02-17";

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy");
DateTime dt = formatter.parseDateTime(input);

TextView tv = (TextView) findViewById(R.id.tvShow);
tv.setText("HERE >>> " + dt.toString());

any ideas?

logcat says:

03-18 16:58:14.919: W/System.err(8259): java.lang.IllegalArgumentException: Invalid format: "2013-02-17" is malformed at "13-02-17"
03-18 16:58:14.919: W/System.err(8259):     at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:871)
03-18 16:58:14.919: W/System.err(8259):     at com.example.dateformatter.MainActivity.onCreate(MainActivity.java:42)
03-18 16:58:14.919: W/System.err(8259):     at android.app.Activity.performCreate(Activity.java:5163)
03-18 16:58:14.919: W/System.err(8259):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-18 16:58:14.919: W/System.err(8259):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
03-18 16:58:14.919: W/System.err(8259):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
03-18 16:58:14.919: W/System.err(8259):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-18 16:58:14.919: W/System.err(8259):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
03-18 16:58:14.927: W/System.err(8259):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 16:58:14.927: W/System.err(8259):     at android.os.Looper.loop(Looper.java:137)
03-18 16:58:14.927: W/System.err(8259):     at android.app.ActivityThread.main(ActivityThread.java:4895)
03-18 16:58:14.927: W/System.err(8259):     at java.lang.reflect.Method.invokeNative(Native Method)
03-18 16:58:14.927: W/System.err(8259):     at java.lang.reflect.Method.invoke(Method.java:511)
03-18 16:58:14.927: W/System.err(8259):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
03-18 16:58:14.927: W/System.err(8259):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
03-18 16:58:14.927: W/System.err(8259):     at dalvik.system.NativeStart.main(Native Method)
03-18 16:58:14.950: D/KeyguardViewMediator(357): setHidden false

Upvotes: 1

Views: 1886

Answers (3)

Jon Skeet
Jon Skeet

Reputation: 1503869

You've asked it to parse "2013-02-17" as if it were in the format "dd/MM/yyyy", which it's clearly not.

You need to specify yyyy-MM-dd as the pattern:

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");

Or if you need dd/MM/yyyy as the pattern, you need to provide it text of that form, e.g.

String input = "17/02/2013";

Either way, the data you're providing has to match the format you're specifying. That's the whole point of specifying the format, after all.

See the docs for DateTimeFormat for what the various letters in the format pattern mean.

Upvotes: 8

Festus Tamakloe
Festus Tamakloe

Reputation: 11310

You are not suppose to make a confusion in the format

Option 1

String input = "2013-02-17";

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd"); DateTime dt = formatter.parseDateTime(input);

TextView tv = (TextView) findViewById(R.id.tvShow); tv.setText("HERE >>> " + dt.toString());

Option 2

String input = "17/02/2013";

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy"); DateTime dt = formatter.parseDateTime(input);

TextView tv = (TextView) findViewById(R.id.tvShow); tv.setText("HERE >>> " + dt.toString());

Upvotes: 0

duggu
duggu

Reputation: 38439

Try this code u input wrong date format.

String input = "17/02/2013";

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy");
DateTime dt = formatter.parseDateTime(input);

Upvotes: 0

Related Questions