Skip to content

Instantly share code, notes, and snippets.

@kosiara
Last active August 30, 2018 19:09
Show Gist options
  • Save kosiara/97934d7cf2a15d9d0993a4e4fae912da to your computer and use it in GitHub Desktop.
Save kosiara/97934d7cf2a15d9d0993a4e4fae912da to your computer and use it in GitHub Desktop.
Reformat Twitter date
/**
* Reformat Twitter date to a new pattern
*/
fun TweetView.reformatDate(newDatePattern: String) {
val twitterDatePattern = "EEE MMM dd HH:mm:ss Z yyyy" // Mon Apr 21 04:08:37 +0000 2018
val dateTextView = findViewById<AppCompatTextView>(R.id.tw__tweet_timestamp)
dateTextView.text = parseDate(tweet.createdAt, twitterDatePattern).format(newDatePattern)
}
/**
* Parse date according to a Pattern
*/
@SuppressLint("SimpleDateFormat")
fun parseDate(stringDate: String, pattern: String): Date = SimpleDateFormat(pattern).parse(stringDate)
/**
* Format Date according to Pattern (SimpleDateFormat)
*/
@SuppressLint("SimpleDateFormat")
fun Date.format(pattern: String) : String = SimpleDateFormat(pattern).format(this)
val tweet = result!!.data
val tweetView = TweetView(componentInstance.context, tweet, R.style.CustomTwitterStyle)
tweetView.reformatDate("HH:mm:ss")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment