gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)
kotlin get current date
- User
kotlin get current date
- Assistant
In Kotlin, you can get the current date using the
java.util.Dateclass. Here's an example:import java.util.Date fun main() { val currentDate = Date() println(currentDate) }The
java.util.Date()constructor creates a newDateobject representing the current date and time. ThecurrentDatevariable will hold this value.To format the date in a specific format, you can use the
SimpleDateFormatclass from thejava.textpackage. 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
SimpleDateFormatclass to specify the desired date format as "dd/MM/yyyy". Theformat()method is then called on thedateFormatobject to convert thecurrentDateinto a formatted string. Finally, the formatted date is printed to the console.