In order to ignore the potentially ambivalent part (repeating hour, minute and zone name in other zone), you have to write your own DateTimeParser in Joda-Time:
DateTimeFormatter dtf =
new DateTimeFormatterBuilder().appendPattern("HHmm 'GMT' (").append(
new DateTimeParser() {
@Override
public int estimateParsedLength() {
return 10;
}
@Override
public int parseInto(DateTimeParserBucket bucket, String text, int position) {
int pos = position;
while (text.charAt(pos) != ')') {
pos++;
}
return pos;
}
}
)
.appendPattern(") MMMM dd, yyyy")
.toFormatter()
.withLocale(Locale.US)
.withZoneUTC();
String input = "0022 GMT (0822 HKT) July 21, 2016";
DateTime dt = dtf.parseDateTime(input);
System.out.println("Joda: " + dt); // 2016-07-21T00:22:00.000Z
For your information, I don't see any way to do this in Java-8 (without input preprocessing), see this example which will throw an exception even with using optional sections. Java-8 lacks a mechanism to write your own parser.
DateTimeFormatter dtf =
DateTimeFormatter.ofPattern("HHmm z ([HHmm z]) MMMM dd, uuuu", Locale.US);
ZonedDateTime zdt = ZonedDateTime.parse(input, dtf); // throws exception!!!
// java.time.format.DateTimeParseException:
// Text '0022 GMT (0822 HKT) July 21, 2016' could not be parsed at index 10
Side note: When you might explore my library Time4J which offers an alternative and more performant parse engine suitable for Java-8, then it offers an easier solution than Joda-Time, see this small gist example.
Else writing a hackish workaround using string preprocessing is always possible (interesting when a 3rd-party-library is not allowed):
String input = "0022 GMT (0822 HKT) July 21, 2016";
StringBuilder sb = new StringBuilder();
boolean markedForRemoval = false;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c == ')') {
markedForRemoval = false;
}
if (!markedForRemoval) {
sb.append(c);
}
if (c == '(') {
markedForRemoval = true;
}
}
input = sb.toString();
System.out.println(input); // 0022 GMT () July 21, 2016
// continue parsing the changed input based on a formatter of your choice