简体   繁体   中英

Compare date time android

What is the best way to convert this string to time and compare it to current time in Android? The string looks like this

2021-07-10T12:37:52.770268

For now i just thought about the splitting it base on T so it has date and time, but i dont know how to format and compare this kind of time string.

And how to display the time between them, like 10 minutes ago, 1 hour ago, etc).

Using this code to parse date time string:

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

for comparing two date, you can use isAfter and isBefore method of datetime. like this:

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)

Update: You can use the this link for API less than 26.

you can use these code :

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";

for example code

    private SimpleDateFormat simpleDateFormatDATE_TIME_WITH_SPACE = new SimpleDateFormat(DATE_TIME_WITH_SPACE);   

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

I prefer to create a extension function for String to convert it into date object:

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

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

Using below method of date class:

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

So i created this ext function, based on ritesh and Pouria answer

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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