Example input string: Mon Feb 07 07:47:04 UTC 2022
I have tried various different patterns, and none work, mostly I get the error:
java.time.format.DateTimeParseException: Text 'Mon Feb 07 07:47:04 UTC 2022' could not be parsed at index 4
This is my code (it's spark/scala but using Java libraries as is allowed). I need to convert strings of the form above, into a numeric value in seconds since the Unix epoch. I don't have control over the input format (it's from an external API) - I could use string manipulation on it if I really had to, but ideally just need to find a pattern that the formatter accepts.
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;
import java.time.ZoneOffset;
import java.time.LocalDateTime;
def df = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("EEE LLL d H:m:s 'UTC' uuuu").toFormatter(Locale.ENGLISH);
def dateTimeStringToEpoch(s: String): Long = LocalDateTime.parse(s, df).toEpochSecond(ZoneOffset.UTC)
println(dateTimeStringToEpoch("Mon Feb 07 07:47:04 UTC 2022"));
Thanks