I want to get a dateTime which is some hours before the specified datetime. The datetime will be in string format and depending on configuration, need a datetime n hours before the given time( example 3 or 4 hours before the given time).
The time format of mine is 2020-10-20T13:00:00-05:00
Using the modern date-time API:
Output:
Learn more about the modern date-time API at Trail: Date Time.
If you are doing it for your Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
Using Joda-Time:
Output:
Note: Check the following notice at the Home Page of Joda-Time
Using legacy API:
Output:
Recommendation: The date-time API of
java.util
and their formatting API,SimpleDateFormat
are outdated and error-prone. I suggest you should stop using them completely and switch to the modern date-time API.