-1

I am working with the Twitter api. I get this back from the api

"created_at": "Mon Dec 17 21:37:27 +0000 2018"

How can I make this a nice date like:

17 Dec 2017 ?

DateFormat outputFormat = new SimpleDateFormat("dd MMM yyyy");
DateFormat inputFormat = new SimpleDateFormat("????");
String inputDateStr = "Mon Dec 17 21:37:27 +0000 2018";
Date date = inputFormat.parse(inputDateStr);
String s = outputFormat.format(date);

can someone help me with this one? something like "EE MMM dd H:m:s +0000 yyyy" ?

Peter
  • 275
  • 1
  • 2
  • 9
  • 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. Dec 18 '18 at 08:09
  • Possible duplicate of [Twitter date unparseable?](https://stackoverflow.com/questions/4521715/twitter-date-unparseable) – Ole V.V. Dec 18 '18 at 08:10
  • Possible duplicate of [How to convert Date.toString back to Date?](https://stackoverflow.com/questions/9431927/how-to-convert-date-tostring-back-to-date) – Ole V.V. Dec 18 '18 at 08:16
  • Please search Stack Overflow before posting. You are likely to get a better answer faster that way. This question has been asked and answered pretty many times already. [I downvoted because research must be done to ask a good question](http://idownvotedbecau.se/noresearch/). – Ole V.V. Dec 18 '18 at 08:19

1 Answers1

1

Here is answer for you

Kotlin version

val date = "Mon Dec 17 21:37:27 +0000 2018"
val dateFormat = "EEE MMM dd HH:mm:ss ZZZZZ yyyy"
val simpleDateFormat = SimpleDateFormat(dateFormat, Locale.ENGLISH)

val result = simpleDateFormat.parse(date)

Java version

String date = "Mon Dec 17 21:37:27 +0000 2018";
String dateFormat = "EEE MMM dd HH:mm:ss ZZZZZ yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat, Locale.ENGLISH);

Date result = simpleDateFormat.parse(date)
Seungmin Ha
  • 151
  • 1
  • 5