Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin get date for tomorrow only

Tags:

date

kotlin

I need to display tomorrow's date only , l have this code and his working fine without problem . and he is give the current date for today. l want change this code to get the date for tomorrow but l dont know how !

  private fun date24hours(s: String): String? {
        try {
            val sdf = SimpleDateFormat("EE, MMM d, yyy")
            val netDate = Date(s.toLong() * 1000)
            return sdf.format(netDate)
        } catch (e: Exception) {
            return e.toString()
like image 534
Ali Ghassan Avatar asked Dec 22 '25 22:12

Ali Ghassan


2 Answers

I might be late to the party, but this is what I found works for me

const val DATE_PATTERN = "MM/dd/yyyy"

internal fun getDateTomorrow(): String {
    val tomorrow = LocalDate.now().plusDays(1)
    return tomorrow.toString(DATE_PATTERN)
}
like image 176
Princeps Polycap Avatar answered Dec 24 '25 11:12

Princeps Polycap


It is possible to use Date for this, but Java 8 LocalDate is a lot easier to work with:

// Set up our formatter with a custom pattern
val formatter = DateTimeFormatter.ofPattern("EE, MMM d, yyy")

// Parse our string with our custom formatter
var parsedDate = LocalDate.parse(s, formatter)

// Simply plus 1 day to make it tomorrows date
parsedDate = parsedDate.plusDays(1)
like image 20
Eamon Scullion Avatar answered Dec 24 '25 11:12

Eamon Scullion