1+ package com.frogobox.appkeyboard.common.ext
2+
3+ import java.text.ParseException
4+ import java.text.SimpleDateFormat
5+ import java.util.Calendar
6+ import java.util.Date
7+ import java.util.Locale
8+ import java.util.TimeZone
9+
10+ /* *
11+ * Created by faisalamircs on 09/11/2025
12+ * -----------------------------------------
13+ * Name : Muhammad Faisal Amir
14+ * E-mail : faisalamircs@gmail.com
15+ * Github : github.com/amirisback
16+ * -----------------------------------------
17+ */
18+
19+
20+
21+ // Format Second
22+ const val SECOND_MILLIS = 1000
23+ const val MINUTE_MILLIS = 60 * SECOND_MILLIS
24+ const val HOUR_MILLIS = 60 * MINUTE_MILLIS
25+ const val DAY_MILLIS = 24 * HOUR_MILLIS
26+
27+ // Format Date
28+ const val DATE_TIME_GLOBAL = " yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" //
29+ const val DATE_TIME_STANDARD = " yyyy-MM-dd HH:mm:ss" // 2018-10-02 12:12:12
30+ const val DATE_ENGLISH_YYYY_MM_DD = " yyyy-MM-dd" // 2018-10-02
31+ const val DATE_ENGLISH_YYYY_MM_DD_CLEAR = " yyyy MM dd" // 2018 10 02
32+ const val DATE_DD_MM_YYYY = " dd-MM-yyyy" // 02-10-2018
33+ const val DATE_EEEE_DD_MM_YYYY = " EEEE, dd MMMM yyyy" // 02-10-2018
34+ const val DATE_DD_MM_YYYY_CLEAR = " dd MM yyyy" // 02-10-2018
35+
36+ // Format Time
37+ const val TIME_GENERAL_HH_MM_SS = " HH:mm:ss" // 12:12:12
38+ const val TIME_GENERAL_HH_MM = " HH:mm" // 12:12
39+
40+ // Format Day
41+ const val DAY_WITH_DATE_TIME_ENGLISH = " EEE, MMM dd yyyy HH:mm" // Mon, Aug 12 2018 12:12
42+ const val DAY_WITH_DATE_TIME_LOCALE = " EEE, dd MMM yyyy HH:mm" // Sen, 12 Agt 2019 12:12
43+ const val DAY_WITH_DATE_TIME_ENGLISH_FULL = " EEEE, MMMM dd yyyy HH:mm" // Monday, August 12 2018 12:12
44+ const val DAY_WITH_DATE_TIME_LOCALE_FULL = " EEEE, dd MMMM yyyy HH:mm" // Senin, 12 Agustus 2018 12:12
45+
46+ fun getTimeStamp (): String {
47+ val simpleDateFormat = SimpleDateFormat (DATE_TIME_STANDARD , Locale .getDefault())
48+ return simpleDateFormat.format(Date ())
49+ }
50+
51+ fun getTimeNow (): String {
52+ val simpleDateFormat = SimpleDateFormat (TIME_GENERAL_HH_MM_SS , Locale .getDefault())
53+ return simpleDateFormat.format(Date ())
54+ }
55+
56+ fun getCurrentDate (format : String ): String {
57+ val simpleDateFormat = SimpleDateFormat (format, Locale .getDefault())
58+ return simpleDateFormat.format(Date ())
59+ }
60+
61+ fun dateTimeToTimeStamp (date : String? ): Long {
62+ var timestamp: Long = 0
63+ val tz = TimeZone .getTimeZone(" UTC" )
64+ val df = SimpleDateFormat (DATE_TIME_GLOBAL , Locale .getDefault())
65+ df.timeZone = tz
66+
67+ if (date != null ) {
68+ try {
69+ timestamp = df.parse(date).time / 1000
70+ } catch (e: ParseException ) {
71+ e.printStackTrace()
72+ }
73+
74+ } else {
75+ timestamp = Date ().time / 1000
76+ }
77+ return timestamp
78+ }
79+
80+ fun getCurrentUTC (): String {
81+ val time = Calendar .getInstance().time
82+ val outputFmt = SimpleDateFormat (TIME_GENERAL_HH_MM_SS , Locale .getDefault())
83+ outputFmt.timeZone = TimeZone .getTimeZone(" UTC" )
84+ return outputFmt.format(time)
85+ }
86+
87+ fun convertClassificationDate (string : String? ): String {
88+ return if (string != null ) {
89+ if (string.contains(" /" )) {
90+ val temp = string.split(" /" )
91+ temp[1 ] + " -" + temp[0 ] + " -01"
92+ } else {
93+ " "
94+ }
95+ } else {
96+ " "
97+ }
98+ }
99+
100+ fun convertDateNewFormat (string : String? ): String {
101+ val formatter = SimpleDateFormat (DATE_ENGLISH_YYYY_MM_DD , Locale .getDefault())
102+ val date = formatter.parse(string) as Date
103+ val newFormat = SimpleDateFormat (DATE_DD_MM_YYYY , Locale (" EN" ))
104+ return newFormat.format(date)
105+ }
106+
107+ fun convertLongDateNewFormat (string : String? ): String {
108+ val formatter = SimpleDateFormat (DATE_TIME_STANDARD , Locale .getDefault())
109+ val date = formatter.parse(string) as Date
110+ val newFormat = SimpleDateFormat (" dd-MM-yy HH:mm:ss" , Locale (" EN" ))
111+ return newFormat.format(date)
112+ }
113+
114+ fun revertFromLongDateNewFormat (string : String? ): String {
115+ val formatter = SimpleDateFormat (" dd-MM-yy HH:mm:ss" , Locale (" EN" ))
116+ val date = formatter.parse(string) as Date
117+ val newFormat = SimpleDateFormat (DATE_TIME_STANDARD , Locale .getDefault())
118+ val finalString = newFormat.format(date)
119+ return finalString
120+ }
121+
122+ fun convertTargetDate (string : String? ): String {
123+ return if (string != null ) {
124+ if (string.contains(" /" )) {
125+ val temp = string.split(" /" )
126+ temp[1 ] + " -" + temp[0 ] + " -01 00:00:00"
127+ } else {
128+ " "
129+ }
130+ } else {
131+ " "
132+ }
133+ }
134+
135+ fun diffTime (timeStart : String , timeEnd : String ): Long {
136+ var min: Long = 0
137+ val diff: Long
138+ val format = SimpleDateFormat (TIME_GENERAL_HH_MM_SS , Locale .getDefault())
139+
140+ val d1: Date ?
141+ val d2: Date ?
142+
143+ try {
144+ d1 = format.parse(timeStart)
145+ d2 = format.parse(timeEnd)
146+
147+ diff = d2.time - d1.time
148+ min = diff / (60 * 1000 )
149+
150+ } catch (e: Exception ) {
151+ e.printStackTrace()
152+ }
153+
154+ return min
155+ }
0 commit comments