From f2c2046b75b5a371f4425ee41f12920dc33684a3 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 17 Oct 2019 14:41:38 +0800 Subject: [PATCH] up --- .../java/io/legado/app/utils/DateUtils.kt | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 app/src/main/java/io/legado/app/utils/DateUtils.kt 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