1

I an new to android and am having trouble working with jodatime. I am getting the same error as many people on here and I have tried all the suggestions but nothing is working for me. I am trying to display the variables a user selected from a date and time picker e.g. 'dayNow, monthNow' etc into one combined variable called 'timeselected' and set it to a textbox so then I can carry out a calculation in jodatime further on down.

The code is as follows:

            DateTime dateTime = new DateTime(timeselected);
            DateTimeFormatter fmt = DateTimeFormat.forPattern("dd MM yyyy" + "\n" + " h:mm a ");
            String formattedtime = fmt.print(dateTime);
            CalculateButton.setText(formattedtime);

            // Plus some hours, minutes, and seconds to the original DateTime.
            DateTimeFormatter fmt2 = DateTimeFormat.forPattern("dd MM yyyy" + "\n" + " h:mm a ");

            DateTime dateTime1 = dateTime.plusHours(timeadded);
            String endtimecalc = fmt2.print(dateTime1);
            TextView endtime = (TextView) findViewById(endtimetextView);
            endtime.setVisibility(View.VISIBLE);
            endtime.setText(endtimecalc);

            String timeselected = dayNow + "-" + monthNow + "-" + yearNow + " " + hourNow + ":" + minuteNow;

            DateTime datetimselected = DateTime.parse(timeselected);

            usertimeselection.setText((CharSequence) datetimselected.toDate());

        }
    });

FATAL EXCEPTION: main Process: com.almac.tracker, PID: 29114 java.lang.IllegalArgumentException: Invalid format: "2017-11-23 T 4:56" is malformed at " T 4:56" at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:945) at org.joda.time.DateTime.parse(DateTime.java:160) at org.joda.time.DateTime.parse(DateTime.java:149) at com.almac.tracker.CreateLine$5.onClick(CreateLine.java:274) at android.view.View.performClick(View.java:6219) at android.view.View$PerformClick.run(View.java:24482) at android.os.Handler.handleCallback(Handler.java:769) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6540) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Almac3
  • 63
  • 7
  • There must be a dup of this, but Joda Time is fussy about formats; you need to separate the date components with `-` not `/`, for a start. See [the docs](http://joda-time.sourceforge.net/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTimeParser\(\)) for more info - `8-11-2017 T 4:16:00` might be needed to pass, for instance. – Ken Y-N Nov 07 '17 at 09:26
  • I have added all the relevant code above if you could take a look please – Almac3 Nov 07 '17 at 09:28

1 Answers1

1

From DateTime.parse(String str) documentation:

Parses a DateTime from the specified string.

This uses ISODateTimeFormat.dateTimeParser().

Your datetime string is not valid for the ISODateTimeFormat, it does not accept / but -.

So either change your / to - and comply to the rule: date-opt-time = date-element ['T' [time-element] [offset]] (see that 'T' between the date and time elements!) So your datetime string should be like this:

String timeselected = yearNow + "-" + monthNow + "-" + dayNow + "'T'" + hourNow + ":" + minuteNow;
DateTime datetimselected = DateTime.parse(timeselected);

either use your own formatter via the DateTime.parse(String str, DateTimeFormatter formatter) method.

UPDATE 1

After running some tests indeed the 'T' is not accepted by the default parser. A string that passes from the parser is :

"2017-11-23T04:56"

So make sure no spaces exist between the date section, the "T" character and the time section.

String timeselected = yearNow + "-" + monthNow + "-" + dayNow + "T" + hourNow + ":" + minuteNow;

UPDATE 2

Attaching my test code for the parser

public class Main {
    public static void main(String[] args) {
        String timeselected = "2017-11-23T04:56";
        DateTime datetimselected = DateTime.parse(timeselected);
        System.out.println(datetimselected.toDate());
    }
}

output:

Thu Nov 23 04:56:00 EET 2017
pleft
  • 7,567
  • 2
  • 21
  • 45
  • @Almac3 I have updated my answer with the correctly formatted datetime string for `DateTime.parse` – pleft Nov 07 '17 at 09:38
  • tried that but it doesn't accept it, also we are trying to do a calculation between 2 date times....does the format matter in order to do the calculation behind the scenes? thankyou – Almac3 Nov 07 '17 at 10:13
  • Oh I see it needs first year then month then day so your string should be: `yearNow + "-" + monthNow + "-" + dayNow + "'T'" + hourNow + ":" + minuteNow;` I have updated my answer accordingly – pleft Nov 07 '17 at 10:22
  • its still not letting me add the public static DateTime line – Almac3 Nov 07 '17 at 10:25
  • What "public static DateTime line" ??? From my answer? This is an alternative (keyword: **either**)! I will update my answer to remove this if it is confusing – pleft Nov 07 '17 at 10:28
  • how do I convert the string 'time selected' to a datetime? – Almac3 Nov 07 '17 at 10:45
  • Its all in my answer (and in your posted code) check line: `DateTime datetimselected = DateTime.parse(timeselected);` – pleft Nov 07 '17 at 10:50
  • ok yes, I need it as a datetime and now it continues to crash with your code - says "invalid format ........ is malformed at 'T 10:58'" – Almac3 Nov 07 '17 at 10:58
  • Post the crash errors, just it crashes is not any useful – pleft Nov 07 '17 at 10:59
  • I have added the newest stacktrace in replace of the older one - thank you – Almac3 Nov 07 '17 at 11:04
  • Seen the stacktrace now. Are you sure this is the output from my answer? I have **T** in single quotes and with no spaces whereas in the stacktrace no single quotes appear and there are spaces before and after – pleft Nov 07 '17 at 11:33
  • Updated my answer, no single quotes required in **T** character but pay attention to the spaces before and after, there should not be any – pleft Nov 07 '17 at 12:11