-1

I want to get date and time from 2019-06-27T12:30:00.000+0000 in android. I tried a code but it is not working.

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    Date date = null;//You will get date object relative to server/client 
timezone wherever it is parsed
    try {
        date = dateFormat.parse("2019-06-27T12:30:00.000+0000");
    } catch (ParseException e) {
        e.printStackTrace();
    }
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); //If you need time just put specific format for time like 'HH:mm:ss'
    String dateStr = formatter.format(date);
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Arjun Babu C
  • 35
  • 1
  • 8
  • As an aside consider throwing away the long outmoded and notoriously troublesome `DateFormat`, `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. Jun 27 '19 at 14:09
  • What do you mean by *not working*? When asking about code that doesn’t work as intended, please always make clear the *desired result* and *precisely how observed result differs*. If you see any error message, quote it verbatim. Thx. [I downvoted because "it's not working" is not helpful](http://idownvotedbecau.se/itsnotworking/). – Ole V.V. Jun 27 '19 at 14:11
  • Possible duplicate of [Convert date into AEST using java](https://stackoverflow.com/questions/48412345/convert-date-into-aest-using-java) and/or [java to mysql. I need convert from string parametre to timestamp](https://stackoverflow.com/questions/53291240/java-to-mysql-i-need-convert-from-string-parametre-to-timestamp) – Ole V.V. Jun 27 '19 at 14:15
  • I am voting to close as a simple typographical error. – Ole V.V. Jun 28 '19 at 10:45

2 Answers2

0

You're missing the millisecond portion of your SimpleDateFormat, below is what you need for that specific pattern:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Mark
  • 535
  • 4
  • 13
0

You're missing out on parts of the Date The pattern you need is,

DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'+'ssss");
isstiaung
  • 611
  • 5
  • 12
  • Yeah? How come? I tried it out with a simple test file and it seemed to work for me. Is there something wrong with the pattern matching? – isstiaung Jun 27 '19 at 14:15
  • I get `Thu Jun 27 12:30:00 CEST 2019`, which in incorrect because CEST is at offset +02:00, where the string had +00:00. Other strings will give other wrong results. – Ole V.V. Jun 27 '19 at 14:18