Home > Software engineering >  I am designing this layout can any one suggest me that how would i print this text like today yester
I am designing this layout can any one suggest me that how would i print this text like today yester

Time:08-10

Here Is my code:

object DateTimeUtils {

    private val calendar by lazy {
        Calendar.getInstance()
    }

    fun calculateTodayYesterday(time:Long):String {
        var label = ""

        label = when (time) {
            calendar.timeInMillis -> {
                "Today"
            }

            (time - 86400000) -> {
                "Yesterday"
            }

            else -> {
                val sdf = SimpleDateFormat("dd MMMM, yyyy", Locale.getDefault())
                sdf.format(time)
            }
        }
        return label
    }
}

Please suggest me any modification or provide me another code.

CodePudding user response:

try this :

fun calculateTodayYesterday(time: Long) : String {
    val currentTime = System.currentTimeMillis()

    if (currentTime - time > 2*86400000) {
        val sdf = SimpleDateFormat("dd MMMM, yyyy", Locale.getDefault())
        return sdf.format(time)
    } else if (currentTime - time > 86400000) {
        return "Yesterday"
    } else {
        return "Today"
    }
}

CodePudding user response:

With java.time you can easily find out if a moment in time (time: Long as epoch millis) has a specific date (LocalDate) in a specific time zone (ZoneId).

Here's a possible modification of your code:

object DateTimeUtils {

    private val today by lazy {
        // date of today in system default time zone
        LocalDate.now()
    }

    // formatter for desired format of a LocalDate
    private val dateFormatter = DateTimeFormatter.ofPattern(
                                    "dd MMMM, uuuu",
                                            Locale.getDefault()
                                )

    fun calculateTodayYesterday(time: Long): String {
        // create a moment in time from the given millis
        val instant = Instant.ofEpochMilli(time)
        /*
         * use that moment to create a datetime in the time zone of the device/jvm,
         * and extract the date-part because that's the information needed
         */
        var givenDate = ZonedDateTime.ofInstant(
                                instant,
                                ZoneId.systemDefault()
                             ).toLocalDate()
        // directly return the result of the when-statement
        return when (givenDate) {
            today -> "Today"
            today.minusDays(1) -> "Yesterday"
            else -> givenDate.format(dateFormatter)
        }
    }
}

Example use in a main:

fun main(args: Array<String>) {
    val todayMillis = Instant.now().toEpochMilli()
    val yesterdayMillis = todayMillis - 86400000
    val millisBeforeYesterday = yesterdayMillis - 86405000

    println("Epoch millis $todayMillis: ${DateTimeUtils.calculateTodayYesterday(todayMillis)}")
    println("Epoch millis $yesterdayMillis: ${DateTimeUtils.calculateTodayYesterday(yesterdayMillis)}")
    println("Epoch millis $millisBeforeYesterday: ${DateTimeUtils.calculateTodayYesterday(millisBeforeYesterday)}")
}

Output of example use (default locale was Locale.GERMANY):

Epoch millis 1660120081052: today
Epoch millis 1660033681052: yesterday
Epoch millis 1659947276052: 08 August, 2022
  • Related