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+ }
0 commit comments