繁体   English   中英

比较日期时间android

[英]Compare date time android

将此字符串转换为时间并将其与Android中的当前时间进行比较的最佳方法是什么? 字符串看起来像这样

2021-07-10T12:37:52.770268

现在我只是考虑基于 T 拆分它,因此它具有日期和时间,但我不知道如何格式化和比较这种时间字符串。

以及如何显示它们之间的时间,例如 10 分钟前、1 小时前等)。

使用此代码解析日期时间字符串:

val dateTime = LocalDateTime.parse("12-08-2016T10:20:30.343", DateTimeFormatter.ofPattern("M/d/y HH:mm:ss.SSS"))

为了比较两个日期,您可以使用日期时间的isAfterisBefore方法。 像这样:

val dateTime1 = LocalDateTime.parse("12-08-2016T10:20:30.343", DateTimeFormatter.ofPattern("M/d/y HH:mm:ss.SSS"))
val dateTime2 = LocalDateTime.parse("10-07-2016T10:20:30.343", DateTimeFormatter.ofPattern("M/d/y HH:mm:ss.SSS"))
dateTime1.isAfter(dateTime2)

更新:对于小于 26 的 API,您可以使用此链接

你可以使用这些代码:

String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
String DATE_TIME_WITH_SPACE = "yyyy-MM-dd HH:mm:ss";
String DATE_SHORT_FORMAT = "yyyy-MM-dd";
String DATE_SHORT_FORMAT_WITH_SLASH = "yyyy/MM/dd";
String NORMAL_DATE_TIME_FORMAT = "yyyy/MM/dd kk:mm:ss";
String DATE_TIME_MILISECONDS = "yyyy-MM-dd'T'HH:mm:ss.sss";
String DATE_TIME_MILISCONDS_2 = "yyyy-MM-dd HH:mm:ss.sss";
String TIME_FORMAT = "HH:mm:ss";
String SHORT_TIME_FORMAT = "HH:mm";

例如代码

    private SimpleDateFormat simpleDateFormatDATE_TIME_WITH_SPACE = new SimpleDateFormat(DATE_TIME_WITH_SPACE);   

simpleDateFormatDATE_TIME_WITH_SPACE.parse('your string date', ""));

我更喜欢为 String 创建一个扩展函数以将其转换为日期对象:

fun String.toDate(): Date? {
    val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
    return dateFormat.parse(this)
}

fun Date.compareToCurrentDateTime() = this.compareTo(Date())

使用以下日期类的方法:

/**
 * Compares two Dates for ordering.
 *
 * @param   anotherDate   the <code>Date</code> to be compared.
 * @return  the value <code>0</code> if the argument Date is equal to
 *          this Date; a value less than <code>0</code> if this Date
 *          is before the Date argument; and a value greater than
 *      <code>0</code> if this Date is after the Date argument.
 * @since   1.2
 * @exception NullPointerException if <code>anotherDate</code> is null.
 */
public int compareTo(Date anotherDate) {
    long thisTime = getMillisOf(this);
    long anotherTime = getMillisOf(anotherDate);
    return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1));
}

所以我根据riteshPouria 的回答创建了这个 ext 函数

/**
    @return  formatted date from given String
 */
fun String.toDate(): Date? {
    val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault())
    return dateFormat.parse(this)
}

/**
    @return  the difference, measure in milliseconds
             between current system time and the time passed to the function
 */
fun Date.compareToCurrentDateTime(): Long {
    val thisTime: Long = this.time
    val currentTime: Long = System.currentTimeMillis();
    return currentTime - thisTime
}

/**
    @return  amount of minutes, in Int
             from given milliseconds value
 */
fun Long.getMinutes(): Int {
    return TimeUnit.MILLISECONDS.toMinutes(this).toInt()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM