You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.3 KiB

package com.gyf.csams.util
import okhttp3.internal.toHexString
import java.text.SimpleDateFormat
import java.util.*
fun randomNum(length: Int = 8): String {
return List(length) { ('0'..'9').random() }.joinToString("")
}
fun encode(char: Char) = "\\u${char.toInt().toHexString()}"
//unicode ->String
fun String.decodeUnicode(): String {
fun decode1(unicode: String) = unicode.toInt(16).toChar()
val unicodes = this.split("\\u").mapNotNull { if (it.isNotBlank()) decode1(it) else null }
return String(unicodes.toCharArray())
}
val CHINESE_UNICODE_AREA = 0X4e00..0X9fa5
/**
* 随机中文
*
* @param length
* @return
*/
fun randomChinese(length: Int = 8): String {
return List(length) {
"\\u${
CHINESE_UNICODE_AREA.random().toHexString()
}".decodeUnicode()
}.joinToString("")
}
const val DATETIME_FORMAT = "yyyy-MM-dd HH:mm"
const val START_TIME = "2021-01-01 00:00"
val FORMAT = SimpleDateFormat(DATETIME_FORMAT, Locale.US)
val startUnix = FORMAT.parse(START_TIME)?.time
fun randomDateTime(): Date {
if (startUnix != null) {
return Date("${(startUnix..Date().time).random()}".toLong())
} else {
throw IllegalArgumentException("生成随机失败,无法获取起始时间")
}
}
fun Date.format(): String {
return FORMAT.format(this)
}