Starting from Java 8+, Android supports the java.time API, which is more efficient, thread-safe, and easier to work with. Here’s how you can achieve the same functionality using LocalDateTime:
val dateTimeFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss")
// Get current time
val currentTime = LocalDateTime.now()
Log.d("TIME", "Current Time: ${currentTime.format(dateTimeFormatter)}")
// Add 10 hours
val newTime = currentTime.plusHours(10)
Log.d("TIME", "Time after 10 hours: ${newTime.format(dateTimeFormatter)}")
Output will be
Current Time: 05/14/2014 01:10:00
Time after 10 hours: 05/14/2014 11:10:00
Why Use java.time?
This approach ensures modern, efficient, and maintainable code in your Android app.