author:wangyimiao

desc:工具类差分整理
master
yimiao 3 years ago
parent 5d8da4bff7
commit b0bac7d3c2

@ -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
object BaseUtils {
private const val TAG: String = "BaseUtils"
fun <T> isListEmpty(list: List<T>?): 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>): 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<String> {
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
}

@ -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
}
}

@ -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())

@ -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)
}
}

@ -0,0 +1,34 @@
package com.yinuo.commonlib.utils
import android.text.TextUtils
/**
* 字符串工具类
*/
class StringUtils {
/**
* 将字符串List转化为用 | 分割的字符串
*/
fun getStringSeparateByLine(list: List<String>): 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<String> {
if (TextUtils.isEmpty(source)) {
return ArrayList()
}
return source.split("|")
}
}
Loading…
Cancel
Save