From b0bac7d3c2e50f54e245ca6686752623329c174b Mon Sep 17 00:00:00 2001 From: yimiao Date: Mon, 19 Jul 2021 15:51:15 +0800 Subject: [PATCH] =?UTF-8?q?author:wangyimiao=20desc:=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB=E5=B7=AE=E5=88=86=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yinuo/commonlib/utils/BaseUtils.kt | 106 +----------------- .../com/yinuo/commonlib/utils/DateUtils.kt | 83 ++++++++++++++ .../com/yinuo/commonlib/utils/DisplayUtils.kt | 9 ++ .../com/yinuo/commonlib/utils/SpManager.kt | 28 ++++- .../com/yinuo/commonlib/utils/StringUtils.kt | 34 ++++++ 5 files changed, 156 insertions(+), 104 deletions(-) create mode 100644 commonLib/src/main/java/com/yinuo/commonlib/utils/DateUtils.kt create mode 100644 commonLib/src/main/java/com/yinuo/commonlib/utils/StringUtils.kt diff --git a/commonLib/src/main/java/com/yinuo/commonlib/utils/BaseUtils.kt b/commonLib/src/main/java/com/yinuo/commonlib/utils/BaseUtils.kt index 1084db8..2b0986b 100644 --- a/commonLib/src/main/java/com/yinuo/commonlib/utils/BaseUtils.kt +++ b/commonLib/src/main/java/com/yinuo/commonlib/utils/BaseUtils.kt @@ -1,117 +1,17 @@ package com.yinuo.commonlib.utils -import android.annotation.SuppressLint import android.content.Context -import android.text.TextUtils -import java.text.SimpleDateFormat -import java.util.* -import kotlin.collections.ArrayList +/** + * 基础工具类 + */ object BaseUtils { private const val TAG: String = "BaseUtils" - /** - * 如果使用大写HH标识使用24小时显示格式,如果使用小写hh就表示使用12小时制格式 - */ - const val DATE_TO_STRING_DETAIL_PATTERN = "yyyy-MM-dd HH:mm:ss" - - /** - * 年-月-日 显示格式 - * */ - const val DATE_TO_STRING_SHORT_PATTERN = "yyyy.MM.dd" - - /** - * 年-月-日 显示格式 - * */ - const val DATE_TO_STRING_LONG_PATTERN = "yyyy_MM_dd HH:mm:ss" - - /** - * 60秒 - */ - const val SECONDS = 60 - - /** - * 60分钟 - */ - const val MINUS = 60 - fun isListEmpty(list: List?): Boolean { return list == null || list.isEmpty() } - @SuppressLint("SimpleDateFormat") - fun getNowTimeFormat(format:String): String { - val formatter = SimpleDateFormat(format) - return formatter.format(getNow()) - } - - /** - * 得到现在时间 - * - * @return - */ - private fun getNow(): Date { - return Date() - } - - /** - * 将字符串List转化为用 | 分割的字符串 - */ - fun getStringSeparateByLine(list: List): String { - var result = "" - for (item in list.withIndex()) { - result = if (item.index == (list.size - 1)) { - "$result${item.value}" - } else { - "$result${item.value}|" - } - } - return result - } - - /** - * 将 | 分割符转化为list - */ - fun getListByString(source: String): List { - if (TextUtils.isEmpty(source)) { - return ArrayList() - } - return source.split("|") - } - - /** - * 将int型的数据转化成 XX:XX:XX" 形式的字符串 - */ - fun getDurationTimeByInt(source: Int): String { - var temp = source - var h = 0 - var m = 0 - var s = 0 - var result = "" - if (temp >= SECONDS * MINUS) { - h = temp / (SECONDS * MINUS) - temp -= h * (SECONDS * MINUS) - } - if (temp >= SECONDS) { - m = temp / SECONDS - temp -= m * SECONDS - } - s = temp - - result = when { - h > 0 -> { - "$h:$m:$s\"" - } - m > 0 -> { - "$m:$s\"" - } - else -> { - "$s\"" - } - } - return result - } - fun getExternalStoragePath(context: Context): String? { return context.getExternalFilesDir(null)?.path } diff --git a/commonLib/src/main/java/com/yinuo/commonlib/utils/DateUtils.kt b/commonLib/src/main/java/com/yinuo/commonlib/utils/DateUtils.kt new file mode 100644 index 0000000..13aed85 --- /dev/null +++ b/commonLib/src/main/java/com/yinuo/commonlib/utils/DateUtils.kt @@ -0,0 +1,83 @@ +package com.yinuo.commonlib.utils + +import android.annotation.SuppressLint +import java.text.SimpleDateFormat +import java.util.* + +/** + * 时间工具类 + */ +object DateUtils { + /** + * 如果使用大写HH标识使用24小时显示格式,如果使用小写hh就表示使用12小时制格式 + */ + const val DATE_TO_STRING_DETAIL_PATTERN = "yyyy-MM-dd HH:mm:ss" + + /** + * 年-月-日 显示格式 + * */ + const val DATE_TO_STRING_SHORT_PATTERN = "yyyy.MM.dd" + + /** + * 年-月-日 显示格式 + * */ + const val DATE_TO_STRING_LONG_PATTERN = "yyyy_MM_dd HH:mm:ss" + + /** + * 60秒 + */ + const val SECONDS = 60 + + /** + * 60分钟 + */ + const val MINUS = 60 + + /** + * 得到现在时间 + * + * @return + */ + private fun getNow(): Date { + return Date() + } + + @SuppressLint("SimpleDateFormat") + fun getNowTimeFormat(format: String): String { + val formatter = SimpleDateFormat(format) + return formatter.format(getNow()) + } + + /** + * 将int型的数据转化成 XX:XX:XX" 形式的字符串 + */ + fun getDurationTimeByInt(source: Int): String { + var temp = source + var h = 0 + var m = 0 + var s = 0 + var result = "" + if (temp >= SECONDS * MINUS) { + h = temp / (SECONDS * MINUS) + temp -= h * (SECONDS * MINUS) + } + if (temp >= SECONDS) { + m = temp / SECONDS + temp -= m * SECONDS + } + s = temp + + result = when { + h > 0 -> { + "$h:$m:$s\"" + } + m > 0 -> { + "$m:$s\"" + } + else -> { + "$s\"" + } + } + return result + } +} \ No newline at end of file diff --git a/commonLib/src/main/java/com/yinuo/commonlib/utils/DisplayUtils.kt b/commonLib/src/main/java/com/yinuo/commonlib/utils/DisplayUtils.kt index 0a8e3a7..c484207 100644 --- a/commonLib/src/main/java/com/yinuo/commonlib/utils/DisplayUtils.kt +++ b/commonLib/src/main/java/com/yinuo/commonlib/utils/DisplayUtils.kt @@ -2,13 +2,22 @@ package com.yinuo.commonlib.utils import android.content.Context +/** + * 界面工具类 + */ object DisplayUtils { + /** + * dp 转 px + */ fun dp2px(context: Context, dpVale: Float): Int { val density = context.resources.displayMetrics.density return ((dpVale * density + 0.5f).toInt()) } + /** + * px 转 dp + */ fun px2dp(context: Context, pxVale: Float): Int { val density = context.resources.displayMetrics.density return ((pxVale / density + 0.5f).toInt()) diff --git a/commonLib/src/main/java/com/yinuo/commonlib/utils/SpManager.kt b/commonLib/src/main/java/com/yinuo/commonlib/utils/SpManager.kt index 5ae93c5..46dcadf 100644 --- a/commonLib/src/main/java/com/yinuo/commonlib/utils/SpManager.kt +++ b/commonLib/src/main/java/com/yinuo/commonlib/utils/SpManager.kt @@ -4,7 +4,6 @@ import android.annotation.SuppressLint import android.content.Context import android.content.SharedPreferences - /** * sharedPreference管理类 */ @@ -27,4 +26,31 @@ object SpManager { fun getBoolean(key: String): Boolean { return sp!!.getBoolean(key, false) } + + fun putInt(key: String, value: Int) { + editor!!.putInt(key, value) + editor!!.apply() + } + + fun getInt(key: String): Int { + return sp!!.getInt(key, -1) + } + + fun putString(key: String, value: String) { + editor!!.putString(key, value) + editor!!.apply() + } + + fun getString(key: String): String? { + return sp!!.getString(key, "") + } + + fun putLong(key: String, value: Long) { + editor!!.putLong(key, value) + editor!!.apply() + } + + fun getLong(key: String): Long { + return sp!!.getLong(key, -1L) + } } \ No newline at end of file diff --git a/commonLib/src/main/java/com/yinuo/commonlib/utils/StringUtils.kt b/commonLib/src/main/java/com/yinuo/commonlib/utils/StringUtils.kt new file mode 100644 index 0000000..858a558 --- /dev/null +++ b/commonLib/src/main/java/com/yinuo/commonlib/utils/StringUtils.kt @@ -0,0 +1,34 @@ +package com.yinuo.commonlib.utils + +import android.text.TextUtils + +/** + * 字符串工具类 + */ +class StringUtils { + + /** + * 将字符串List转化为用 | 分割的字符串 + */ + fun getStringSeparateByLine(list: List): String { + var result = "" + for (item in list.withIndex()) { + result = if (item.index == (list.size - 1)) { + "$result${item.value}" + } else { + "$result${item.value}|" + } + } + return result + } + + /** + * 将 | 分割符转化为list + */ + fun getListByString(source: String): List { + if (TextUtils.isEmpty(source)) { + return ArrayList() + } + return source.split("|") + } +} \ No newline at end of file