0

The date from server is coming as 2018-05-19T04:51:29.6702751+00:00. I need to convert this string in Date object in Android.

I don't know how to handle the part after "+" sign

Dipali Shah
  • 3,742
  • 32
  • 47
Kishita Variya
  • 810
  • 9
  • 19
  • do you want to remove the data after + sign or what, thwn convert into DAte Object – Amit Ranjan May 21 '18 at 05:56
  • trim the text off after + sign and convert into Date object – Naveen T P May 21 '18 at 06:03
  • 1
    I want to use that data. It gives the GMT time zone which will affect the resulting Date in current time zone (where the user is) – Kishita Variya May 21 '18 at 06:10
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. May 21 '18 at 06:36

1 Answers1

1

If you are on Java-8 you can parse the string using ZonedDateTime class something like:

    ZonedDateTime d = ZonedDateTime.parse("2018-05-19T04:51:29.6702751+00:00");
    Date date = Date.from(d.toInstant());
    System.out.println(date.toString());

For Java-7 you can do like this:

    SimpleDateFormat formatwithzone = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSXXX");
    Date date1 = formatwithzone.parse("2018-05-19T04:51:29.6702751+05:30");
    System.out.println(date1.toString());
piy26
  • 1,574
  • 11
  • 21
  • The correct answer. May use `OffsetDateTime`. Unless a `Date` object is needed for some legacy API, one would prefer to stay with the `ZonedDateTime` or `OffsetDateTime`. Similar code works on pre-level-26 Android too when you use [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP). In this case the conversion goes like `Date date = DateTimeUtils.toDate(d.toInstant()):`. – Ole V.V. May 21 '18 at 06:32
  • My minimum API level is 21 and none of these are working there – Kishita Variya May 21 '18 at 06:37
  • I am assuming API level 21 is compatible with JDK 7. – piy26 May 21 '18 at 07:01
  • @Kishita The `ZonedDateTime` version works. The long explanation of what you need to do for it to work is [in this question: How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). The Android edition of `SimpleDateFormat` may sometimes behave differently from the standard Java 7 one. In any case that class is troublesome. – Ole V.V. May 21 '18 at 17:14
  • When I run the Java-7 snippet from the answer on Oracle Java in my time zone; I expect the time 01:21:29 because I am at UTC offset +02:00, so difference should be three and a half hours. However I get `Sat May 19 03:13:11 CEST 2018`. It’s still possible Android Java behaves better. I’ve heard that it handles more than three `S` in the format pattern string as expected, which a standard `SimpleDateFormat` doesn’t, as we just saw. – Ole V.V. May 21 '18 at 17:22