I am receiving a string in ISO 8601 date-time format 2020-11-03T15:23:24.388Z
. What is the best way to convert this into epoch seconds in Java?
Using the modern date-time API, you can do it as shown below:
import java.time.Instant;
public class Main {
public static void main(String[] args) {
Instant instant = Instant.parse("2020-11-03T15:23:24.388Z");
long seconds = instant.getEpochSecond();
System.out.println(seconds);
}
}
Output:
1604417004
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.
Learn more about the modern date-time API at Trail: Date Time.