Kotlin compare two Dates

I’m trying to make input validation on a date that the user add. I want to check if the Date added by the user is after Today Date, but I don’t understand how to do this. The date that the user insert is a String so i have to convert it to a Date, but I need to get the today date and compare with the input date. For now I write this:

fun isDateValid(myDate: String) : Boolean{
           val date = SimpleDateFormat("yyyy-MM-dd").parse(myDate)
           return //compare 2 the dates
}

So i convert the date given by the user to Date type, but how can I get the local Date (now) and compare the two Dates? I am using API 21 so I can’t use LocalDateTime because it give me error

UPDATE I trying Lino answer:

fun isDateValid(myDate: String) : Boolean{
            val date = SimpleDateFormat("yyyy-MM-dd").parse(myDate)
            return !date.before(Date())
}

but i get this error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.ticketapp, PID: 9173
    java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
     Caused by: java.text.ParseException: Unparseable date: ""
        at java.text.DateFormat.parse(DateFormat.java:362)
        at com.example.ticketapp.Util$Companion.isDeadLineValid(Util.kt:32)
        at com.example.ticketapp.ui.newTicket.NewTicketViewModel.checkInputs(NewTicketViewModel.kt:222)
        at com.example.ticketapp.ui.newTicket.NewTicketViewModel.createTicket(NewTicketViewModel.kt:196)
        at com.example.ticketapp.databinding.FragmentNewTicketBindingImpl._internalCallbackOnClick(FragmentNewTicketBindingImpl.java:783)
        at com.example.ticketapp.generated.callback.OnClickListener.onClick(OnClickListener.java:11)
        at android.view.View.performClick(View.java:7448)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
        at android.view.View.performClickInternal(View.java:7425)
        at android.view.View.access$3600(View.java:810)
        at android.view.View$PerformClick.run(View.java:28305)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
I/Process: Sending signal. PID: 9173 SIG: 9

Answer

You can use the following to check if your date is valid:

fun isDateValid(myDate: String) : Boolean {
    try {
        val date = SimpleDateFormat("yyyy-MM-dd").parse(myDate)
        return !date.before(Date())
    } catch(ignored: java.text.ParseException) {
        return false
    }
}

You can also factor out the SimpleDateFormat and put that into a top-level variable in the same file / class as isDateValid():

private val formatter = SimpleDateFormat("yyyy-MM-dd")

then replace:

val date = SimpleDateFormat("yyyy-MM-dd").parse(myDate)

with

val date = formatter.parse(myDate)

Leave a Reply

Your email address will not be published. Required fields are marked *