79460015

Date: 2025-02-22 17:44:53
Score: 0.5
Natty:
Report link

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.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: Qamar Abbas