Skip to content

Commit 111f8a9

Browse files
committed
update: countdown timer
1 parent 9debfa0 commit 111f8a9

4 files changed

Lines changed: 139 additions & 20 deletions

File tree

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.qomunal.opensource.androidresearch.common.ext
2+
3+
import android.os.CountDownTimer
4+
import android.widget.TextView
5+
import java.text.DecimalFormat
6+
import java.text.NumberFormat
7+
import java.text.ParseException
8+
import java.text.SimpleDateFormat
9+
import java.util.Date
10+
import java.util.Locale
11+
import java.util.TimeZone
12+
13+
14+
const val ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
15+
const val DATE_TIME_FORMAT = "dd-MM-yyyy HH:mm:ss"
16+
17+
const val DATE_TIME_FORMAT_AM_PM = "dd MMMM yyyy, H.mm a"
18+
19+
const val SIMPLE_DATE_FORMAT = "dd-MM-yyyy"
20+
21+
val TIME_ZONE_ID_EXT = TimeZone.getDefault().id
22+
23+
// -------------------------------------------------------------------------------------------------
24+
25+
fun String.dateFormater(
26+
from: String = SIMPLE_DATE_FORMAT,
27+
to: String = DATE_TIME_FORMAT,
28+
): String {
29+
val fromParser = SimpleDateFormat(from, Locale.getDefault())
30+
val toParser = SimpleDateFormat(to, Locale.getDefault())
31+
return toParser.format(fromParser.parse(this) as Date)
32+
}
33+
34+
fun String.dateFormaterToUTC(
35+
from: String = SIMPLE_DATE_FORMAT,
36+
to: String = DATE_TIME_FORMAT,
37+
): String {
38+
val fromParser = SimpleDateFormat(from, Locale.getDefault()).apply {
39+
timeZone = TimeZone.getTimeZone(TIME_ZONE_ID_EXT)
40+
}
41+
val toParser = SimpleDateFormat(to, Locale.getDefault()).apply {
42+
timeZone = TimeZone.getTimeZone("UTC")
43+
}
44+
return if (this == "") {
45+
"-"
46+
} else {
47+
toParser.format(fromParser.parse(this) as Date)
48+
}
49+
}
50+
51+
fun String.dateFormaterFromUTC(
52+
from: String = SIMPLE_DATE_FORMAT,
53+
to: String = DATE_TIME_FORMAT,
54+
): String {
55+
val fromParser = SimpleDateFormat(from, Locale.getDefault()).apply {
56+
timeZone = TimeZone.getTimeZone("UTC")
57+
}
58+
val toParser = SimpleDateFormat(to, Locale.getDefault()).apply {
59+
timeZone = TimeZone.getTimeZone(TIME_ZONE_ID_EXT)
60+
}
61+
return if (this == "") {
62+
"-"
63+
} else {
64+
toParser.format(fromParser.parse(this) as Date)
65+
}
66+
}
67+
68+
fun String.toIsoDateUTC(format: String = DATE_TIME_FORMAT): String {
69+
return if (this == "") {
70+
"-"
71+
} else {
72+
dateFormaterToUTC(from = format, to = ISO_FORMAT)
73+
}
74+
}
75+
76+
fun String.fromIsoDateUTC(format: String = DATE_TIME_FORMAT): String {
77+
return if (this == "") {
78+
"-"
79+
} else {
80+
dateFormaterFromUTC(from = ISO_FORMAT, to = format)
81+
}
82+
}
83+
84+
// -------------------------------------------------------------------------------------------------
85+
86+
87+
fun String.getMillisExt(format: String = SIMPLE_DATE_FORMAT): Long {
88+
var date = Date()
89+
try {
90+
date = SimpleDateFormat(format, getLocaleExt()).parse(this) ?: Date()
91+
} catch (e: ParseException) {
92+
e.printStackTrace()
93+
}
94+
return date.time
95+
}
96+
97+
98+
fun String.formatDateExt(to: String = DATE_TIME_FORMAT_AM_PM): String {
99+
return fromIsoDateUTC(format = to)
100+
}
101+
102+
103+
fun TextView.countdownTimerExt(millisInFuture: Long, countDownInterval: Long = 1000) {
104+
object : CountDownTimer(millisInFuture, countDownInterval) {
105+
override fun onTick(millisUntilFinished: Long) {
106+
// Used for formatting digits to be in 2 digits only
107+
val f: NumberFormat = DecimalFormat("00")
108+
val hour = (millisUntilFinished / 3600000) % 24
109+
val min = (millisUntilFinished / 60000) % 60
110+
val sec = (millisUntilFinished / 1000) % 60
111+
text = f.format(hour) + " Jam : " + f.format(min) + " Menit : " + f.format(sec) + " Detik"
112+
}
113+
114+
override fun onFinish() {
115+
// When the task is over it will print 00:00:00
116+
text = "00 Jam : 00 Menit : 00 Detik"
117+
}
118+
}.start()
119+
}

app/src/main/java/com/qomunal/opensource/androidresearch/common/ext/MainExt.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.qomunal.opensource.androidresearch.common.ext
22

33
import android.content.Context
44
import android.widget.Toast
5+
import java.util.Locale
56

67
/**
78
* Created by faisalamircs on 13/01/2024
@@ -15,3 +16,7 @@ import android.widget.Toast
1516
fun Context.showToast(message: String) {
1617
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
1718
}
19+
20+
fun getLocaleExt(): Locale {
21+
return Locale.getDefault()
22+
}

app/src/main/java/com/qomunal/opensource/androidresearch/ui/main/MainActivity.kt

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.qomunal.opensource.androidresearch.ui.main
22

33
import android.os.Bundle
4-
import android.os.CountDownTimer
4+
import android.util.Log
55
import androidx.activity.viewModels
66
import com.qomunal.opensource.androidresearch.common.base.BaseActivity
7+
import com.qomunal.opensource.androidresearch.common.ext.ISO_FORMAT
8+
import com.qomunal.opensource.androidresearch.common.ext.countdownTimerExt
9+
import com.qomunal.opensource.androidresearch.common.ext.fromIsoDateUTC
10+
import com.qomunal.opensource.androidresearch.common.ext.getMillisExt
711
import com.qomunal.opensource.androidresearch.databinding.ActivityMainBinding
8-
import java.text.DecimalFormat
9-
import java.text.NumberFormat
1012

1113

1214
class MainActivity : BaseActivity<ActivityMainBinding>() {
@@ -26,21 +28,14 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
2628

2729
override fun initUI() {
2830
binding.apply {
29-
object : CountDownTimer(50000, 1000) {
30-
override fun onTick(millisUntilFinished: Long) {
31-
// Used for formatting digits to be in 2 digits only
32-
val f: NumberFormat = DecimalFormat("00")
33-
val hour = (millisUntilFinished / 3600000) % 24
34-
val min = (millisUntilFinished / 60000) % 60
35-
val sec = (millisUntilFinished / 1000) % 60
36-
btnTest.text = f.format(hour) + ":" + f.format(min) + ":" + f.format(sec)
37-
}
38-
39-
override fun onFinish() {
40-
// When the task is over it will print 00:00:00
41-
btnTest.text = "00:00:00"
42-
}
43-
}.start()
31+
32+
val dates = "2025-06-16T08:54:12.000Z"
33+
34+
Log.d("Stechu", "dates: $dates")
35+
Log.d("Stechu", "dates: ${dates.fromIsoDateUTC()}")
36+
Log.d("Stechu", "dates: ${dates.getMillisExt(ISO_FORMAT)}")
37+
38+
tvText.countdownTimerExt(dates.getMillisExt(ISO_FORMAT))
4439
}
4540
}
4641

app/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
android:layout_width="match_parent"
55
android:layout_height="match_parent">
66

7-
<Button
8-
android:id="@+id/btn_test"
7+
<TextView
8+
android:id="@+id/tv_text"
99
android:layout_width="wrap_content"
1010
android:layout_height="wrap_content"
1111
android:text="Click On Me"

0 commit comments

Comments
 (0)