diff --git a/app/src/main/java/io/legado/app/utils/DateUtils.kt b/app/src/main/java/io/legado/app/utils/DateUtils.kt new file mode 100644 index 000000000..dc1c81f62 --- /dev/null +++ b/app/src/main/java/io/legado/app/utils/DateUtils.kt @@ -0,0 +1,30 @@ +package io.legado.app.utils + +import android.annotation.SuppressLint +import java.text.ParseException +import java.text.SimpleDateFormat +import java.util.* + + +object DateUtils { + + /** + * @Description: 任意时间字符串转换成时间,无需指定解析模板 + * */ + @SuppressLint("SimpleDateFormat") + @Throws(ParseException::class) + fun parseStringToDate(date: String): Date { + val result: Date + var parse = date.replaceFirst("[0-9]{4}([^0-9]?)".toRegex(), "yyyy$1") + parse = parse.replaceFirst("^[0-9]{2}([^0-9]?)".toRegex(), "yy$1") + parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)".toRegex(), "$1MM$2") + parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}( ?)".toRegex(), "$1dd$2") + parse = parse.replaceFirst("( )[0-9]{1,2}([^0-9]?)".toRegex(), "$1HH$2") + parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)".toRegex(), "$1mm$2") + parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)".toRegex(), "$1ss$2") + val format = SimpleDateFormat(parse) + result = format.parse(date) + return result + } + +} \ No newline at end of file