0

How do I can parse this date 2018-01-09T11:11:02.0+03:00 to dd.MM.yyyy hh:mm format in Android?

And what does T between 09 and 11 mean? Thanks.

I don't know how the back-end developer got this format. I am using Java.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
picKit
  • 412
  • 1
  • 4
  • 14

3 Answers3

3

You can do this with SimpleDateFormat.

Here is a tested example in Java:

String dateString = "2018-01-09T11:11:02.0+03:00";

String inPattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
String outPattern = "dd.MM.yyyy hh:mm";

SimpleDateFormat inFormat = new SimpleDateFormat(inPattern, Locale.getDefault());
SimpleDateFormat outFormat = new SimpleDateFormat(outPattern, Locale.getDefault());

try {
    Date inDate = inFormat.parse(dateString);
    String outDate = outFormat.format(inDate);
    Log.e("TEST", outDate);
} catch (ParseException e) {
    e.printStackTrace();
}

Here is a tested example in Kotlin:

val dateString = "2018-01-09T11:11:02.0+03:00"

val inPattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"
val outPattern = "dd.MM.yyyy hh:mm"

val inFormat = SimpleDateFormat(inPattern, Locale.getDefault())
val outFormat = SimpleDateFormat(outPattern, Locale.getDefault())

val inDate = inFormat.parse(dateString)
val outDate = outFormat.format(inDate)

Log.e("TEST", outDate)
Anthony Cannon
  • 1,245
  • 9
  • 20
  • Thanks. How do I can do similar thing by Java? – picKit May 16 '18 at 10:42
  • I've updated my answer with the Java implementation. – Anthony Cannon May 16 '18 at 10:46
  • Thank u very much – picKit May 16 '18 at 10:49
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. May 16 '18 at 13:55
2

If you are using java, you can use SimpeDateFormat with patterns:

        String date = "2018-01-09T11:11:02.0+03:00";
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        SimpleDateFormat output = new SimpleDateFormat("dd.MM.yyyy hh:mm");
        Date d = null;
        try {
            d = dateformat.parse(date /*your date as String*/);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String formattedDate = output.format(d);

        Log.d("Date format", "output date :" + formattedDate);

The output is :

D/Date format: output date :09.01.2018 09:11

EDIT : Thanks to @OleV.V., for API > 26, or using ThreeTenABP we can use

DateTimeFormatter, we can do something like that

    String date = "2018-01-09T11:11:02.0+03:00";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
    DateTimeFormatter formatterOut = DateTimeFormatter.ofPattern("dd.MM.yyyy hh:mm");
    LocalDate parsedDate = LocalDate.parse(date, formatter);

    String formattedDate = formatterOut.format(parsedDate);
    Log.d("Date format", "output date :" + formattedDate);
E.Abdel
  • 1,992
  • 1
  • 13
  • 24
  • Thank u very much – picKit May 16 '18 at 10:49
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. May 16 '18 at 13:55
  • @OleV.V. I agree, but it require API > 26 – E.Abdel May 16 '18 at 14:11
  • It does not, @E.Abdel. Add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your project and you’re going on low API levels too. And who knows, the asker may be on level 26 already. At least recommend this solution first and let the asker decide for herself/himself whether to go for something inferior. – Ole V.V. May 16 '18 at 14:13
  • 2
    @OleV.V. I will check that, especially to see how much the apk size increase after adding the lib, thank you – E.Abdel May 16 '18 at 14:31
2
    DateTimeFormatter desiredFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");

    String backendDateTimeString = "2018-01-09T11:11:02.0+03:00";
    OffsetDateTime dateTime = OffsetDateTime.parse(backendDateTimeString);
    String presentationDateTimeString = dateTime.format(desiredFormatter);
    System.out.println(presentationDateTimeString);

This prints:

09.01.2018 11:11

Please note: I am using uppercase HH in the format pattern string. This indicates hour of day from 00 through 23. In the question you used lowercase hh, which in a format pattern string means hour with AM or PM from 01 through 12, so 00:33 would come out as 12:33 and 15:47 as 03:47. I didn’t think you intended this.

The format that your backend developer got, 2018-01-09T11:11:02.0+03:00, is ISO 8601. It’s widespread, and it’s the international standard, so it’s good that s/he got that. The funny T in the middle indicates the start of the time part to separate it from the date part. The one-arg OffsetDateTime.parse method parses ISO 8601, which is why we didn’t need any formatter for parsing. OffsetDateTime and DateTimeFormatter are classes from java.time, the modern Java date and time API.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (API level 26 and up) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages (my code was tested with these imports).

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161