1

I am dealing with strings such as the following: 0022 GMT (0822 HKT) July 21, 2016

Obviously these strings specify the time of the day twice for two different time zones. Can the pattern syntax of Joda Time's DateTimeFormat.forPattern() handle this kind of redundant information?

One possibility would be to just ignore one of the two time expressions 0022 GMT and 0822 HKT. This would require some kind of wildcard that could match the part of the time expression to be ignored which would look something like Hm z '(*)' MMM dd, y.

Does such a wildcard or anything else that could parse the above time string exist in Joda Time's pattern syntax?

zepp133
  • 1,542
  • 2
  • 19
  • 23

1 Answers1

1

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
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126