How can I calculate and format time like HH:mm, Of much time is left before tomorrow? with SimpleDateFormat.
Question posted in Android Studio
The official documentation can be found here.
The official documentation can be found here.
How can I calculate and format time like HH:mm, Of much time is left before tomorrow? with SimpleDateFormat.
2
Answers
SimpleDateFormat
and related legacy classes were years ago supplanted by the modern java.time classes defined in JSR 310.Determining “tomorrow” requires a time zone. Days start earlier as you move eastward through time zones.
Capture the current moment as seen through that time zone.
Now we are in a position to determine “tomorrow”, specifically the first moment of the following day.
Extract the date portion.
Get first moment. Note that we do not assume the day starts at 00:00 🕛. Some dates in some zones start at a different time of day, such as 01:00 🕐. Let java.time determine the first moment.
Calculate time to elapse.
Generate text in standard ISO 8601 format.
If you insist on the risky ambiguous use of clock-time to represent the duration, you can build your own string by interrogating the
Duration
object. Call itsto…Part
methods.