-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateUtils.kt
148 lines (135 loc) · 4.32 KB
/
DateUtils.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.applogist.extensions
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*
/**
* Format String with given coming and sending date format
* @param comingDateFormat : Current Date format of String
* @param sendingDateFormat : Desired Date format for String
*/
fun String.formatDate(comingDateFormat: SimpleDateFormat, sendingDateFormat: SimpleDateFormat = comingDateFormat): String? {
val date: Date?
try {
date = comingDateFormat.parse(this)
} catch (e: ParseException) {
e.printStackTrace()
return null
}
date?.let { return sendingDateFormat.format(it) } ?: return null
}
/**
* Format String with given coming and sending date format patterns
* @param comingDatePattern : Current date format pattern
* @param sendingDatePattern : Desired date format pattern
*/
fun String.formatDate(comingDatePattern : String, sendingDatePattern : String = comingDatePattern, locale : Locale = Locale.getDefault()): String? {
val comingDateFormat = SimpleDateFormat(comingDatePattern, locale)
val date: Date?
try {
date = comingDateFormat.parse(this)
} catch (e: ParseException) {
e.printStackTrace()
return null
}
val sendingDateFormat = SimpleDateFormat(sendingDatePattern, locale)
date?.let { return sendingDateFormat.format(it) } ?: return null
}
/**
* Convert date to Calendar
* @param comingDateFormat : Current date format of String
*/
fun String.convertDateToCalendar(comingDateFormat: SimpleDateFormat) : Calendar? {
val date: Date?
try {
date = comingDateFormat.parse(this)
} catch (e: ParseException) {
e.printStackTrace()
return null
}
date?.let {
val cal = Calendar.getInstance()
cal.time = date
return cal
} ?: return null
}
/**
* Convert date to Calendar
* @param sendingDatePattern : Current date format pattern
*/
fun String.convertDateToCalendar(sendingDatePattern : String, locale : Locale = Locale.getDefault()) : Calendar? {
val comingDateFormat = SimpleDateFormat(sendingDatePattern, locale)
val date: Date?
try {
date = comingDateFormat.parse(this)
} catch (e: ParseException) {
e.printStackTrace()
return null
}
date?.let {
val cal = Calendar.getInstance()
cal.time = date
return cal
} ?: return null
}
/**
* Convert date to millis
* @param comingDateFormat : Current date format of String
*/
fun String.convertToMillis(comingDateFormat: SimpleDateFormat) : Long? {
return this.convertDateToCalendar(comingDateFormat)?.timeInMillis
}
/**
* Convert date to to Millis
* @param sendingDatePattern : Current date format pattern
*/
fun String.convertToMillis(sendingDatePattern : String, locale : Locale = Locale.getDefault()) : Long? {
return this.convertDateToCalendar(sendingDatePattern, locale)?.timeInMillis
}
/**
* Convert long millis to date
* @param dateFormat : Desired date format
*/
fun Long.convertDateString(dateFormat: SimpleDateFormat): String? {
val date: Date?
try {
date = Date(this)
} catch (e: ParseException) {
e.printStackTrace()
return null
}
return dateFormat.format(date)
}
/**
* Convert long millis to date
* @param dateFormatPattern : Desired date format pattern
*/
fun Long.convertDateString(dateFormatPattern: String, locale : Locale = Locale.getDefault()): String? {
val comingDateFormat = SimpleDateFormat(dateFormatPattern, locale)
val date: Date?
try {
date = Date(this)
} catch (e: ParseException) {
e.printStackTrace()
return null
}
return comingDateFormat.format(date)
}
/**
* @param pattern the pattern describing the date and time format
* @param locale the locale whose date format symbols should be used
* @param ifNull if you give null, when error happened
* function return null otherwise return today date
* @param timeZone the given new time zone
*/
fun String.toDate(pattern: String, locale: Locale = Locale.getDefault(), ifNull: Date? = Date(), timeZone: TimeZone? = null): Date? {
val format = SimpleDateFormat(pattern, locale)
if(timeZone != null){
format.timeZone = timeZone
}
return try {
format.parse(this) ?: ifNull
} catch (e: ParseException) {
e.printStackTrace()
ifNull
}
}