First, do use java.time
and its DateTimeFormatter
class for this. SimpleDateFormat
is notoriously troublesome and is long outdated along with the Date
class. java.time
is the modern Java date and time API and is so much nicer to work with.
Second, Joop Eggen is correct in his answer that your string looks like a URL encoded parameter that was originally Fri, 31 Dec 3999 23:00:00 GMT
. This sounds even more likely as this is a standard format known as RFC 1123 and commonly used with HTTP. So your library for getting your URL parameters should URL decode the string for you. Then it’s straightforward since the formatter to use has already been defined for you:
String startedFrom = "Fri, 31 Dec 3999 23:00:00 GMT";
OffsetDateTime result
= OffsetDateTime.parse(startedFrom, DateTimeFormatter.RFC_1123_DATE_TIME);
System.out.println(result);
This prints
3999-12-31T23:00Z
If you cannot get your library to URL decode, do it yourself by using URLDecoder
:
String startedFrom = "Fri,+31+Dec+3999+23:00:00+GMT";
try {
startedFrom = URLDecoder.decode(startedFrom, StandardCharsets.UTF_16.name());
} catch (UnsupportedEncodingException uee) {
throw new AssertionError("UTF_16 is not supported — this should not be possible", uee);
}
Now proceed as above.
You may of course also define a formatter to parse the string with the pluses in it. I don’t know why you should want to, though. If you do, you just need to have them in the format pattern string too:
DateTimeFormatter formatterWithPluses
= DateTimeFormatter.ofPattern("EEE,+d+MMM+yyyy+H:mm:ss+z", Locale.ROOT);
ZonedDateTime result = ZonedDateTime.parse(startedFrom, formatterWithPluses);
This time we got a ZonedDateTime
with GMT as the name of the time zone:
3999-12-31T23:00Z[GMT]
Depending on what you need the date-time for, you may convert it to OffsetDateTime
or Instant
by calling result.toOffsetDateTime()
or result.toInstant()
.