Skip to content

Instantly share code, notes, and snippets.

@kosiara kosiara/TweetViewUtil.kt
Last active Aug 30, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.