Explorar el Código

DateUtil 更新

hubowen hace 2 años
padre
commit
321fdc1395

+ 3 - 1
app/src/main/AndroidManifest.xml

@@ -15,12 +15,14 @@
         tools:targetApi="31">
         <activity
             android:name="com.convenient.android.lib.MainActivity"
-            android:exported="true" >
+            android:exported="true">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
+
+        <activity android:name=".DateUtilActivity" />
     </application>
 
 </manifest>

+ 38 - 0
app/src/main/java/com/convenient/android/lib/DateUtilActivity.kt

@@ -0,0 +1,38 @@
+package com.convenient.android.lib
+
+import android.os.Bundle
+import androidx.lifecycle.lifecycleScope
+import com.convenient.android.common.base.viewbinding.BaseBindingActivity
+import com.convenient.android.common.utils.date.*
+import com.convenient.android.lib.databinding.ActivityDateBinding
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
+
+/**
+ * @author: hubowen
+ * @date: 2022/8/3
+ * @description:
+ */
+class DateUtilActivity : BaseBindingActivity<ActivityDateBinding>(ActivityDateBinding::inflate) {
+
+    override fun onCreate(savedInstanceState: Bundle?) {
+        super.onCreate(savedInstanceState)
+        binding.apply {
+
+            lifecycleScope.launch(Dispatchers.IO) {
+                while (true) {
+                    delay(1000)
+                    withContext(Dispatchers.Main) {
+                        idDateActivityTvTime.text = System.currentTimeMillis().toDate(DateUtils.DateFormat.DEFAULT_PATTERN.dateFormat)
+                    }
+                }
+            }
+
+            idDateActivityTvContent.text =
+                "${System.currentTimeMillis().toDate(DateUtils.DateFormat.DEFAULT_PATTERN.dateFormat)}\n" +
+                        "${year("年")}${month(isAbbreviation = true)}${day("日")}${hour("时")}${minute("分")}${second("秒")}${week(isAbbreviation = true)}"
+        }
+    }
+}

+ 10 - 6
app/src/main/java/com/convenient/android/lib/MainActivity.kt

@@ -1,14 +1,18 @@
 package com.convenient.android.lib
 
-import androidx.appcompat.app.AppCompatActivity
 import android.os.Bundle
-import java.io.Serializable
-
-class MainActivity : AppCompatActivity() {
+import com.convenient.android.common.base.viewbinding.BaseBindingActivity
+import com.convenient.android.common.extension.clickWithTrigger
+import com.convenient.android.common.extension.readyGo
+import com.convenient.android.lib.databinding.ActivityMainBinding
 
+class MainActivity : BaseBindingActivity<ActivityMainBinding>(ActivityMainBinding::inflate) {
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
-        setContentView(R.layout.activity_main)
-
+        binding.apply {
+            etText.clickWithTrigger {
+                readyGo(DateUtilActivity::class.java)
+            }
+        }
     }
 }

+ 36 - 0
app/src/main/res/layout/activity_date.xml

@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
+
+    <androidx.appcompat.widget.AppCompatTextView
+        android:id="@+id/id_date_activity_tv_date"
+        android:layout_width="match_parent"
+        android:layout_height="50dp"
+        android:gravity="center"
+        android:text="DateUtil"
+        android:textSize="14sp"
+        app:layout_constraintLeft_toLeftOf="parent"
+        app:layout_constraintTop_toTopOf="parent" />
+
+    <androidx.appcompat.widget.AppCompatTextView
+        android:id="@+id/id_date_activity_tv_time"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:gravity="center"
+        android:textSize="14sp"
+        app:layout_constraintLeft_toLeftOf="parent"
+        app:layout_constraintRight_toRightOf="parent"
+        app:layout_constraintTop_toBottomOf="@+id/id_date_activity_tv_date" />
+
+    <androidx.appcompat.widget.AppCompatTextView
+        android:id="@+id/id_date_activity_tv_content"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="10dp"
+        app:layout_constraintLeft_toLeftOf="parent"
+        app:layout_constraintRight_toRightOf="parent"
+        app:layout_constraintTop_toBottomOf="@+id/id_date_activity_tv_time" />
+
+</androidx.constraintlayout.widget.ConstraintLayout>

+ 8 - 9
app/src/main/res/layout/activity_main.xml

@@ -5,14 +5,13 @@
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context=".MainActivity">
-<androidx.appcompat.widget.AppCompatEditText
-    app:layout_constraintTop_toTopOf="parent"
-    app:layout_constraintStart_toStartOf="parent"
-    app:layout_constraintEnd_toEndOf="parent"
-    android:id="@+id/et_text"
-    android:layout_width="match_parent"
-    android:layout_height="wrap_content"
-    />
-
 
+    <androidx.appcompat.widget.AppCompatButton
+        android:id="@+id/et_text"
+        android:text="测试"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent" />
 </androidx.constraintlayout.widget.ConstraintLayout>

+ 116 - 0
lib_common/src/main/java/com/convenient/android/common/utils/date/DateExtension.kt

@@ -0,0 +1,116 @@
+package com.convenient.android.common.utils.date
+
+import java.util.*
+
+/**
+ * @author: hubowen
+ * @date: 2022/8/3
+ * @description:
+ */
+
+/**
+ * @param: format 时间格式
+ * @description: 时间戳转换成默认时间格式
+ */
+fun Long.toDate(format: String): String = DateUtils.getFormatDate(this, format)
+
+/**
+ * @param: singleDateFormat 单独时间格式
+ * @description: 获得当前时间信息
+ */
+fun getSingleDate(singleDateFormat: DateUtils.SingleDateFormat): Int {
+    Calendar.getInstance().apply {
+        return when (singleDateFormat) {
+            DateUtils.SingleDateFormat.YEAR -> get(Calendar.YEAR)
+            DateUtils.SingleDateFormat.MONTH -> get(Calendar.MONTH) + 1
+            DateUtils.SingleDateFormat.DAY -> get(Calendar.DAY_OF_MONTH)
+            DateUtils.SingleDateFormat.HOUR -> get(Calendar.HOUR_OF_DAY)
+            DateUtils.SingleDateFormat.MINUTE -> get(Calendar.MINUTE)
+            DateUtils.SingleDateFormat.SECOND -> get(Calendar.SECOND)
+            DateUtils.SingleDateFormat.WEEK -> get(Calendar.DAY_OF_WEEK) - 1
+        }
+    }
+}
+
+/**
+ * @param: endStr 结尾字符
+ * @description: 获得当前年份
+ */
+fun year(endStr: String? = ""): String = "${getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.YEAR)}$endStr"
+
+/**
+ * @param: endStr 结尾字符
+ * @param: isAbbreviation 是否简写
+ * @description: 获得当前月份
+ */
+fun month(endStr: String? = "", isAbbreviation: Boolean = false): String {
+    fun getAbbreviation(month: Int): String {
+        return when (month) {
+            1 -> "Jan"
+            2 -> "Feb"
+            3 -> "Mar"
+            4 -> "Apr"
+            5 -> "May"
+            6 -> "Jun"
+            7 -> "Jul"
+            8 -> "Aug"
+            9 -> "Sept"
+            10 -> "Oct"
+            11 -> "Nov"
+            else -> "Dec"
+        }
+    }
+
+    return when (isAbbreviation) {
+        true -> "${getAbbreviation(getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.MONTH))}$endStr"
+        else -> "${getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.MONTH)}$endStr"
+    }
+}
+
+/**
+ * @param: endStr 结尾字符
+ * @description: 获得当前日期
+ */
+fun day(endStr: String? = ""): String = "${getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.DAY)}$endStr"
+
+/**
+ * @param: endStr 结尾字符
+ * @description: 获得当前小时
+ */
+fun hour(endStr: String? = ""): String = "${getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.HOUR)}$endStr"
+
+/**
+ * @param: endStr 结尾字符
+ * @description: 获得当前分钟
+ */
+fun minute(endStr: String? = ""): String = "${getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.MINUTE)}$endStr"
+
+/**
+ * @param: endStr 结尾字符
+ * @description: 获得当前秒数
+ */
+fun second(endStr: String? = ""): String = "${getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.SECOND)}$endStr"
+
+/**
+ * @param: endStr 结尾字符
+ * @param: isAbbreviation 是否简写
+ * @description: 获得当前星期
+ */
+fun week(endStr: String? = "", isAbbreviation: Boolean = false): String {
+    fun getAbbreviation(week: Int): String {
+        return when (week) {
+            1 -> "Mon"
+            2 -> "Tues"
+            3 -> "Wed"
+            4 -> "Thur"
+            5 -> "Fri"
+            6 -> "Sat"
+            else -> "Sun"
+        }
+    }
+
+    return when (isAbbreviation) {
+        true -> "${getAbbreviation(getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.WEEK))}$endStr"
+        else -> "${getSingleDate(singleDateFormat = DateUtils.SingleDateFormat.WEEK)}$endStr"
+    }
+}

+ 18 - 155
lib_common/src/main/java/com/convenient/android/common/utils/date/DateUtils.kt

@@ -10,10 +10,21 @@ import java.util.*
  * description: 时间相关的工具类
  */
 object DateUtils {
+    //默认的时间格式
+    enum class DateFormat(val dateFormat: String) {
+        DEFAULT_PATTERN("yyyy-MM-dd HH:mm:ss"),
+    }
 
-
-    const val DATE_DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss"
-
+    //默认单独时间格式
+    enum class SingleDateFormat {
+        YEAR,
+        MONTH,
+        DAY,
+        HOUR,
+        MINUTE,
+        SECOND,
+        WEEK
+    }
 
     /**
      * 获取格式化的时间
@@ -21,155 +32,11 @@ object DateUtils {
      * @param pattern 格式
      * @param locale 区域
      */
-    fun getFormatDate(time: Long, pattern: String, locale: Locale = Locale.getDefault()): String {
-        val sdf = SimpleDateFormat(pattern, locale)
-        return sdf.format(Date(time))
-    }
-
-    /**
-     * 获取当前格式化后的时间
-     */
-    fun getTime(pattern: String = DATE_DEFAULT_PATTERN): String {
-        return getFormatDate(System.currentTimeMillis(), pattern)
-    }
-
-
-    /**
-     * 返回指定的时间毫秒数
-     * @param date 时间
-     * @param pattern 传入的时间对应格式
-     * @description 时间格式例如:yyyy-MM-dd HH:mm:ss
-     */
-    fun getMillionSeconds(date: String, pattern: String): Long {
-        val sdf = SimpleDateFormat(pattern)
-        var millionSeconds: Long = 0
-        millionSeconds = try {
-            if (date.isEmpty()) {
-                return millionSeconds
-            }
-            // 毫秒
-            sdf.parse(date).time
-        } catch (e: Exception) {
-            e.printStackTrace()
-            return millionSeconds
-        }
-        return millionSeconds
-    }
-
-
-    /**
-     * @return
-     * @方法说明:判断用户的设备时区是否为东八区(中国)
-     * @方法名称:isInEasternEightZones
-     * @返回值:boolean
-     */
-    fun isInEasternEightZones(): Boolean {
-        return TimeZone.getDefault() === TimeZone.getTimeZone("GMT+08")
-    }
-
-
-    /**
-     * @param date
-     * @param oldZone
-     * @param newZone
-     * @return
-     * @方法说明:根据不同时区,转换时间 2014年7月31日
-     * @方法名称:transformTime
-     * @返回值:Date
-     */
-    fun transformTime(date: Date, oldZone: TimeZone, newZone: TimeZone): Date? {
-        val timeOffset = (oldZone.getOffset(date.time) - newZone.getOffset(date.time))
-        return Date(date.time - timeOffset)
-    }
+    fun getFormatDate(time: Long, pattern: String = DateFormat.DEFAULT_PATTERN.dateFormat, locale: Locale = Locale.getDefault()) = SimpleDateFormat(pattern, locale).format(Date(time))
 
+    fun isBefore(src: Date, destDate: Date): Boolean = src.before(destDate)
 
-    /**
-     * 获取星期几
-     * @return 1-7 对应星期一 - 星期日
-     */
-    fun getDayOfWeek(date: Date) : Int{
-        val weeks = arrayOf(7, 1, 2, 3, 4, 5,6)
-
-        val cal = Calendar.getInstance()
-        cal.time = date
-        var week_index = cal[Calendar.DAY_OF_WEEK] - 1
-        if (week_index < 0) {
-            week_index = 0
-        }
-        return weeks[week_index]
-    }
-
-
-    /**
-     * 将系统时间转成英文字符串 星期-日期-月份
-     *
-     * @return Friday 29, Sept.
-     */
-    fun systemTimeToEnglishString(): String {
-        val dataTime: String = getTime("yyyy-MM-dd")
-        val split = dataTime.split("-".toRegex()).toTypedArray()
-        var month = split[1]
-        val day = split[2]
-        var dayOfWeek: Int = getDayOfWeek(Date())
-        var week = ""
-        when (dayOfWeek) {
-            7 -> {
-                week = "Sunday"
-                week = "Sun"
-            }
-            1 -> {
-                week = "Monday"
-                week = "Mon"
-            }
-            2 -> {
-                week = "Tuesday"
-                week = "Tues"
-            }
-            3 -> {
-                week = "Wednesday"
-                week = "Wed"
-            }
-            4 -> {
-                week = "Thursday"
-                week = "Thur"
-            }
-            5 -> {
-                week = "Friday"
-                week = "Fri"
-            }
-            6 -> {
-                week = "Saturday"
-                week = "Sat"
-            }
-            else -> {}
-        }
-        when (month) {
-            "01" -> month = "Jan"
-            "02" -> month = "Feb"
-            "03" -> month = "Mar"
-            "04" -> month = "Apr"
-            "05" -> month = "May"
-            "06" -> month = "Jun"
-            "07" -> month = "Jul"
-            "08" -> month = "Aug"
-            "09" -> month = "Sept"
-            "10" -> month = "Oct"
-            "11" -> month = "Nov"
-            "12" -> month = "Dec"
-            else -> {}
-        }
-        return "$week , $month.$day"
-    }
-
-
-    fun isBefore(src : Date, destDate: Date) : Boolean{
-        return src.before(destDate)
-    }
-
-
-    fun isAfter(src: Date, destDate: Date) : Boolean{
-        return src.after(destDate)
-    }
+    fun isAfter(src: Date, destDate: Date): Boolean = src.after(destDate)
 
     /**
      * 传入的时间是否介于一段时间内
@@ -177,9 +44,5 @@ object DateUtils {
      * @param startDate 介于的开始时间
      * @param endDate 介于的结束时间
      */
-    fun timeBetweenDate(time : Date, startDate: Date, endDate: Date) : Boolean{
-        return isBefore(time, endDate) && isAfter(time, startDate)
-    }
-
-
+    fun timeBetweenDate(time: Date, startDate: Date, endDate: Date): Boolean = isBefore(time, endDate) && isAfter(time, startDate)
 }