-1

I am calling t a web service to retrieve dates. These dates come in this format

"2018-01-24T05:00:00.000Z"

Now, I need to parse that to more legible, something like "dd-MM-YYYY", e.g. 1-24-2018.

How can I do it?

I'm using Android Studio and a read about Instant but it requires minSdkVersion 26 and is set to 15 what is perfect for me right now.

theboringdeveloper
  • 1,429
  • 13
  • 17
Miguel Barra
  • 79
  • 1
  • 2
  • 5
  • Can you post more examples of the format, it's not clear what it actually signifies – theboringdeveloper Jan 24 '18 at 18:04
  • 2
    You are asking: 1) how to parse a date, and 2) how to format a date. Both questions have been answered a gazillion times, and all you have to do is **research**, aka a web search for those two questions. http://idownvotedbecau.se/noresearch/ – Andreas Jan 24 '18 at 18:32
  • 1
    I believe you *can* use `Instant`on API level 15 through [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP). And that you *should* consider doing so. In that case import `org.threeten.bp.Instant` and other date-time classes from the same package with subpackages. – Ole V.V. Jan 24 '18 at 20:17
  • 1
    @GursheeshSingh, it’s clearly ISO 8601, so well-defined. The format can for instance be descirbed as by [`DateTimeFormatter.ISO_INSTANT`](https://docs.oracle.com/javase/9/docs/api/java/time/format/DateTimeFormatter.html#ISO_INSTANT). – Ole V.V. Jan 24 '18 at 20:59

3 Answers3

2

You can use custom deserializer for JSON for dates.

You can easily parse them to Java Date object

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
Date date = dateFormat.parse("2018-01-24T05:00:00.000Z");

for more details see https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html

Saurabh Singh
  • 381
  • 5
  • 15
-1

You can try something like this....

I did this in Eclipse....

    public static void main(String[] args) throws Exception {


    String jsonDate = "2018-01-24T05:00:00.000Z";

    String[] split = jsonDate.split("-");

    System.out.println(split[0]);
    System.out.println(split[1]);
    System.out.println(split[2].substring(0, 2));

    String month = split[1].toString();
    String day = split[2].substring(0, 2).toString();
    String year = split[0].toString();

    System.out.println(month + "-" + day + "-" + year);
}

output

2018

01

24

01-24-2018

letsCode
  • 2,774
  • 1
  • 13
  • 37
-1
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

private String formatDateFromString (String dateString) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS" );
            Date date = simpleDateFormat.parse(dateString);
            simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
            return simpleDateFormat.format(date);
        } catch (ParseException e) {
        }
        return "";
    }
tanni tanna
  • 544
  • 4
  • 12