Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)

kotlin get current date

  1. User

    kotlin get current date

  2. Assistant

    In Kotlin, you can get the current date using the java.util.Date class. Here's an example:

    import java.util.Date
    
    fun main() {
        val currentDate = Date()
        println(currentDate)
    }
    

    The java.util.Date() constructor creates a new Date object representing the current date and time. The currentDate variable will hold this value.

    To format the date in a specific format, you can use the SimpleDateFormat class from the java.text package. Here's an example:

    import java.text.SimpleDateFormat
    import java.util.Date
    
    fun main() {
        val currentDate = Date()
        val dateFormat = SimpleDateFormat("dd/MM/yyyy")
        val formattedDate = dateFormat.format(currentDate)
        println(formattedDate)
    }
    

    This code uses the SimpleDateFormat class to specify the desired date format as "dd/MM/yyyy". The format() method is then called on the dateFormat object to convert the currentDate into a formatted string. Finally, the formatted date is printed to the console.