Skip to content

Commit

Permalink
修改几处扩展
Browse files Browse the repository at this point in the history
  • Loading branch information
caoyanglee committed May 18, 2021
1 parent e0a8ed3 commit f2905c0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
6 changes: 2 additions & 4 deletions lib/src/main/java/com/pmm/ui/ktx/CoreKt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.pmm.ui.ktx
import android.content.ContentResolver
import android.graphics.Color
import android.provider.Settings
import androidx.core.graphics.ColorUtils
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -54,10 +55,7 @@ fun doubleClick(singleClick: () -> Unit, doubleClick: () -> Unit, delay: Long =
/**
* 是否是亮色
*/
fun Int.isLightColor(): Boolean {
val darkness = 1 - (0.299 * Color.red(this) + 0.587 * Color.green(this) + 0.114 * Color.blue(this)) / 255
return darkness < 0.5
}
fun Int.isLightColor(): Boolean = ColorUtils.calculateLuminance(this) >= 0.5



Expand Down
39 changes: 21 additions & 18 deletions lib/src/main/java/com/pmm/ui/ktx/TimeKt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,25 @@ import java.util.*
* String转Date
* @param format 模板 yyyy-MM-dd HH:mm:ss
*/
fun String.string2Date(
format: String = "yyyy-MM-dd HH:mm:ss",
local: Locale = Locale.getDefault(),
isUtc: Boolean = false
): Date? = SimpleDateFormat(format, local).apply {
if (isUtc) this.timeZone = TimeZone.getTimeZone("UTC")
}.parse(this)
fun String?.string2Date(
format: String = "yyyy-MM-dd HH:mm:ss",
local: Locale = Locale.getDefault(),
isUtc: Boolean = false
): Date? {
if (this.isNullOrEmpty()) return null
return SimpleDateFormat(format, local).apply {
if (isUtc) this.timeZone = TimeZone.getTimeZone("UTC")
}.parse(this)
}

/**
* Date转String
* @param format 模板 yyyy-MM-dd HH:mm:ss
*/
fun Date.date2String(
format: String = "yyyy-MM-dd HH:mm:ss",
local: Locale = Locale.getDefault(),
isUtc: Boolean = false
format: String = "yyyy-MM-dd HH:mm:ss",
local: Locale = Locale.getDefault(),
isUtc: Boolean = false
): String = SimpleDateFormat(format, local).apply {
if (isUtc) this.timeZone = TimeZone.getTimeZone("UTC")
}.format(this)
Expand All @@ -36,9 +39,9 @@ fun Date.date2String(
* @param format 模板 yyyy-MM-dd HH:mm:ss
*/
fun Long.formatDate(
format: String = "yyyy-MM-dd HH:mm:ss",
local: Locale = Locale.getDefault(),
isUtc: Boolean = false
format: String = "yyyy-MM-dd HH:mm:ss",
local: Locale = Locale.getDefault(),
isUtc: Boolean = false
): String = SimpleDateFormat(format, local).apply {
if (isUtc) this.timeZone = TimeZone.getTimeZone("UTC")
}.format(Date(this))
Expand Down Expand Up @@ -109,7 +112,7 @@ fun Long.lessThanDays(day: Int = 1): Boolean {
val currentTime = Calendar.getInstance().time.time
val recordTime = this
if (recordTime == 0L) return false
if (recordTime>currentTime)return false
if (recordTime > currentTime) return false
val differ = currentTime - recordTime
if (differ < 1000 * 60 * 60 * 24 * day) {
return true
Expand All @@ -130,8 +133,8 @@ fun Date.lessThanDays(day: Int = 1): Boolean = this.time.lessThanDays(day)
* @param utcFormatStr 模板 yyyy-MM-dd'T'HH:mm:ss.SSS'Z' 2019-05-09T05:52:56.000Z
*/
fun String.local2UTC(
localFormatStr: String = "yyyy-MM-dd HH:mm:ss",
utcFormatStr: String = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
localFormatStr: String = "yyyy-MM-dd HH:mm:ss",
utcFormatStr: String = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
): String {
val locaFormat = SimpleDateFormat(localFormatStr, Locale.getDefault()).apply {
this.timeZone = TimeZone.getDefault()
Expand All @@ -156,8 +159,8 @@ fun String.local2UTC(
* @param utcFormatStr 模板 yyyy-MM-dd'T'HH:mm:ss.SSS'Z' 2019-05-09T05:52:56.000Z
*/
fun String.utc2Local(
localFormatStr: String = "yyyy-MM-dd HH:mm:ss",
utcFormatStr: String = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
localFormatStr: String = "yyyy-MM-dd HH:mm:ss",
utcFormatStr: String = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
): String {
if (this.isBlank()) return ""
val utcFormat = SimpleDateFormat(utcFormatStr, Locale.getDefault()).apply {
Expand Down

0 comments on commit f2905c0

Please sign in to comment.