DateUtils.kt 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package com.cooleshow.base.utils
  2. import java.text.ParseException
  3. import java.text.SimpleDateFormat
  4. import java.util.Calendar
  5. import java.util.Date
  6. import java.util.TimeZone
  7. /**
  8. * 日期工具类 默认使用 "yyyy-MM-dd HH:mm:ss" 格式化日期
  9. */
  10. object DateUtils {
  11. /**
  12. * 英文简写(默认)如:12-01
  13. */
  14. var FORMAT_MONTH_DAY = "MM-dd"
  15. /**
  16. * 英文简写(默认)如:2010-12-01
  17. */
  18. var FORMAT_SHORT = "yyyy-MM-dd"
  19. /**
  20. * 英文全称 如:2010-12-01 23:15:06
  21. */
  22. /**
  23. * 获得默认的 date pattern
  24. */
  25. var datePattern = "yyyy-MM-dd HH:mm:ss"
  26. var FORMAT_LONG_NEW = "yyyy-MM-dd HH:mm"
  27. /**
  28. * 精确到毫秒的完整时间 如:yyyy-MM-dd HH:mm:ss.S
  29. */
  30. var FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S"
  31. /**
  32. * 中文简写 如:2010年12月01日
  33. */
  34. var FORMAT_SHORT_CN_MINI = "MM月dd日 HH:mm"
  35. /**
  36. * 中文简写 如:2010年12月01日
  37. */
  38. var FORMAT_SHORT_CN = "yyyy年MM月dd日"
  39. /**
  40. * 中文全称 如:2010年12月01日 23时15分06秒
  41. */
  42. var FORMAT_LONG_CN = "yyyy年MM月dd日 HH时mm分ss秒"
  43. /**
  44. * 精确到毫秒的完整中文时间
  45. */
  46. var FORMAT_FULL_CN = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒"
  47. /**
  48. * 精确到毫秒的完整中文时间
  49. */
  50. var FORMAT_SPEFULL_CN = "yyyy年MM月dd日 HH:mm"
  51. /**
  52. * 英文简写(默认)如:2010-12-01
  53. */
  54. var FORMAT_SHORT_SPE = "yyyyMMdd"
  55. var FORMAT_SHORT_SPE_ = "HH:mm"
  56. var TIMEZONE = "Asia/Shanghai"
  57. /**
  58. * 根据预设格式返回当前日期
  59. * @return
  60. */
  61. val now: String
  62. get() = format(Date())
  63. /**
  64. * 根据用户格式返回当前日期
  65. * @param format
  66. * *
  67. * @return
  68. */
  69. fun getNow(format: String): String {
  70. return format(Date(), format)
  71. }
  72. val defTimeZone: TimeZone
  73. get() = TimeZone.getTimeZone(TIMEZONE)
  74. /**
  75. * 使用用户格式格式化日期
  76. * @param date
  77. * * 日期
  78. * *
  79. * @param pattern
  80. * * 日期格式
  81. * *
  82. * @return
  83. */
  84. @JvmOverloads fun format(date: Date?, pattern: String = datePattern): String {
  85. var returnValue = ""
  86. if (date != null) {
  87. val df = SimpleDateFormat(pattern)
  88. df.timeZone = defTimeZone
  89. returnValue = df.format(date)
  90. }
  91. return returnValue
  92. }
  93. /**
  94. * 使用用户格式提取字符串日期
  95. * @param strDate
  96. * * 日期字符串
  97. * *
  98. * @param pattern
  99. * * 日期格式
  100. * *
  101. * @return
  102. */
  103. @JvmOverloads fun parse(strDate: String, pattern: String = datePattern): Date? {
  104. val df = SimpleDateFormat(pattern)
  105. df.timeZone = defTimeZone
  106. try {
  107. return df.parse(strDate)
  108. } catch (e: ParseException) {
  109. e.printStackTrace()
  110. return null
  111. }
  112. }
  113. /**
  114. * 时间戳转date str
  115. */
  116. fun convertTimeToString(time: Long, format: String): String {
  117. val sdf = SimpleDateFormat(format)
  118. sdf.timeZone = defTimeZone
  119. return sdf.format(time)
  120. }
  121. /**
  122. * 获取当前时间的前一天时间
  123. * @param cl
  124. * *
  125. * @return
  126. */
  127. fun getBeforeDay(cl: Calendar): Calendar {
  128. val day = cl.get(Calendar.DATE)
  129. cl.set(Calendar.DATE, day - 1)
  130. return cl
  131. }
  132. /**
  133. * 获取当前时间的后一天时间
  134. * @param cl
  135. * *
  136. * @return
  137. */
  138. fun getAfterDay(cl: Calendar): Calendar {
  139. val day = cl.get(Calendar.DATE)
  140. cl.set(Calendar.DATE, day + 1)
  141. return cl
  142. }
  143. fun getWeek(c: Calendar): String {
  144. var Week = ""
  145. if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
  146. Week += "周天"
  147. }
  148. if (c.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
  149. Week += "周一"
  150. }
  151. if (c.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY) {
  152. Week += "周二"
  153. }
  154. if (c.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY) {
  155. Week += "周三"
  156. }
  157. if (c.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {
  158. Week += "周四"
  159. }
  160. if (c.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
  161. Week += "周五"
  162. }
  163. if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
  164. Week += "周六"
  165. }
  166. return Week
  167. }
  168. // date类型转换为String类型
  169. // formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
  170. // data Date类型的时间
  171. fun dateToString(date: Date, formatType: String): String {
  172. val sdf = SimpleDateFormat(formatType)
  173. sdf.timeZone = defTimeZone
  174. return sdf.format(date)
  175. }
  176. // long类型转换为String类型
  177. // currentTime要转换的long类型的时间
  178. // formatType要转换的string类型的时间格式
  179. @Throws(ParseException::class)
  180. fun longToString(currentTime: Long, formatType: String): String {
  181. val date = longToDate(currentTime, formatType) // long类型转成Date类型
  182. val strTime = dateToString(date, formatType) // date类型转成String
  183. return strTime
  184. }
  185. // string类型转换为date类型
  186. // strTime要转换的string类型的时间,formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
  187. // HH时mm分ss秒,
  188. // strTime的时间格式必须要与formatType的时间格式相同
  189. @Throws(ParseException::class)
  190. fun stringToDate(strTime: String, formatType: String): Date {
  191. val formatter = SimpleDateFormat(formatType)
  192. formatter.timeZone = defTimeZone
  193. var date: Date? = null
  194. date = formatter.parse(strTime)
  195. return date
  196. }
  197. // long转换为Date类型
  198. // currentTime要转换的long类型的时间
  199. // formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
  200. @Throws(ParseException::class)
  201. fun longToDate(currentTime: Long, formatType: String): Date {
  202. val dateOld = Date(currentTime) // 根据long类型的毫秒数生命一个date类型的时间
  203. val sDateTime = dateToString(dateOld, formatType) // 把date类型的时间转换为string
  204. val date = stringToDate(sDateTime, formatType) // 把String类型转换为Date类型
  205. return date
  206. }
  207. // string类型转换为long类型
  208. // strTime要转换的String类型的时间
  209. // formatType时间格式
  210. // strTime的时间格式和formatType的时间格式必须相同
  211. fun stringToLong(strTime: String, formatType: String): Long {
  212. var date: Date? = null // String类型转成date类型
  213. try {
  214. date = stringToDate(strTime, formatType)
  215. } catch (e: ParseException) {
  216. e.printStackTrace()
  217. }
  218. return if (date == null) {
  219. 0
  220. } else {
  221. dateToLong(date)
  222. }
  223. }
  224. // date类型转换为long类型
  225. // date要转换的date类型的时间
  226. fun dateToLong(date: Date): Long {
  227. return date.time
  228. }
  229. //当前时间毫秒数
  230. val curTime: Long
  231. get() {
  232. val c = Calendar.getInstance(defTimeZone)
  233. return c.timeInMillis
  234. }
  235. }