【问题标题】:Compare date time android比较日期时间android
【发布时间】:2021-09-20 08:30:51
【问题描述】:

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

2021-07-10T12:37:52.770268

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

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

【问题讨论】:

标签: android kotlin


【解决方案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,您可以使用 this link

【讨论】:

【解决方案2】:

你可以使用这些代码:

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', ""));

【讨论】:

  • 我尝试了 DATE_TIME_MILISECONDS 但不起作用编辑:我尝试了顶部的,它有效
【解决方案3】:

我更喜欢为 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));
}

【讨论】:

  • 比较只返回 0、1 或 -1。我可以得到米利斯吗?因为 getMilis 不能在 Date 类之外访问
  • nvm,我创建了我的。感谢您的代表:D
【解决方案4】:

所以我创建了这个 ext 函数,基于 riteshPouria 答案

/**
    @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()
}

【讨论】:

    猜你喜欢
    • 2011-01-21
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2015-07-20
    • 2012-12-29
    • 2016-10-10
    • 2011-04-28
    相关资源
    最近更新 更多