Pergunta

Im trying to convert a String to a DateTime object then only saving the Time part back in a String.

I'm getting the above exception. How can i just exstract the time part eg H:mm from the original?

So the original DateTime is: 2014-05-02T21:00:00+01:00

and i would like just the 21:00 part.

Log.e(TAG, "recordItem[0] = " + recordItem[0]);

        recordItem[0] = recordItem[0].replace('[', ' ');
        recordItem[0] = recordItem[0].trim();

        recordItem[0] = recordItem[0].replace('T', ' ');



        DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd H:mm:ss.SSS");
        DateTime dt = dtf.parseDateTime(recordItem[0]);

        DateTimeFormatter fmt = DateTimeFormat.forPattern("H:mm");
        String formattedStart = fmt.print(dt);

        Log.e(TAG, "formattedStart = " + formattedStart);

        callStartTime = formattedStart;

.

Console logs:

05-02 16:10:25.780: E/CarerDetailsFragment(6430): recordItem[0] = [2014-05-02T21:00:00+01:00
05-02 16:10:25.780: D/AndroidRuntime(6430): Shutting down VM
05-02 16:10:25.780: W/dalvikvm(6430): threadid=1: thread exiting with uncaught exception (group=0x409ed1f8)
05-02 16:10:25.780: E/AndroidRuntime(6430): FATAL EXCEPTION: main
05-02 16:10:25.780: E/AndroidRuntime(6430): java.lang.IllegalArgumentException: Invalid format: "2014-05-02 21:00:00+01:00" is malformed at "+01:00"
05-02 16:10:25.780: E/AndroidRuntime(6430):     at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:866)
05-02 16:10:25.780: E/AndroidRuntime(6430):     at com.carefreegroup.rr3.carefreeoncall.CarerDetailsFragment.onListItemClick(CarerDetailsFragment.java:395)
05-02 16:10:25.780: E/AndroidRuntime(6430):     at android.app.ListFragment$2.onItemClick(ListFragment.java:160)
05-02 16:10:25.780: E/AndroidRuntime(6430):     at android.widget.AdapterView.performItemClick(AdapterView.java:292)
05-02 16:10:25.780: E/AndroidRuntime(6430):     at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
05-02 16:10:25.780: E/AndroidRuntime(6430):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
05-02 16:10:25.780: E/AndroidRuntime(6430):     at android.widget.AbsListView$1.run(AbsListView.java:3168)
05-02 16:10:25.780: E/AndroidRuntime(6430):     at android.os.Handler.handleCallback(Handler.java:605)
05-02 16:10:25.780: E/AndroidRuntime(6430):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-02 16:10:25.780: E/AndroidRuntime(6430):     at android.os.Looper.loop(Looper.java:137)
05-02 16:10:25.780: E/AndroidRuntime(6430):     at android.app.ActivityThread.main(ActivityThread.java:4424)
05-02 16:10:25.780: E/AndroidRuntime(6430):     at java.lang.reflect.Method.invokeNative(Native Method)
05-02 16:10:25.780: E/AndroidRuntime(6430):     at java.lang.reflect.Method.invoke(Method.java:511)
05-02 16:10:25.780: E/AndroidRuntime(6430):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-02 16:10:25.780: E/AndroidRuntime(6430):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-02 16:10:25.780: E/AndroidRuntime(6430):     at dalvik.system.NativeStart.main(Native Method)

[edit1]

05-02 16:32:10.499: E/CarerDetailsFragment(6568): recordItem[0] = 2014-05-02T21:00:00+01:00
05-02 16:32:10.499: D/AndroidRuntime(6568): Shutting down VM
05-02 16:32:10.499: W/dalvikvm(6568): threadid=1: thread exiting with uncaught exception (group=0x409ed1f8)
05-02 16:32:10.509: E/AndroidRuntime(6568): FATAL EXCEPTION: main
05-02 16:32:10.509: E/AndroidRuntime(6568): java.lang.IllegalArgumentException: Invalid format: "2014-05-02T21:00:00+01:00" is malformed at "+01:00"
05-02 16:32:10.509: E/AndroidRuntime(6568):     at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:866)
Foi útil?

Solução

Your data pattern seems to be wrong. If your original date string is 2014-05-02T21:00:00+01:00 it should be yyyy-MM-dd'T'HH:mm:ss.Z without have to trim or replace "T" char

Outras dicas

ISO 8601

Your input string complies with the ISO 8601 standard. Both Joda-Time and java.time frameworks use this standard as their defaults when parsing/generating textual representations of date-time values. So no need to specify a coded parsing pattern or formatter.

Joda-Time

The following code creates a date-time assigned an offset-from-UTC of +01:00.

DateTime dateTime = DateTime.parse( "2014-05-02T21:00:00+01:00" );

Using a constructor has a different meaning. The following code parses the value with the embedded offset but then adjusts the results into your JVM’s current default time zone.

DateTime dateTime = new DateTime( "2014-05-02T21:00:00+01:00" ) );

To account for Daylight Saving Time (DST) and other anomalies, adjust into your desired/expected time zone rather than just an offset-from-UTC. Use a proper time zone name, in the format of continent/region such as Africa/Tunis or Europe/Paris. Never use the 3-4 letter codes such as EST or IST that are neither standardized nor unique.

DateTimeZone zone = DateTimezone.forID( "Europe/Paris" );
DateTime dateTime_Europe_Paris = dateTime.withZone( zone );

From that DateTime object we generate a String representation of its time-of-day value. We need a DateTimeFormatter to define this String format. Fortunately, Joda-Time already has one pre-defined for us, with just hour and minute values, accessed by calling the static method IsoDateTimeFormat.hourMinute().

DateTimeFormatter formatter = ISODateTimeFormat.hourMinute();
String output = formatter.print( dateTime_Europe_Paris );

Search StackOverflow for many more Questions and Answers with example code for Joda-Time.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top