Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 439 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Kotlin - convert UTC to local time

#1
I'm trying to convert a UTC string date to local time, so it's in a more readable format.
I have a `textView` that in my activity layout:

<TextView
android:id="@+id/dateTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/titleTv"
tools:text="Published Date" />

In my activity:

class FullArticleActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_full_article)

val articleJson = intent.getStringExtra(ARTICLE_KEY)


if(!articleJson.isNullOrBlank()) {
val article = Gson().fromJson<Article>(articleJson, Article::class.java)

val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ", Locale.getDefault())
formatter.timeZone = TimeZone.getTimeZone("UTC")
val formattedDate = formatter.parse(article.publishedAt)

titleTv.text = article.title
//UTC string
dateTv.text = article.publishedAt
contentTv.text = article.content

The `publishedAt` string is in this format from the api call `"2018-12-10T19:48:39Z"`. How can I convert this ZULU time format to local time?
Reply

#2
Try using Extension Functions

fun String.toDate(dateFormat: String = "yyyy-MM-dd HH:mm:ss", timeZone: TimeZone = TimeZone.getTimeZone("UTC")): Date {
val parser = SimpleDateFormat(dateFormat, Locale.getDefault())
parser.timeZone = timeZone
return parser.parse(this)
}

fun Date.formatTo(dateFormat: String, timeZone: TimeZone = TimeZone.getDefault()): String {
val formatter = SimpleDateFormat(dateFormat, Locale.getDefault())
formatter.timeZone = timeZone
return formatter.format(this)
}

Usage:

"2018-09-10 22:01:00".toDate().formatTo("dd MMM yyyy")

Output: "11 Sep 2018"
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through